如果你没有听(用)过 Json Schema,请允许我首先简单介绍一下。JSON Schema 是用于验证 JSON 数据结构的工具,如果你厌恶对 Json 数据各种 if else 的判断和校验,那该工具非常适合。它的官网:JSON Schema | The home of JSON Schema,先看一个简单的例子,假设我们有下面的 Schema:
{
"type": "object",
"properties": {
"first_name": { "type": "string" },
"last_name": { "type": "string" },
"birthday": { "type": "string", "format": "date" },
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" },
"country": { "type" : "string" }
}
}
}
}
这个 Schema 一共定义了四个字段,每个字段的类型都做了规定,address 本身也是一个 Json Object。此时,有效的数据是:
{
"first_name": "George",
"last_name": "Washington",
"birthday": "1732-02-22",
"address": {
"street_address": "3200 Mount Vernon Memorial Highway",
"city": "Mount Vernon",
"state": "Virginia",
"country": "United States"
}
}
而下面这样的无效数据则会被 Json Schema 验证并报错:
{
"name": "George Washington",
"birthday": "February 22, 1732",
"address": "Mount Vernon, Virginia, United States"
}
Json Schema 本身是语言无关的,这里已经有很多实现了:Implementations | JSON Schema,Rust 版本的使用与其他语言类似:
use jsonschema::{Draft, JSONSchema};
use serde_json::json;
fn main() {
let schema = json!({"maxLength": 5});
let instance = json!("foo");
# 编译 Schema
let compiled = JSONSchema::compile(&schema)
.expect("A valid schema");
# 验证实例
let result = compiled.validate(&instance);
if let Err(errors) = result {
for error in errors {
println!("Validation error: {}", error);
println!(
"Instance path: {}", error.instance_path
);
}
}
}
这个工具唯一有个麻烦的地方就是编写 Schema 比较费劲,可以理解为设计类。不过好在写好之后就省事了。
GitHub:Stranger6667/jsonschema-rs: JSON Schema validation library
用法如下:
pub struct AThing(u8, &'static str);
const A_VEC_CONST: ManuallyDrop<Vec<AThing>> = vec_const!(AThing, AThing(5, "wow"), AThing(2, "cool"));
fn main()
{
assert_eq!(*TEST, vec!(AThing(5, "wow"), AThing(2, "cool")));
}
GitHub:Eolu/vec-const: Rust crate to allow declaration of vecs as consts
包括:构建、发布、文档等功能。Cargo 功能已经很强大了,为啥还要做这个东西呢?因为有时我们需要做更多的事情,比如复制一些文件、发布到 ftp 或输入长命令。这些重复性任务必须自动化(也称为 “工作流自动化”)。
$ cargo install cargo-auto
$ cargo auto new
$ cargo auto build
$ cargo auto release
$ cargo auto docs
GitHub:LucianoBestia/cargo-auto: cargo auto - automation tasks written in Rust language
RillRate 是完全使用 Rust 和 Yew 框架制作的机器人、微服务和物联网的快速 UI。全栈 Rust 是真实存在的!
本周增加的新功能:
GitHub:rillrate/rillrate: Dynamic UI for bots, microservices, and IoT.
具体使用方式如下:
for_ch! {
for x in 0..10; // forall x in 0..10,
// you can add a label before `for`
for y in x..10, for _ in 0..5; // forall y in x..x+5,
// zipping
if let Some(z) = foo(x, y).await?; // exists z. Some(z) = foo(x, y).await?
// if let guard
if x - y < z; // satisfies x - y < z
// guard
println!("x = {}, y = {}, z = {}", x, y, z);
}
# 结果为
for x in 0..10 {
for y in (x..10).zip(0..5) {
if let Some(z) = foo(x, y).await? {
if x - y < z {
println!("x = {}, y = {}, z = {}", x, y, z);
}
}
}
}
不过,谁会这么写代码呢,完全没有可读性嘛,看来只能用来装逼了。
GitHub:TOETOE55/for_ch: A macro to write nested for loop
该库提供了通过 Any
动态地对类型进行专门化的函数。基本思想是,如果 T 和 U 实际上是同一类型,则允许将任何 T “强制转换” 为任何其他 U。
use identity_cast::IdentityCast;
fn print_i32_specially<T: 'static>(v: T) {
match v.into_same::<i32>() {
Ok(v) => {
println!("This is a `i32` with value {}", v);
}
Err(_) => {
println!("This is some `T`");
}
}
}
print_i32_specially("123");
# This is some `T`
print_i32_specially(123);
# This is a `i32` with value 123
GitHub:Kimundi/identity_cast
From 日报小组 长琴
社区学习交流平台订阅: