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
//! The `open_enum!` macro behind every API enum.
/// Defines an open string enum: the listed values as variants plus `Other(String)`, so values
/// added to the API later decode without error. Serialised as the plain string.
macro_rules! open_enum {
(
$(#[$meta:meta])*
$name:ident {
$(
$(#[$vmeta:meta])*
$variant:ident => $value:literal
),* $(,)?
}
) => {
$(#[$meta])*
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum $name {
$(
#[doc = concat!("`\"", $value, "\"`")]
$(#[$vmeta])*
$variant,
)*
/// A value this SDK version does not know (passed through unchanged).
Other(String),
}
impl $name {
/// The wire value.
pub fn as_str(&self) -> &str {
match self {
$(Self::$variant => $value,)*
Self::Other(s) => s.as_str(),
}
}
/// Whether this SDK version knows the value (it is not `Other`).
pub fn is_known(&self) -> bool {
!matches!(self, Self::Other(_))
}
}
impl From<&str> for $name {
fn from(s: &str) -> Self {
match s {
$($value => Self::$variant,)*
other => Self::Other(other.to_string()),
}
}
}
impl From<String> for $name {
fn from(s: String) -> Self {
Self::from(s.as_str())
}
}
impl std::str::FromStr for $name {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(s))
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl serde::Serialize for $name {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(self.as_str())
}
}
impl<'de> serde::Deserialize<'de> for $name {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let s = <std::borrow::Cow<'de, str> as serde::Deserialize>::deserialize(d)?;
Ok(Self::from(s.as_ref()))
}
}
};
}