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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//! Impls for ints
use crate::DeserializeErrors;
use crate::{Key, Value};
use nonempty::NonEmpty;
use pastey::paste;
use std::num::{
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroU8,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
};
use miette::{Diagnostic, SourceSpan};
use thiserror::Error;
use crate::util::value_type_description;
use crate::{DeserializeValue, Error};
/// Generates `DeserializeValue` for all int types
macro_rules! impl_deserialize_for_ints {
($($num:ty, $kind:ident,)*) => {
$(
paste! {
#[doc = concat!("Failed to deserialize [`", stringify!($num), "`](core::primitive::", stringify!($num),")")]
#[derive(Diagnostic, Error, Debug, Clone, Eq, PartialEq, Hash)]
#[error("Type mismatch")]
pub struct [<Deserialize $num:upper_camel Error>] {
/// Details on why deserialization failed
pub details: String,
/// Location of the error in the source file
#[label("{details}")]
pub span: SourceSpan,
}
impl $crate::Error for [<Deserialize $num:upper_camel Error>] {
fn expected_type() -> std::borrow::Cow<'static, str> {
match stringify!($kind) {
"i" => "int".into(),
"u" => "positive int".into(),
_ => unreachable!(),
}
}
}
#[doc = concat!("Failed to deserialize [`", stringify!([< NonZero $num:upper_camel >]),"`]")]
#[derive(Diagnostic, Error, Debug, Clone, Eq, PartialEq, Hash)]
#[error("Type mismatch")]
pub struct [<Deserialize NonZero $num:upper_camel Error>] {
/// Describes the type that was expected and the type that was found.
pub details: String,
/// Span of the value in the source.
#[label("{details}")]
pub span: SourceSpan,
}
impl $crate::Error for [<Deserialize NonZero $num:upper_camel Error>] {
fn expected_type() -> std::borrow::Cow<'static, str> {
match stringify!($kind) {
"i" => "non-zero int".into(),
"u" => "non-zero positive int".into(),
_ => unreachable!(),
}
}
}
impl DeserializeValue<'_> for $num {
type Error = [<Deserialize $num:upper_camel Error>];
fn deserialize_value(_key: Key, value: Value) -> Result<Self, DeserializeErrors<Self, NonEmpty<Self::Error>>> {
if let Some(n) = value.as_ref().as_integer() {
let n = n.as_str().parse::<i64>().unwrap();
#[allow(irrefutable_let_patterns, reason = "macro")]
if let Ok(n_converted) = <$num>::try_from(n) {
Ok(n_converted)
} else {
let min = <$num>::MIN;
let max = <$num>::MAX;
let details = if stringify!($kind) == "u" && n.is_negative() {
format!(
"expected a positive int from `{min}` to `{max}`, but this is negative"
)
} else {
format!(
"expected an int from `{min}` to `{max}`, but this int is out of bounds"
)
};
Err(DeserializeErrors::err(
[<Deserialize $num:upper_camel Error>] {
details,
span: value.span().into(),
}
))
}
} else {
// If the value is not an int, create a type mismatch error.
let err = [<Deserialize $num:upper_camel Error>] {
details: [<Deserialize $num:upper_camel Error>]::details(
value_type_description(&value)
),
span: value.span().into(),
};
Err(DeserializeErrors::err(err))
}
}
}
impl DeserializeValue<'_> for [<NonZero $num:camel>] {
type Error = [<Deserialize NonZero $num:upper_camel Error>];
fn deserialize_value(_key: Key, value: Value) -> Result<Self, DeserializeErrors<Self, NonEmpty<Self::Error>>> {
if let Some(n) = value.as_ref().as_integer() {
let n = n.as_str().parse::<i64>().unwrap();
if n == 0 {
// If the int is zero, return a specific non-zero error.
return Err(DeserializeErrors::err(
[<Deserialize NonZero $num:upper_camel Error>] {
details: format!("expected a non-zero int, but found 0"),
span: value.span().into(),
}
));
}
#[allow(irrefutable_let_patterns, reason = "macro")]
if let Ok(n_converted) = <$num>::try_from(n) {
Ok(
// SAFETY: We just checked that `n == 0` and returned earlier
// from the function, so n_converted is guaranteed non-zero.
unsafe {
<[<NonZero $num:camel>]>::new_unchecked(n_converted)
}
)
} else {
let min = <$num>::MIN;
let max = <$num>::MAX;
// Determine the specific error message based on the int kind and value.
let details = if stringify!($kind) == "u" && n.is_negative() {
format!(
"expected a non-zero positive int from `{min}` to `{max}`, but this is negative"
)
} else {
format!(
"expected a non-zero int from `{min}` to `{max}`, but this int is out of bounds"
)
};
Err(DeserializeErrors::err(
[<Deserialize NonZero $num:upper_camel Error>] {
details,
span: value.span().into(),
}
))
}
} else {
// If the value is not an int, create a type mismatch error.
let err = [<Deserialize NonZero $num:upper_camel Error>] {
details: [<Deserialize $num:upper_camel Error>]::details(
value_type_description(&value)
),
span: value.span().into(),
};
Err(DeserializeErrors::err(err))
}
}
}
}
)*
};
}
impl_deserialize_for_ints! {
i8, i,
i16, i,
i32, i,
i64, i,
i128, i,
isize, i,
u8, u,
u16, u,
u32, u,
u64, u,
u128, u,
usize, u,
}
/// Implement `DeserializeValue` for these atomic types
macro_rules! impl_deserialize_for_atomic {
($($n:literal $sn:literal)*) => {
pastey::paste! {
$(
#[cfg(target_has_atomic = $sn)]
impl DeserializeValue<'_> for std::sync::atomic::[<AtomicU $n:upper_camel>] {
type Error = [<DeserializeU $n:upper_camel Error>];
fn deserialize_value(
key: Key,
value: Value
) -> Result<Self, DeserializeErrors<Self, NonEmpty<Self::Error>>> {
<[< u $n >] as DeserializeValue>::deserialize_value(key, value)
.map(Into::into)
.map_err(|err| err.map(Into::into))
}
}
#[cfg(target_has_atomic = $sn)]
impl DeserializeValue<'_> for std::sync::atomic::[<AtomicI $n:upper_camel>] {
type Error = [<DeserializeI $n:upper_camel Error>];
fn deserialize_value(
key: Key,
value: Value
) -> Result<Self, DeserializeErrors<Self, NonEmpty<Self::Error>>> {
<[< i $n >] as DeserializeValue>::deserialize_value(key, value)
.map(Into::into)
.map_err(|err| err.map(Into::into))
}
}
)*
}
};
}
// NOTE: `target_has_atomic` does not accept `stringify!()` so we have to do this
impl_deserialize_for_atomic!(8 "8" 16 "16" 32 "32" 64 "64");
#[cfg(target_has_atomic = "ptr")]
impl DeserializeValue<'_> for std::sync::atomic::AtomicUsize {
type Error = DeserializeUsizeError;
fn deserialize_value(
key: Key,
value: Value,
) -> Result<Self, DeserializeErrors<Self, NonEmpty<Self::Error>>> {
<usize as DeserializeValue>::deserialize_value(key, value)
.map(Into::into)
.map_err(|err| err.map(Into::into))
}
}
#[cfg(target_has_atomic = "ptr")]
impl DeserializeValue<'_> for std::sync::atomic::AtomicIsize {
type Error = DeserializeIsizeError;
fn deserialize_value(
key: Key,
value: Value,
) -> Result<Self, DeserializeErrors<Self, NonEmpty<Self::Error>>> {
<isize as DeserializeValue>::deserialize_value(key, value)
.map(Into::into)
.map_err(|err| err.map(Into::into))
}
}