#typed #numbers #num #serde #serde-derive #serialization

typed-num

Rust中的带serde支持的类型化数字

2个不稳定版本

0.2.0 2023年3月22日
0.1.0 2023年3月22日

1791编码

Download history 81/week @ 2024-03-25 84/week @ 2024-04-01 88/week @ 2024-04-08 196/week @ 2024-04-15 86/week @ 2024-04-22 2/week @ 2024-04-29 82/week @ 2024-05-06 73/week @ 2024-05-13 30/week @ 2024-05-20 122/week @ 2024-05-27 61/week @ 2024-06-03 24/week @ 2024-06-10 35/week @ 2024-06-17 184/week @ 2024-06-24 84/week @ 2024-07-01 96/week @ 2024-07-08

每月401次下载

WTFPL 许可证

6KB
142

typed-num

Rust中的带serde支持的类型化数字。

use typed_num::Num;
use serde_derive::{Serialize, Deserialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Config {
    version: Num<3>,
    hash: String,
}

const OLD_CONFIG: &str = r#"
version = 2
hash = "OoXQqX+ZRNE7VLmkbhGlj2R1B3n3gAJAaGh9kS0mAv8="
"#;

const NEW_CONFIG: &str = r#"
version = 3
hash = "OoXQqX+ZRNE7VLmkbhGlj2R1B3n3gAJAaGh9kS0mAv8="
"#;

let new_config = Config {
    version: Num,
    hash: "OoXQqX+ZRNE7VLmkbhGlj2R1B3n3gAJAaGh9kS0mAv8=".to_string(),
};

toml::from_str::<Config>(OLD_CONFIG).unwrap_err();
assert_eq!(toml::from_str::<Config>(NEW_CONFIG).unwrap(), new_config);
assert_eq!(toml::to_string_pretty(&new_config).unwrap().trim(), NEW_CONFIG.trim());

依赖关系

~110–350KB