專案共用工具包
Go to file
daniel.w 51da2eed19 feat: cargo fmt 2024-06-16 17:57:35 +08:00
src feat: cargo fmt 2024-06-16 17:57:35 +08:00
tests feat: cargo fmt 2024-06-16 17:57:35 +08:00
.gitignore init common library 2024-06-16 09:38:23 +08:00
Cargo.toml init common library 2024-06-16 12:48:34 +08:00
readme.md feat: add use common library way readme 2024-06-16 17:53:50 +08:00

readme.md

Rust Library

專案共用核心

建立專案的指令

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 v1.0.0

如何新增Module

假設新增一個 module utils/first_utils/first

初始化

再一路的路徑底下的 mod.rs 新增資料匣或檔案名稱

  1. src/ 下 lib.rs
pub mod utils;
  1. utils/ 下 mod.rs
pub mod first_utils;
  1. first_utils/ 下 mod.rs
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
  1. 使用下列指令執行
mkdir -p .cargo
echo "[net]\ngit-fetch-with-cli = true" > .cargo/config.toml
  1. 在 cargo.toml 內加入下列指令,可指定分隻或者 tag 都可
[dependencies]
utils = { git = "ssh://git@git.30cm.net/Wanderland/library-rusttgeoip.git", branch = "main" }

如果未產生 ssh key 連線至你的 git 的話請使用下列方式

產生ssh-key 請參考 github ssh , gitlab ssh

add ssh key to the ssh-agent 請參考 Adding your SSH key to the ssh-agent

$ eval "$(ssh-agent -s)"
> Agent pid 59566
$ touch ~/.ssh/config
Host code.30cm.net
  Hostname code.30cm.net
  User igs170911
  IdentityFile ~/.ssh/id_rsa
$ ssh-add --apple-use-keychain ~/.ssh/id_ed25519