1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! [![github]](https://github.com/dtolnay/monostate) [![crates-io]](https://crates.io/crates/monostate) [![docs-rs]](https://docs.rs/monostate)
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
//! <br>
//!
//! This library implements a type macro for a zero-sized type that is Serde
//! deserializable only from one specific value.
//!
//! # 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,
//!     },
//! }
//! ```

#![no_std]
#![allow(non_camel_case_types, non_upper_case_globals)]
#![allow(
    clippy::borrow_as_ptr,
    clippy::builtin_type_shadow,
    clippy::cast_lossless,
    clippy::cast_sign_loss,
    clippy::expl_impl_clone_on_copy,
    clippy::missing_safety_doc,
    clippy::module_name_repetitions,
    clippy::ptr_as_ptr
)]

#[doc(hidden)]
pub mod alphabet;
mod debug;
mod default;
mod deserialize;
mod eq;
mod format;
mod hash;
mod ord;
mod partial_eq;
mod partial_ord;
mod serialize;
mod string;

pub use monostate_impl::MustBe;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeChar<const char: char>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBePosInt<const u128: u128>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeNegInt<const i128: i128>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeU8<const u8: u8>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeU16<const u16: u16>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeU32<const u32: u32>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeU64<const u64: u64>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeU128<const u128: u128>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeI8<const i8: i8>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeI16<const i16: i16>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeI32<const i32: i32>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeI64<const i64: i64>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeI128<const i128: i128>;

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct MustBeBool<const bool: bool>;

mod void {
    use core::marker::PhantomData;

    enum Void {}

    impl Copy for Void {}

    impl Clone for Void {
        fn clone(&self) -> Self {
            match *self {}
        }
    }

    pub struct MustBeStr<T>(PhantomData<T>, Void);

    impl<T> Copy for MustBeStr<T> {}

    impl<T> Clone for MustBeStr<T> {
        fn clone(&self) -> Self {
            *self
        }
    }
}

mod value {
    pub use super::MustBeStr::MustBeStr;
}

// Equivalent to `pub struct MustBeStr<const str: &'static str>;` but using
// the type encoding described in impl/src/lib.rs to avoid depending on
// #![feature(adt_const_params)] for now.
#[doc(hidden)]
pub enum MustBeStr<str> {
    __Phantom(void::MustBeStr<str>),
    MustBeStr,
}

impl<str> Copy for MustBeStr<str> {}

impl<str> Clone for MustBeStr<str> {
    fn clone(&self) -> Self {
        *self
    }
}

#[doc(hidden)]
pub use self::value::*;