#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

#1905 in Encoding

Download history 33/week @ 2024-09-18 39/week @ 2024-09-25 21/week @ 2024-10-02 87/week @ 2024-10-09 43/week @ 2024-10-16 47/week @ 2024-10-23 98/week @ 2024-10-30 81/week @ 2024-11-06 76/week @ 2024-11-13 75/week @ 2024-11-20 79/week @ 2024-11-27 112/week @ 2024-12-04 103/week @ 2024-12-11 105/week @ 2024-12-18 37/week @ 2024-12-25 29/week @ 2025-01-01

301 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–325KB