diff --git a/Cargo.toml b/Cargo.toml index 38d1955..471c0f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,4 @@ name = "library-rust" version = "0.1.0" edition = "2021" -[dependencies] +[dependencies] \ No newline at end of file diff --git a/readme.md b/readme.md index 825ed57..d3d6db4 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,133 @@ 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 函數就會被公開了 +```bash +mkdir -p config +cd ./config +go mod init gitea.30cm.net/Ark/library-go/config +go work use ./config +``` + +p.s 建議測試覆蓋率盡量達到80% + +```bash +before-commit-check +``` + +2. commit + +```bash +git add ./config +git commit -m "feat: add config module" +git tag "config/v1.0.0" +git push origin config/v1.0.0 +``` + +3. Merge request and Code + Review + +------------------ + +## 如何使用私有Module + +強制git使用ssh方式而不是https: + +```bash +git config --global --add url."git@git.30cm.net:".insteadOf "https://gitea.30cm.net/" +``` + +設定go env + +```bash +export GOPRIVATE="gitea.30cm.net" +``` + +產生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 yt.com + Hostname gitea.30cm.net + User igs170911 + IdentityFile ~/.ssh/id_rsa +``` + +```bash +$ ssh-add --apple-use-keychain ~/.ssh/id_ed25519 +``` + +在自己repo底下執行: + +```bash +go get gitea.30cm.net/Ark/library-go/config@v1.0.0 +``` diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..fab870e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod utils; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} diff --git a/src/utils/first_utils/first.rs b/src/utils/first_utils/first.rs new file mode 100644 index 0000000..1079c5c --- /dev/null +++ b/src/utils/first_utils/first.rs @@ -0,0 +1,4 @@ +// Just for testing to using library +pub fn public_function() -> String { + "called public func `public_function()`".to_string() +} \ No newline at end of file diff --git a/src/utils/first_utils/mod.rs b/src/utils/first_utils/mod.rs new file mode 100644 index 0000000..844c965 --- /dev/null +++ b/src/utils/first_utils/mod.rs @@ -0,0 +1 @@ +pub mod first; \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..a4e977f --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod first_utils; \ No newline at end of file diff --git a/tests/first_test.rs b/tests/first_test.rs new file mode 100644 index 0000000..9151501 --- /dev/null +++ b/tests/first_test.rs @@ -0,0 +1,10 @@ +#[cfg(test)] +mod tests { + use library_rust::utils; + + #[test] + fn test_first() { + let result: String = utils::first_utils::first::public_function(); + assert!(result.contains("called public func `public_function()`")); + } +} \ No newline at end of file