init common library

This commit is contained in:
daniel.w 2024-06-16 12:48:34 +08:00
parent 41089b821e
commit a756bd57bf
8 changed files with 147 additions and 4 deletions

129
readme.md
View File

@ -1,4 +1,133 @@
Rust Library 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
```

1
src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod utils;

View File

@ -1,3 +0,0 @@
fn main() {
println!("Hello, world!");
}

View File

@ -0,0 +1,4 @@
// Just for testing to using library
pub fn public_function() -> String {
"called public func `public_function()`".to_string()
}

View File

@ -0,0 +1 @@
pub mod first;

1
src/utils/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod first_utils;

10
tests/first_test.rs Normal file
View File

@ -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()`"));
}
}