| 1 |
//! Serde visitor for various types. |
| 2 |
|
| 3 |
use core::fmt; |
| 4 |
use core::marker::PhantomData; |
| 5 |
|
| 6 |
use serde::de; |
| 7 |
#[cfg(feature = "parsing")] |
| 8 |
use serde::Deserializer; |
| 9 |
|
| 10 |
#[cfg(feature = "parsing")] |
| 11 |
use super::{ |
| 12 |
DATE_FORMAT, OFFSET_DATE_TIME_FORMAT, PRIMITIVE_DATE_TIME_FORMAT, TIME_FORMAT, |
| 13 |
UTC_OFFSET_FORMAT, |
| 14 |
}; |
| 15 |
use crate::error::ComponentRange; |
| 16 |
#[cfg(feature = "parsing")] |
| 17 |
use crate::format_description::well_known::*; |
| 18 |
use crate::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday}; |
| 19 |
|
| 20 |
/// A serde visitor for various types. |
| 21 |
pub(super) struct Visitor<T: ?Sized>(pub(super) PhantomData<T>); |
| 22 |
|
| 23 |
impl<'a> de::Visitor<'a> for Visitor<Date> { |
| 24 |
type Value = Date; |
| 25 |
|
| 26 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 27 |
formatter.write_str("a `Date`") |
| 28 |
} |
| 29 |
|
| 30 |
#[cfg(feature = "parsing")] |
| 31 |
fn visit_str<E: de::Error>(self, value: &str) -> Result<Date, E> { |
| 32 |
Date::parse(value, &DATE_FORMAT).map_err(E::custom) |
| 33 |
} |
| 34 |
|
| 35 |
fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<Date, A::Error> { |
| 36 |
let year = item!(seq, "year")?; |
| 37 |
let ordinal = item!(seq, "day of year")?; |
| 38 |
Date::from_ordinal_date(year, ordinal).map_err(ComponentRange::into_de_error) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
impl<'a> de::Visitor<'a> for Visitor<Duration> { |
| 43 |
type Value = Duration; |
| 44 |
|
| 45 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 46 |
formatter.write_str("a `Duration`") |
| 47 |
} |
| 48 |
|
| 49 |
fn visit_str<E: de::Error>(self, value: &str) -> Result<Duration, E> { |
| 50 |
let (seconds, nanoseconds) = value.split_once('.').ok_or_else(|| { |
| 51 |
de::Error::invalid_value(de::Unexpected::Str(value), &"a decimal point") |
| 52 |
})?; |
| 53 |
|
| 54 |
let seconds = seconds |
| 55 |
.parse() |
| 56 |
.map_err(|_| de::Error::invalid_value(de::Unexpected::Str(seconds), &"seconds"))?; |
| 57 |
let mut nanoseconds = nanoseconds.parse().map_err(|_| { |
| 58 |
de::Error::invalid_value(de::Unexpected::Str(nanoseconds), &"nanoseconds") |
| 59 |
})?; |
| 60 |
|
| 61 |
if seconds < 0 { |
| 62 |
nanoseconds *= -1; |
| 63 |
} |
| 64 |
|
| 65 |
Ok(Duration::new(seconds, nanoseconds)) |
| 66 |
} |
| 67 |
|
| 68 |
fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<Duration, A::Error> { |
| 69 |
let seconds = item!(seq, "seconds")?; |
| 70 |
let nanoseconds = item!(seq, "nanoseconds")?; |
| 71 |
Ok(Duration::new(seconds, nanoseconds)) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
impl<'a> de::Visitor<'a> for Visitor<OffsetDateTime> { |
| 76 |
type Value = OffsetDateTime; |
| 77 |
|
| 78 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 79 |
formatter.write_str("an `OffsetDateTime`") |
| 80 |
} |
| 81 |
|
| 82 |
#[cfg(feature = "parsing")] |
| 83 |
fn visit_str<E: de::Error>(self, value: &str) -> Result<OffsetDateTime, E> { |
| 84 |
OffsetDateTime::parse(value, &OFFSET_DATE_TIME_FORMAT).map_err(E::custom) |
| 85 |
} |
| 86 |
|
| 87 |
fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<OffsetDateTime, A::Error> { |
| 88 |
let year = item!(seq, "year")?; |
| 89 |
let ordinal = item!(seq, "day of year")?; |
| 90 |
let hour = item!(seq, "hour")?; |
| 91 |
let minute = item!(seq, "minute")?; |
| 92 |
let second = item!(seq, "second")?; |
| 93 |
let nanosecond = item!(seq, "nanosecond")?; |
| 94 |
let offset_hours = item!(seq, "offset hours")?; |
| 95 |
let offset_minutes = item!(seq, "offset minutes")?; |
| 96 |
let offset_seconds = item!(seq, "offset seconds")?; |
| 97 |
|
| 98 |
Date::from_ordinal_date(year, ordinal) |
| 99 |
.and_then(|date| date.with_hms_nano(hour, minute, second, nanosecond)) |
| 100 |
.and_then(|datetime| { |
| 101 |
UtcOffset::from_hms(offset_hours, offset_minutes, offset_seconds) |
| 102 |
.map(|offset| datetime.assume_offset(offset)) |
| 103 |
}) |
| 104 |
.map_err(ComponentRange::into_de_error) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
impl<'a> de::Visitor<'a> for Visitor<PrimitiveDateTime> { |
| 109 |
type Value = PrimitiveDateTime; |
| 110 |
|
| 111 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 112 |
formatter.write_str("a `PrimitiveDateTime`") |
| 113 |
} |
| 114 |
|
| 115 |
#[cfg(feature = "parsing")] |
| 116 |
fn visit_str<E: de::Error>(self, value: &str) -> Result<PrimitiveDateTime, E> { |
| 117 |
PrimitiveDateTime::parse(value, &PRIMITIVE_DATE_TIME_FORMAT).map_err(E::custom) |
| 118 |
} |
| 119 |
|
| 120 |
fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<PrimitiveDateTime, A::Error> { |
| 121 |
let year = item!(seq, "year")?; |
| 122 |
let ordinal = item!(seq, "day of year")?; |
| 123 |
let hour = item!(seq, "hour")?; |
| 124 |
let minute = item!(seq, "minute")?; |
| 125 |
let second = item!(seq, "second")?; |
| 126 |
let nanosecond = item!(seq, "nanosecond")?; |
| 127 |
|
| 128 |
Date::from_ordinal_date(year, ordinal) |
| 129 |
.and_then(|date| date.with_hms_nano(hour, minute, second, nanosecond)) |
| 130 |
.map_err(ComponentRange::into_de_error) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
impl<'a> de::Visitor<'a> for Visitor<Time> { |
| 135 |
type Value = Time; |
| 136 |
|
| 137 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 138 |
formatter.write_str("a `Time`") |
| 139 |
} |
| 140 |
|
| 141 |
#[cfg(feature = "parsing")] |
| 142 |
fn visit_str<E: de::Error>(self, value: &str) -> Result<Time, E> { |
| 143 |
Time::parse(value, &TIME_FORMAT).map_err(E::custom) |
| 144 |
} |
| 145 |
|
| 146 |
fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<Time, A::Error> { |
| 147 |
let hour = item!(seq, "hour")?; |
| 148 |
let minute = item!(seq, "minute")?; |
| 149 |
let second = item!(seq, "second")?; |
| 150 |
let nanosecond = item!(seq, "nanosecond")?; |
| 151 |
|
| 152 |
Time::from_hms_nano(hour, minute, second, nanosecond).map_err(ComponentRange::into_de_error) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl<'a> de::Visitor<'a> for Visitor<UtcOffset> { |
| 157 |
type Value = UtcOffset; |
| 158 |
|
| 159 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 160 |
formatter.write_str("a `UtcOffset`") |
| 161 |
} |
| 162 |
|
| 163 |
#[cfg(feature = "parsing")] |
| 164 |
fn visit_str<E: de::Error>(self, value: &str) -> Result<UtcOffset, E> { |
| 165 |
UtcOffset::parse(value, &UTC_OFFSET_FORMAT).map_err(E::custom) |
| 166 |
} |
| 167 |
|
| 168 |
fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<UtcOffset, A::Error> { |
| 169 |
let hours = item!(seq, "offset hours")?; |
| 170 |
let mut minutes = 0; |
| 171 |
let mut seconds = 0; |
| 172 |
|
| 173 |
if let Ok(Some(min)) = seq.next_element() { |
| 174 |
minutes = min; |
| 175 |
if let Ok(Some(sec)) = seq.next_element() { |
| 176 |
seconds = sec; |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
UtcOffset::from_hms(hours, minutes, seconds).map_err(ComponentRange::into_de_error) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
impl de::Visitor<'_> for Visitor<Weekday> { |
| 185 |
type Value = Weekday; |
| 186 |
|
| 187 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 188 |
formatter.write_str("a `Weekday`") |
| 189 |
} |
| 190 |
|
| 191 |
fn visit_str<E: de::Error>(self, value: &str) -> Result<Weekday, E> { |
| 192 |
match value { |
| 193 |
"Monday" => Ok(Weekday::Monday), |
| 194 |
"Tuesday" => Ok(Weekday::Tuesday), |
| 195 |
"Wednesday" => Ok(Weekday::Wednesday), |
| 196 |
"Thursday" => Ok(Weekday::Thursday), |
| 197 |
"Friday" => Ok(Weekday::Friday), |
| 198 |
"Saturday" => Ok(Weekday::Saturday), |
| 199 |
"Sunday" => Ok(Weekday::Sunday), |
| 200 |
_ => Err(E::invalid_value(de::Unexpected::Str(value), &"a `Weekday`")), |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
fn visit_u64<E: de::Error>(self, value: u64) -> Result<Weekday, E> { |
| 205 |
match value { |
| 206 |
1 => Ok(Weekday::Monday), |
| 207 |
2 => Ok(Weekday::Tuesday), |
| 208 |
3 => Ok(Weekday::Wednesday), |
| 209 |
4 => Ok(Weekday::Thursday), |
| 210 |
5 => Ok(Weekday::Friday), |
| 211 |
6 => Ok(Weekday::Saturday), |
| 212 |
7 => Ok(Weekday::Sunday), |
| 213 |
_ => Err(E::invalid_value( |
| 214 |
de::Unexpected::Unsigned(value), |
| 215 |
&"a value in the range 1..=7", |
| 216 |
)), |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
impl de::Visitor<'_> for Visitor<Month> { |
| 222 |
type Value = Month; |
| 223 |
|
| 224 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 225 |
formatter.write_str("a `Month`") |
| 226 |
} |
| 227 |
|
| 228 |
fn visit_str<E: de::Error>(self, value: &str) -> Result<Month, E> { |
| 229 |
match value { |
| 230 |
"January" => Ok(Month::January), |
| 231 |
"February" => Ok(Month::February), |
| 232 |
"March" => Ok(Month::March), |
| 233 |
"April" => Ok(Month::April), |
| 234 |
"May" => Ok(Month::May), |
| 235 |
"June" => Ok(Month::June), |
| 236 |
"July" => Ok(Month::July), |
| 237 |
"August" => Ok(Month::August), |
| 238 |
"September" => Ok(Month::September), |
| 239 |
"October" => Ok(Month::October), |
| 240 |
"November" => Ok(Month::November), |
| 241 |
"December" => Ok(Month::December), |
| 242 |
_ => Err(E::invalid_value(de::Unexpected::Str(value), &"a `Month`")), |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
fn visit_u64<E: de::Error>(self, value: u64) -> Result<Month, E> { |
| 247 |
match value { |
| 248 |
1 => Ok(Month::January), |
| 249 |
2 => Ok(Month::February), |
| 250 |
3 => Ok(Month::March), |
| 251 |
4 => Ok(Month::April), |
| 252 |
5 => Ok(Month::May), |
| 253 |
6 => Ok(Month::June), |
| 254 |
7 => Ok(Month::July), |
| 255 |
8 => Ok(Month::August), |
| 256 |
9 => Ok(Month::September), |
| 257 |
10 => Ok(Month::October), |
| 258 |
11 => Ok(Month::November), |
| 259 |
12 => Ok(Month::December), |
| 260 |
_ => Err(E::invalid_value( |
| 261 |
de::Unexpected::Unsigned(value), |
| 262 |
&"a value in the range 1..=12", |
| 263 |
)), |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/// Implement a visitor for a well-known format. |
| 269 |
macro_rules! well_known { |
| 270 |
($article:literal, $name:literal, $($ty:tt)+) => { |
| 271 |
#[cfg(feature = "parsing")] |
| 272 |
impl de::Visitor<'_> for Visitor<$($ty)+> { |
| 273 |
type Value = OffsetDateTime; |
| 274 |
|
| 275 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 276 |
formatter.write_str(concat!($article, " ", $name, "-formatted `OffsetDateTime`")) |
| 277 |
} |
| 278 |
|
| 279 |
fn visit_str<E: de::Error>(self, value: &str) -> Result<OffsetDateTime, E> { |
| 280 |
OffsetDateTime::parse(value, &$($ty)+).map_err(E::custom) |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
#[cfg(feature = "parsing")] |
| 285 |
impl<'a> de::Visitor<'a> for Visitor<Option<$($ty)+>> { |
| 286 |
type Value = Option<OffsetDateTime>; |
| 287 |
|
| 288 |
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 289 |
formatter.write_str(concat!( |
| 290 |
$article, |
| 291 |
" ", |
| 292 |
$name, |
| 293 |
"-formatted `Option<OffsetDateTime>`" |
| 294 |
)) |
| 295 |
} |
| 296 |
|
| 297 |
fn visit_some<D: Deserializer<'a>>( |
| 298 |
self, |
| 299 |
deserializer: D, |
| 300 |
) -> Result<Option<OffsetDateTime>, D::Error> { |
| 301 |
deserializer |
| 302 |
.deserialize_any(Visitor::<$($ty)+>(PhantomData)) |
| 303 |
.map(Some) |
| 304 |
} |
| 305 |
|
| 306 |
fn visit_none<E: de::Error>(self) -> Result<Option<OffsetDateTime>, E> { |
| 307 |
Ok(None) |
| 308 |
} |
| 309 |
|
| 310 |
fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> { |
| 311 |
Ok(None) |
| 312 |
} |
| 313 |
} |
| 314 |
}; |
| 315 |
} |
| 316 |
|
| 317 |
well_known!("an", "RFC2822", Rfc2822); |
| 318 |
well_known!("an", "RFC3339", Rfc3339); |
| 319 |
well_known!( |
| 320 |
"an", |
| 321 |
"ISO 8601", |
| 322 |
Iso8601::<{ super::iso8601::SERDE_CONFIG }> |
| 323 |
); |