summaryrefslogtreecommitdiffstats
path: root/vendor/toml_edit/src/de/datetime.rs
blob: 14de28b3f13c3cfe0897d9495c1d8628e6c3b590 (plain)
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
use serde::de::value::BorrowedStrDeserializer;
use serde::de::IntoDeserializer;

use crate::de::Error;

pub(crate) struct DatetimeDeserializer {
    date: Option<crate::Datetime>,
}

impl DatetimeDeserializer {
    pub(crate) fn new(date: crate::Datetime) -> Self {
        Self { date: Some(date) }
    }
}

impl<'de> serde::de::MapAccess<'de> for DatetimeDeserializer {
    type Error = Error;

    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
    where
        K: serde::de::DeserializeSeed<'de>,
    {
        if self.date.is_some() {
            seed.deserialize(BorrowedStrDeserializer::new(
                toml_datetime::__unstable::FIELD,
            ))
            .map(Some)
        } else {
            Ok(None)
        }
    }

    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
    where
        V: serde::de::DeserializeSeed<'de>,
    {
        if let Some(date) = self.date.take() {
            seed.deserialize(date.to_string().into_deserializer())
        } else {
            panic!("next_value_seed called before next_key_seed")
        }
    }
}