1 | use serde::de::value::BorrowedStrDeserializer; |
2 | use serde::de::IntoDeserializer; |
3 | |
4 | use crate::de::Error; |
5 | |
6 | pub(crate) struct DatetimeDeserializer { |
7 | date: Option<crate::Datetime>, |
8 | } |
9 | |
10 | impl DatetimeDeserializer { |
11 | pub(crate) fn new(date: crate::Datetime) -> Self { |
12 | Self { date: Some(date) } |
13 | } |
14 | } |
15 | |
16 | impl<'de> serde::de::MapAccess<'de> for DatetimeDeserializer { |
17 | type Error = Error; |
18 | |
19 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
20 | where |
21 | K: serde::de::DeserializeSeed<'de>, |
22 | { |
23 | if self.date.is_some() { |
24 | seed.deserialize(BorrowedStrDeserializer::new( |
25 | toml_datetime::__unstable::FIELD, |
26 | )) |
27 | .map(Some) |
28 | } else { |
29 | Ok(None) |
30 | } |
31 | } |
32 | |
33 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> |
34 | where |
35 | V: serde::de::DeserializeSeed<'de>, |
36 | { |
37 | if let Some(date) = self.date.take() { |
38 | seed.deserialize(date.to_string().into_deserializer()) |
39 | } else { |
40 | panic!("next_value_seed called before next_key_seed" ) |
41 | } |
42 | } |
43 | } |
44 | |