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
//! Internal macros for unit enums.
//!
//! Every wire unit enum carries a `#[serde(other)] Unknown` fallback (serde rule 5), a
//! `pub const ALL` without it, `as_str`, `from_wire` and `Display`, and is `#[non_exhaustive]`
//! (the macro adds it), so a new value is not a breaking change. The derive list is written
//! at each call site so the registry test can see it in the source.
/// Defines a wire unit enum whose JSON form is a string.
///
/// The variants take the discriminants 0, 1, 2, … in order; `Unknown` always has
/// [`UNKNOWN_DISCRIMINANT`](crate::registry::UNKNOWN_DISCRIMINANT), so adding a variant never
/// moves it.
macro_rules! unit_enum {
(
$(#[$meta:meta])*
pub enum $name:ident {
$( $(#[$vmeta:meta])* $variant:ident = $wire:literal ),+ $(,)?
}
) => {
$(#[$meta])*
#[non_exhaustive]
pub enum $name {
$( $(#[$vmeta])* #[serde(rename = $wire)] $variant, )+
/// A value this build does not know, sent by a newer peer. The server never sends it.
/// Its discriminant is always
/// [`UNKNOWN_DISCRIMINANT`](crate::registry::UNKNOWN_DISCRIMINANT).
#[serde(other, rename = "unknown")]
#[schemars(skip)]
Unknown = $crate::registry::UNKNOWN_DISCRIMINANT,
}
impl $name {
/// Every known value in wire order, without `Unknown`.
pub const ALL: &'static [$name] = &[ $( $name::$variant ),+ ];
/// The wire string. `Unknown` gives `"unknown"`.
pub const fn as_str(self) -> &'static str {
match self {
$( $name::$variant => $wire, )+
$name::Unknown => "unknown",
}
}
/// Parses a wire string. Strings this build does not know give `None`.
pub fn from_wire(s: &str) -> Option<Self> {
match s {
$( $wire => Some($name::$variant), )+
_ => None,
}
}
/// Whether this is the forward-compat `Unknown` value.
pub const fn is_unknown(self) -> bool {
matches!(self, $name::Unknown)
}
}
impl $crate::registry::UnitEnum for $name {
fn all() -> &'static [Self] {
Self::ALL
}
fn wire_str(self) -> &'static str {
self.as_str()
}
fn is_unknown_value(self) -> bool {
self.is_unknown()
}
}
impl ::core::fmt::Display for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_str(self.as_str())
}
}
};
}
/// Defines a wire unit enum that is also stored as a frozen `smallint` code.
macro_rules! coded_enum {
(
$(#[$meta:meta])*
pub enum $name:ident {
$( $(#[$vmeta:meta])* $variant:ident = $wire:literal => $code:literal ),+ $(,)?
}
) => {
unit_enum! {
$(#[$meta])*
pub enum $name {
$( $(#[$vmeta])* $variant = $wire ),+
}
}
impl $name {
/// The frozen storage code. `Unknown` gives `0`, which no value uses.
pub const fn as_i16(self) -> i16 {
match self {
$( $name::$variant => $code, )+
$name::Unknown => 0,
}
}
/// Looks up a storage code. Codes this build does not know give `None`.
pub const fn from_i16(code: i16) -> Option<Self> {
match code {
$( $code => Some($name::$variant), )+
_ => None,
}
}
}
};
}