14 releases

0.1.13 May 6, 2024
0.1.11 Jan 2, 2024
0.1.10 Nov 14, 2023
0.1.9 Jul 21, 2023
0.1.0 Jan 25, 2022

#10 in #success

Download history 33718/week @ 2024-08-27 35510/week @ 2024-09-03 35604/week @ 2024-09-10 35460/week @ 2024-09-17 40079/week @ 2024-09-24 54052/week @ 2024-10-01 51780/week @ 2024-10-08 57642/week @ 2024-10-15 62018/week @ 2024-10-22 56600/week @ 2024-10-29 54175/week @ 2024-11-05 54539/week @ 2024-11-12 60619/week @ 2024-11-19 59157/week @ 2024-11-26 66687/week @ 2024-12-03 54248/week @ 2024-12-10

251,097 downloads per month
Used in 131 crates (via monostate)

MIT/Apache

9KB
148 lines

Monostate

github crates.io docs.rs build status

This library implements a type macro for a zero-sized type that is Serde deserializable only from one specific value.

[dependencies]
monostate = "0.1"

Examples

use monostate::MustBe;
use serde::Deserialize;

#[derive(Deserialize)]
struct Example {
    kind: MustBe!("success"),
    code: MustBe!(200),
}

The above struct would deserialize from {"kind":"success", "code":200} in JSON, but would fail the deserialization if "kind" or "code" were any other value.

This can sometimes be helpful in processing untagged enums in which the variant identification is more convoluted than what is handled by Serde's externally tagged and internally tagged representations, for example because the variant tag has an inconsistent type or key.

use monostate::MustBe;
use serde::Deserialize;

#[derive(Deserialize)]
#[serde(untagged)]
pub enum ApiResponse {
    Success {
        success: MustBe!(true),
    },
    Error {
        kind: MustBe!("error"),
        message: String,
    },
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~220–660KB
~16K SLoC