library-rust/readme.md

118 lines
2.6 KiB
Markdown

Rust Library
================
> 專案共用核心
### 建立專案的指令
```bash
cargo new my_project_name
```
### 推薦項目的目錄結構
```
my_project/
├── Cargo.toml
├── src/
│ ├── main.rs
│ ├── lib.rs
│ ├── common/
│ │ ├── mod.rs
│ │ ├── config.rs
│ │ ├── errors.rs
│ │ ├── utils.rs
│ │ ├── logger.rs
│ │ └── types.rs
│ └── some_other_module/
│ ├── mod.rs
│ └── other.rs
└── tests/
├── common_tests.rs
└── other_tests.rs
```
### Current modules
| module | latest version |
|------------------------------|----------------|
| [config](conf) | v1.0.0 |
## 如何新增Module
> 假設新增一個 `module`
>`utils/first_utils/first`
### 初始化
再一路的路徑底下的 mod.rs 新增資料匣或檔案名稱
1. src/ 下 lib.rs
```rust
pub mod utils;
```
2. utils/ 下 mod.rs
```rust
pub mod first_utils;
```
3. first_utils/ 下 mod.rs
```rust
pub mod first;
```
這樣 first 裡面的 pub 函數就會被公開了
p.s 建議測試覆蓋率盡量達到80%
------------------
## 如何使用私有Module
1. 在新項目底下加入 .cargo 目錄 並新增 config.toml 檔案
```
my_project/
├── Cargo.toml
├── src/
│ ├── main.rs
│ └── utils/
│ └── first_utils/
│ └── first.rs
└── .cargo/
└── config.toml
```
2. 使用下列指令執行
```bash
mkdir -p .cargo
echo "[net]\ngit-fetch-with-cli = true" > .cargo/config.toml
```
3. 在 cargo.toml 內加入下列指令,可指定分隻或者 tag 都可
```toml
[dependencies]
utils = { git = "ssh://git@git.30cm.net/Wanderland/library-rusttgeoip.git", branch = "main" }
```
### 如果未產生 ssh key 連線至你的 git 的話請使用下列方式
產生ssh-key
請參考 [github ssh](https://docs.github.com/en/authentication/connecting-to-github-with-ssh) , [gitlab ssh](https://docs.gitlab.com/ee/user/ssh.html)
add ssh key to the ssh-agent
請參考 [Adding your SSH key to the ssh-agent](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent)
```bash
$ eval "$(ssh-agent -s)"
> Agent pid 59566
```
```bash
$ touch ~/.ssh/config
```
```
Host code.30cm.net
Hostname code.30cm.net
User igs170911
IdentityFile ~/.ssh/id_rsa
```
```bash
$ ssh-add --apple-use-keychain ~/.ssh/id_ed25519
```