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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
//! Provides helpers for deserializing [`Value`]/[`ValueInner`] into Rust types
use crate::{
span::Spanned,
value::{self, Table, Value, ValueInner},
DeserError, Deserialize, Error, ErrorKind, Span,
};
use std::{fmt::Display, str::FromStr};
/// Helper for construction an [`ErrorKind::Wanted`]
#[inline]
pub fn expected(expected: &'static str, found: ValueInner<'_>, span: Span) -> Error {
Error {
kind: ErrorKind::Wanted {
expected,
found: found.type_str(),
},
span,
line_info: None,
}
}
/// Attempts to acquire a [`ValueInner::String`] and parse it, returning an error
/// if the value is not a string, or the parse implementation fails
#[inline]
pub fn parse<T, E>(value: &mut Value<'_>) -> Result<T, Error>
where
T: FromStr<Err = E>,
E: Display,
{
let s = value.take_string(None)?;
match s.parse() {
Ok(v) => Ok(v),
Err(err) => Err(Error {
kind: ErrorKind::Custom(format!("failed to parse string: {err}").into()),
span: value.span,
line_info: None,
}),
}
}
/// A helper for dealing with [`ValueInner::Table`]
pub struct TableHelper<'de> {
/// The table the helper is operating upon
pub table: Table<'de>,
/// The errors accumulated while deserializing
pub errors: Vec<Error>,
/// The list of keys that have been requested by the user, this is used to
/// show a list of keys that _could_ be used in the case the finalize method
/// fails due to keys still being present in the map
expected: Vec<&'static str>,
/// The span for the table location
span: Span,
}
impl<'de> From<(Table<'de>, Span)> for TableHelper<'de> {
fn from((table, span): (Table<'de>, Span)) -> Self {
Self {
table,
span,
expected: Vec::new(),
errors: Vec::new(),
}
}
}
impl<'de> TableHelper<'de> {
/// Creates a helper for the value, failing if it is not a table
pub fn new(value: &mut Value<'de>) -> Result<Self, DeserError> {
let table = match value.take() {
ValueInner::Table(table) => table,
other => return Err(expected("a table", other, value.span).into()),
};
Ok(Self {
errors: Vec::new(),
table,
expected: Vec::new(),
span: value.span,
})
}
/// Returns true if the table contains the specified key
#[inline]
pub fn contains(&self, name: &str) -> bool {
self.table.contains_key(name)
}
/// Takes the specified key and its value if it exists
#[inline]
pub fn take(&mut self, name: &'static str) -> Option<(value::Key<'de>, Value<'de>)> {
self.expected.push(name);
self.table.remove_entry(name)
}
/// Attempts to deserialize the specified key
///
/// Errors that occur when calling this method are automatically added to
/// the set of errors that are reported from [`Self::finalize`], so not early
/// returning if this method fails will still report the error by default
///
/// # Errors
/// - The key does not exist
/// - The [`Deserialize`] implementation for the type returns an error
#[inline]
pub fn required<T: Deserialize<'de>>(&mut self, name: &'static str) -> Result<T, Error> {
Ok(self.required_s(name)?.value)
}
/// The same as [`Self::required`], except it returns a [`Spanned`]
pub fn required_s<T: Deserialize<'de>>(
&mut self,
name: &'static str,
) -> Result<Spanned<T>, Error> {
self.expected.push(name);
let Some(mut val) = self.table.remove(name) else {
let missing = Error {
kind: ErrorKind::MissingField(name),
span: self.span,
line_info: None,
};
self.errors.push(missing.clone());
return Err(missing);
};
Spanned::<T>::deserialize(&mut val).map_err(|mut errs| {
let err = errs.errors.last().unwrap().clone();
self.errors.append(&mut errs.errors);
err
})
}
/// Attempts to deserialize the specified key, if it exists
///
/// Note that if the key exists but deserialization fails, an error will be
/// appended and if [`Self::finalize`] is called it will return that error
/// along with any others that occurred
pub fn optional<T: Deserialize<'de>>(&mut self, name: &'static str) -> Option<T> {
self.optional_s(name).map(|v| v.value)
}
/// The same as [`Self::optional`], except it returns a [`Spanned`]
pub fn optional_s<T: Deserialize<'de>>(&mut self, name: &'static str) -> Option<Spanned<T>> {
self.expected.push(name);
let mut val = self.table.remove(name)?;
match Spanned::<T>::deserialize(&mut val) {
Ok(v) => Some(v),
Err(mut err) => {
self.errors.append(&mut err.errors);
None
}
}
}
/// Called when you are finished with this [`TableHelper`]
///
/// If errors have been accumulated when using this [`TableHelper`], this will
/// return an error with all of those errors.
///
/// Additionally, if [`Option::None`] is passed, any keys that still exist
/// in the table will be added to an [`ErrorKind::UnexpectedKeys`] error,
/// which can be considered equivalent to [`#[serde(deny_unknown_fields)]`](https://serde.rs/container-attrs.html#deny_unknown_fields)
///
/// If you want simulate [`#[serde(flatten)]`](https://serde.rs/field-attrs.html#flatten)
/// you can instead put that table back in its original value during this step
pub fn finalize(mut self, original: Option<&mut Value<'de>>) -> Result<(), DeserError> {
if let Some(original) = original {
original.set(ValueInner::Table(self.table));
} else if !self.table.is_empty() {
let keys = self
.table
.into_keys()
.map(|key| (key.name.into(), key.span))
.collect();
self.errors.push(
(
ErrorKind::UnexpectedKeys {
keys,
expected: self.expected.into_iter().map(String::from).collect(),
},
self.span,
)
.into(),
);
}
if self.errors.is_empty() {
Ok(())
} else {
Err(DeserError {
errors: self.errors,
})
}
}
}
impl<'de> Deserialize<'de> for String {
fn deserialize(value: &mut Value<'de>) -> Result<Self, DeserError> {
value
.take_string(None)
.map(|s| s.into())
.map_err(DeserError::from)
}
}
impl<'de> Deserialize<'de> for std::borrow::Cow<'de, str> {
fn deserialize(value: &mut Value<'de>) -> Result<Self, DeserError> {
value.take_string(None).map_err(DeserError::from)
}
}
impl<'de> Deserialize<'de> for bool {
fn deserialize(value: &mut Value<'de>) -> Result<Self, DeserError> {
match value.take() {
ValueInner::Boolean(b) => Ok(b),
other => Err(expected("a bool", other, value.span).into()),
}
}
}
macro_rules! integer {
($num:ty) => {
impl<'de> Deserialize<'de> for $num {
fn deserialize(value: &mut Value<'de>) -> Result<Self, DeserError> {
match value.take() {
ValueInner::Integer(i) => {
let i = i.try_into().map_err(|_| {
DeserError::from(Error {
kind: ErrorKind::OutOfRange(stringify!($num)),
span: value.span,
line_info: None,
})
})?;
Ok(i)
}
other => Err(expected(stringify!($num), other, value.span).into()),
}
}
}
};
}
integer!(u8);
integer!(u16);
integer!(u32);
integer!(u64);
integer!(i8);
integer!(i16);
integer!(i32);
integer!(i64);
integer!(usize);
integer!(isize);
impl<'de> Deserialize<'de> for f32 {
fn deserialize(value: &mut Value<'de>) -> Result<Self, DeserError> {
match value.take() {
ValueInner::Float(f) => Ok(f as f32),
other => Err(expected("a float", other, value.span).into()),
}
}
}
impl<'de> Deserialize<'de> for f64 {
fn deserialize(value: &mut Value<'de>) -> Result<Self, DeserError> {
match value.take() {
ValueInner::Float(f) => Ok(f),
other => Err(expected("a float", other, value.span).into()),
}
}
}
impl<'de, T> Deserialize<'de> for Vec<T>
where
T: Deserialize<'de>,
{
fn deserialize(value: &mut value::Value<'de>) -> Result<Self, DeserError> {
match value.take() {
ValueInner::Array(arr) => {
let mut errors = Vec::new();
let mut s = Vec::new();
for mut v in arr {
match T::deserialize(&mut v) {
Ok(v) => s.push(v),
Err(mut err) => errors.append(&mut err.errors),
}
}
if errors.is_empty() {
Ok(s)
} else {
Err(DeserError { errors })
}
}
other => Err(expected("an array", other, value.span).into()),
}
}
}