validated_newtype

Crates.iovalidated_newtype
lib.rsvalidated_newtype
version0.1.1
sourcesrc
created_at2019-12-24 00:14:43.225974
updated_at2019-12-24 00:38:07.042002
descriptionSimple newtypes with checked predicates (primarily for serde)
homepagehttps://github.com/GoldsteinE/validated_newtype
repositoryhttps://github.com/GoldsteinE/validated_newtype
max_upload_size
id191921
size10,317
Max “Goldstein” Siling (GoldsteinE)

documentation

README

validated_newtype

Simple checked newtype generator, primarily for use with serde. Serde support (and dependency) may be disabled with default_features = false. This is #![no_std] library.

Usage:

validated_newtype! {
    /// Documentation comments and attributes are applied
    #[derive(Debug, PartialOrd, Ord, PartialEq, Eq)]
    // base type name => optional visibility + newtype name
    u32 => pub Percent
    // condition to check when creating/deserializing
    if |n: &u32| *n <= 100;
    // error message if condition is not met
    error "percent must be in range 0-100"
}

let x: Percent = serde_json::from_str("42").unwrap();
assert_eq!(*x, 42);
let y: Result<Percent, _> = serde_json::from_str("1337");
assert!(y.is_err());

Instances of generated newtype can be created only via TryFrom or Deserialize, so they always hold valid data.

Dynamic error generation

validated_newtype! {
    #[derive(Debug)]
    u32 => pub Percent
    if |n: &u32| *n <= 100;
    else |n: &u32| format!("number {} is not in range 0-100", n) => String
}

// Deserialize for newtypes uses try_into internally
let x: Result<Percent, _> = 1337.try_into();
assert!(x.is_err());
assert_eq!(x.unwrap_err(), "number 1337 is not in range 0-100");

Manually implement TryFrom

validated_newtype! {
    #[derive(Debug)]
    u32 => pub Percent
}

impl TryFrom<u32> for Percent {
    type Error = &'static str;

    fn try_from(val: u32) -> Result<Self, Self::Error> {
        if val > 100 {
            Err("percent must be in range 0-100")
        } else {
            Ok(Self(val))
        }
    }
}

let x: Percent = serde_json::from_str("42").unwrap();
assert_eq!(*x, 42);
let y: Result<Percent, _> = serde_json::from_str("1337");
assert!(y.is_err());

License: MIT

Commit count: 8

cargo fmt