22 releases (3 stable)

1.0.2 Oct 14, 2025
1.0.0 Sep 29, 2025
0.1.18 Sep 29, 2025
0.1.14 Mar 3, 2025
0.1.0 Jan 25, 2022

#10 in #untagged

Download history 221216/week @ 2025-10-14 252682/week @ 2025-10-21 295226/week @ 2025-10-28 327901/week @ 2025-11-04 316411/week @ 2025-11-11 298096/week @ 2025-11-18 214339/week @ 2025-11-25 229279/week @ 2025-12-02 206674/week @ 2025-12-09 207542/week @ 2025-12-16 122145/week @ 2025-12-23 129815/week @ 2025-12-30 200790/week @ 2026-01-06 189523/week @ 2026-01-13 209388/week @ 2026-01-20 221139/week @ 2026-01-27

851,521 downloads per month
Used in 489 crates (via monostate)

MIT/Apache

10KB
153 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 = "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

~140–530KB
~13K SLoC