#typed #numbers #num #serde #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

#1791 in Encoding

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 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

~110–350KB