Pre commit
使用 pre-commit ,在根目录编写
.pre-commit-config.yaml 后,运行 pre-commit install,注册 pre-commit
hook。
使用 cargo-deny
来检查依赖授权或者可疑来源等问题。
通过 "///" 注释可以用 markdown 格式撰写,之后通过 "cargo doc"
编译,markdown 里的代码就会被编译成 doctest,然后 "cargo test"
测试。
在 crate 文档主页上的内容,可以在 lib.rs 或者 main.rs 的开头用
“//!”,比如:
如果你想强迫自己要撰写每个公共接口的文档,保持系统有良好的文档覆盖,那么可以使用
#![deny(missing_docs)] ,写在 lib.rs 的开头。
CI CD
使用 github workflow 来进行持续集成,比如 .github/workflows/build.yml
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| name: build
on: push: branches: - master pull_request: branches: - master
jobs: build-rust: strategy: matrix: platform: [ubuntu-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: - uses: actions/checkout@v2 - name: Cache cargo registry uses: actions/cache@v1 with: path: ~/.cargo/registry key: ${{ runner.os }}-cargo-registry - name: Cache cargo index uses: actions/cache@v1 with: path: ~/.cargo/git key: ${{ runner.os }}-cargo-index - name: Cache cargo build uses: actions/cache@v1 with: path: target key: ${{ runner.os }}-cargo-build-target - name: Install stable uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true - name: Check code format run: cargo fmt -- --check - name: Check the package for errors run: cargo check --all - name: Lint rust sources run: cargo clippy --all-targets --all-features --tests --benches -- -D warnings - name: Run tests run: cargo test --all-features -- --test-threads=1 --nocapture - name: Generate docs run: cargo doc --all-features --no-deps
|
编译处理
存储字典映射的 bincode 到可执行文件中,运行时直接读取使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| use std::io::{self, BufRead}; use std::{env, fs::File, path::Path};
fn main() { println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=src/t2s.txt");
let out_dir = env::var_os("OUT_DIR").unwrap(); let out_file = Path::new(&out_dir).join("map.bin"); let f = File::create(&out_file).unwrap(); let v = get_kv("src/t2s.txt"); bincode::serialize_into(f, &v).unwrap(); }
fn s2c(s: &str) -> char { let mut chars = s.chars(); let c = chars.next().unwrap(); assert!(chars.next().is_none()); assert!(c.len_utf8() == 3); c }
fn get_kv(filename: &str) -> Vec<(char, char)> { let f = File::open(filename).unwrap(); let lines = io::BufReader::new(f).lines(); let mut v = Vec::with_capacity(4096); for line in lines { let line = line.unwrap(); let kv: Vec<_> = line.split(' ').collect(); v.push((s2c(kv[0]), s2c(kv[1]))); }
v }
|
保存 bincode ,并读取使用:
| static MAP_DATA: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/map.bin"));
lazy_static! { static ref MAP: HashMap<char, char> = { let data: Vec<(char, char)> = bincode::deserialize(MAP_DATA).unwrap(); data.into_iter().collect() }; ... }
|
监控与日志
在一个分布式系统下,需要把收集到的日志集中起来,进行过滤和查询。另外,收集系统的行为数据和性能指标,监控系统运行时的状态。