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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::{json, warning};
/// A type used to deserialize a JSON string value into a structured Rust enum.
///
/// The deserialized value may not map to a `Known` variant in the enum and therefore be `Unknown`.
/// The caller can then decide what to do with the `Unknown` variant.
#[derive(Clone, Debug)]
pub(crate) enum Enum<T> {
Known(T),
Unknown(String),
}
/// Create an `Enum<T>` from a `&str`.
///
/// This is used in conjunction with `FromJson`
pub(crate) trait IntoEnum: Sized {
fn enum_from_str(s: &str) -> Enum<Self>;
}
/// A wrapper around `WarningKind` that associates the `enum` name with the warning.
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Warning {
/// The type name of the enum that raised this warning.
type_name: &'static str,
/// The kind of warning raised.
kind: WarningKind,
}
#[cfg(test)]
impl Warning {
pub(crate) fn type_name(&self) -> &'static str {
self.type_name
}
pub(crate) fn kind(&self) -> &WarningKind {
&self.kind
}
}
impl std::fmt::Display for Warning {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.kind {
WarningKind::ContainsEscapeCodes => write!(
f,
"The {} value contains escape codes but it does not need them.",
self.type_name
),
WarningKind::Decode(warning) => std::fmt::Display::fmt(&warning, f),
WarningKind::PreferUpperCase => {
write!(f, "The `{}` should be uppercase.", self.type_name)
}
WarningKind::InvalidVariant => {
write!(f, "The value is not a valid `{}`.", self.type_name)
}
WarningKind::InvalidType => {
write!(f, "A {} value should be a string.", self.type_name)
}
}
}
}
impl crate::Warning for Warning {
fn id(&self) -> warning::Id {
match &self.kind {
WarningKind::ContainsEscapeCodes => warning::Id::from_static("contains_escape_codes"),
WarningKind::Decode(warning) => warning.id(),
WarningKind::PreferUpperCase => warning::Id::from_static("prefer_upper_case"),
WarningKind::InvalidVariant => warning::Id::from_static("invalid_variant"),
WarningKind::InvalidType => warning::Id::from_static("invalid_type"),
}
}
}
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum WarningKind {
/// The enum variant doesn't need to have escape-codes.
ContainsEscapeCodes,
/// The field at the path could not be decoded.
Decode(json::decode::Warning),
/// Each enum should be uppercase.
PreferUpperCase,
/// The value is not a valid.
InvalidVariant,
/// The JSON value given is not a string.
InvalidType,
}
impl WarningKind {
/// Convert a `WarningKind` into a `Warning` by supplying a type name.
pub(crate) fn into_warning(self, type_name: &'static str) -> Warning {
Warning {
type_name,
kind: self,
}
}
}
/// This macro defines the `FromJson` impl for the given enum.
///
/// Note: The `IntoEnum` should already be defined for the enum.
#[macro_export]
macro_rules! define_enum_from_json {
($kind:ident, display_name: $name:literal, warning_id: $warning:literal) => {
impl $crate::json::FromJson<'_> for $kind {
type Warning = $crate::enumeration::Warning;
fn from_json(elem: &$crate::json::Element<'_>) -> $crate::Verdict<Self, Self::Warning> {
use $crate::warning::IntoCaveat as _;
let value = Enum::<$kind>::from_json(elem)?;
let (value, warnings) = value.into_parts();
let value = match value {
$crate::Enum::Known(v) => v,
$crate::Enum::Unknown(_) => {
return warnings.bail(
$crate::enumeration::WarningKind::InvalidVariant
.into_warning(stringify!($kind)),
elem,
);
}
};
Ok(value.into_caveat(warnings))
}
}
impl $crate::json::FromJson<'_> for $crate::Enum<$kind> {
type Warning = $crate::enumeration::Warning;
fn from_json(elem: &$crate::json::Element<'_>) -> $crate::Verdict<Self, Self::Warning> {
use $crate::warning::IntoCaveat as _;
let mut warnings = $crate::warning::Set::new();
let value = elem.as_value();
let Some(s) = value.to_raw_str() else {
return warnings.bail(
$crate::enumeration::WarningKind::InvalidType
.into_warning(stringify!($kind)),
elem,
);
};
// We can't use `fn gather_warnings_into` here as we want to convert the warnings
// using the type name from the macro instead of the `impl From<WarningA> for WarningB`.
// This requires a manual conversion from `decode::Warning` to `Warning`.
let (s, inner_warnings) = s.has_escapes(elem).into_parts();
let inner_warnings =
inner_warnings
.into_inner()
.into_iter()
.map(|(elem_id, group)| {
(
elem_id,
group.map(|w| {
$crate::enumeration::WarningKind::Decode(w)
.into_warning(stringify!($kind))
}),
)
});
warnings.extend(inner_warnings);
let s = match s {
$crate::json::decode::PendingStr::NoEscapes(s) => s,
$crate::json::decode::PendingStr::HasEscapes(_) => {
return warnings.bail(
$crate::enumeration::WarningKind::ContainsEscapeCodes
.into_warning(stringify!($kind)),
elem,
);
}
};
if !s.chars().all(|c| c.is_uppercase() || c == '_') {
warnings.insert(
$crate::enumeration::WarningKind::PreferUpperCase
.into_warning(stringify!($kind)),
elem,
);
}
let value = $kind::enum_from_str(s);
Ok(value.into_caveat(warnings))
}
}
};
}