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

typed-num

Typed number in Rust with serde support

2 unstable releases

0.2.0 Mar 22, 2023
0.1.0 Mar 22, 2023

#1859 in Encoding

Download history 52/week @ 2024-07-19 75/week @ 2024-07-26 35/week @ 2024-08-02 55/week @ 2024-08-09 52/week @ 2024-08-16 18/week @ 2024-08-23 37/week @ 2024-08-30 11/week @ 2024-09-06 2/week @ 2024-09-13 59/week @ 2024-09-20 17/week @ 2024-09-27 61/week @ 2024-10-04 70/week @ 2024-10-11 26/week @ 2024-10-18 77/week @ 2024-10-25 85/week @ 2024-11-01

279 downloads per month

WTFPL license

6KB
142 lines

typed-num

Typed number in Rust with serde support.

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());

Dependencies

~100–335KB