rust第六篇,用rust写的通用工具
# just
just
为您提供一种保存和运行项目特有命令的便捷方式。
命令,在此也称为配方,存储在一个名为 justfile
的文件中,其语法受 make
启发:
然后你可以用 just RECIPE
运行它们
$ just test-all
cc *.c -o main
./test --all
Yay, all your tests passed!
1
2
3
4
2
3
4
just
有很多很棒的特性,而且相比 make
有很多改进:
just
是一个命令运行器,而不是一个构建系统,所以它避免了许多make
的复杂性和特异性 (opens new window)。不需要.PHONY
配方!- 支持 Linux、MacOS 和 Windows,而且无需额外的依赖。(尽管如果你的系统没有
sh
,你需要 选择一个不同的 Shell (opens new window))。 - 错误具体且富有参考价值,语法错误将会与产生它们的上下文一起被报告。
- 配方可以接受 命令行参数 (opens new window)。
- 错误会尽可能被静态地解决。未知的配方和循环依赖关系会在运行之前被报告。
just
可以 加载.env
文件 (opens new window),简化环境变量注入。- 配方可以在 命令行中列出 (opens new window)。
- 命令行自动补全脚本 支持大多数流行的 Shell (opens new window)。
- 配方可以用 任意语言 (opens new window) 编写,如 Python 或 NodeJS。
just
可以从任何子目录中调用,而不仅仅是包含justfile
的目录。
https://github.com/casey/just
# 实现命令行
https://suibianxiedianer.github.io/rust-cli-book-zh_CN/tutorial/cli-args_zh.html
# Typeshare
支持在其他语言中无缝接入Rust中的类型, 支持语言包括typescript、kotlin、swift、scala、go
在Cargo.toml中加入依赖
typeshare = "1.0.0"
1
rust中的类型
// Rust type definitions
#[typeshare]
struct MyStruct {
my_name: String,
my_age: u32,
}
#[typeshare]
#[serde(tag = "type", content = "content")]
enum MyEnum {
MyVariant(bool),
MyOtherVariant,
MyNumber(u32),
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
生成的typescript类型
// Generated Typescript definitions
export interface MyStruct {
my_name: string;
my_age: number;
}
export type MyEnum =
| { type: "MyVariant", content: boolean }
| { type: "MyOtherVariant", content: undefined }
| { type: "MyNumber", content: number };
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# webAssembly
# exitsm
webAssembly构建工具
https://github.com/extism/extism
# firecracker
https://github.com/firecracker-microvm/firecracker
# trustfall
查询
https://github.com/obi1kenobi/trustfall
# delta
diff高亮
https://github.com/dandavison/delta
# 模版引擎
# lol-html
https://github.com/cloudflare/lol-html
使用
use lol_html::{element, HtmlRewriter, Settings};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut output = vec![];
let mut rewriter = HtmlRewriter::new(
Settings {
element_content_handlers: vec![
element!("a[href]", |el| {
let href = el
.get_attribute("href")
.expect("href was required")
.replace("http:", "https:");
el.set_attribute("href", &href)?;
Ok(())
})
],
..Settings::default()
},
|c: &[u8]| output.extend_from_slice(c)
);
rewriter.write(b"<div><a href=")?;
rewriter.write(b"http://example.com>")?;
rewriter.write(b"</a></div>")?;
rewriter.end()?;
assert_eq!(
String::from_utf8(output)?,
r#"<div><a href="https://example.com"></a></div>"#
);
Ok(())
}
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
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
# maud
https://maud.lambda.xyz/getting-started.html
← rust(五) Vue.js前端框架(一) →