12 releases

0.1.11 Jan 2, 2024
0.1.10 Nov 14, 2023
0.1.9 Jul 21, 2023
0.1.6 Mar 30, 2023
0.1.0 Jan 25, 2022

#12 in #success

Download history 13939/week @ 2023-11-24 15338/week @ 2023-12-01 13605/week @ 2023-12-08 14977/week @ 2023-12-15 10600/week @ 2023-12-22 11849/week @ 2023-12-29 17044/week @ 2024-01-05 20652/week @ 2024-01-12 25262/week @ 2024-01-19 24035/week @ 2024-01-26 23666/week @ 2024-02-02 25813/week @ 2024-02-09 28261/week @ 2024-02-16 32460/week @ 2024-02-23 34128/week @ 2024-03-01 14499/week @ 2024-03-08

114,450 downloads per month
Used in 67 crates (via monostate)

MIT/Apache

9KB
146 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 indentification 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

~345–800KB
~19K SLoC