MCPcopy Index your code
hub / github.com/dtolnay/monostate

github.com/dtolnay/monostate @1.0.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.0.2 ↗ · + Follow
114 symbols 152 edges 30 files 1 documented · 1%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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.

Extension points exported contracts — how you extend this code

MustBe (Interface)
(no doc) [13 implementers]
src/value.rs
StringBuffer (Interface)
(no doc) [10 implementers]
src/string.rs
Sealed (Interface)
(no doc) [13 implementers]
src/value.rs
ConstStr (Interface)
(no doc) [1 implementers]
src/string.rs
Sealed (Interface)
(no doc) [1 implementers]
src/string.rs

Core symbols most depended-on inside this repo

cmp
called by 15
src/ord.rs
as_str
called by 6
src/format.rs
unsupported
called by 2
impl/src/lib.rs
must_be_str
called by 1
impl/src/lib.rs
must_be_byte
called by 1
impl/src/lib.rs
must_be_char
called by 1
impl/src/lib.rs
must_be_int
called by 1
impl/src/lib.rs
must_be_bool
called by 1
impl/src/lib.rs

Shape

Class 42
Method 35
Function 28
Interface 5
Enum 4

Languages

Rust100%

Modules by API surface

src/deserialize.rs30 symbols
tests/test.rs16 symbols
src/lib.rs15 symbols
src/string.rs14 symbols
impl/src/lib.rs9 symbols
src/format.rs4 symbols
tests/ui/incorrect_len.rs2 symbols
src/value.rs2 symbols
src/alphabet.rs2 symbols
tests/ui/ptrsized.rs1 symbols
tests/ui/overflow.rs1 symbols
tests/ui/float.rs1 symbols

For agents

$ claude mcp add monostate \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact