https://github.com/dtolnay/monostate
Type that deserializes only from one specific value
https://github.com/dtolnay/monostate
serde
Last synced: 5 months ago
JSON representation
Type that deserializes only from one specific value
- Host: GitHub
- URL: https://github.com/dtolnay/monostate
- Owner: dtolnay
- License: apache-2.0
- Created: 2022-01-25T09:16:43.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-03-03T04:50:11.000Z (7 months ago)
- Last Synced: 2025-05-09T22:03:16.626Z (5 months ago)
- Topics: serde
- Language: Rust
- Homepage:
- Size: 150 KB
- Stars: 231
- Watchers: 4
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE-APACHE
Awesome Lists containing this project
README
Monostate
=========[
](https://github.com/dtolnay/monostate)
[](https://crates.io/crates/monostate)
[](https://docs.rs/monostate)
[](https://github.com/dtolnay/monostate/actions?query=branch%3Amaster)
This library implements a type macro for a zero-sized type that is Serde
deserializable only from one specific value.```toml
[dependencies]
monostate = "0.1"
```
## Examples
```rust
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.```rust
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.