Crates.io | monostate |
lib.rs | monostate |
version | 0.1.13 |
source | src |
created_at | 2022-01-25 23:09:52.892896 |
updated_at | 2024-05-06 23:25:17.572141 |
description | Type that deserializes only from one specific value |
homepage | |
repository | https://github.com/dtolnay/monostate |
max_upload_size | |
id | 521134 |
size | 68,721 |
This library implements a type macro for a zero-sized type that is Serde deserializable only from one specific value.
[dependencies]
monostate = "0.1"
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,
},
}