summaryrefslogtreecommitdiff
path: root/serde_dhall/src/serde.rs
blob: 26708c1211cbdfdbf9a92757ce847d8e5c8a7566 (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
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
use std::borrow::Cow;

use dhall::phase::NormalizedExpr;
use dhall_syntax::ExprF;

use crate::de::{Deserialize, Error, Result};
use crate::Value;

impl<'a, T> Deserialize for T
where
    T: serde::Deserialize<'a>,
{
    fn from_dhall(v: &Value) -> Result<Self> {
        T::deserialize(Deserializer(Cow::Owned(v.to_expr())))
    }
}

struct Deserializer<'a>(Cow<'a, NormalizedExpr>);

impl<'de: 'a, 'a> serde::de::IntoDeserializer<'de, Error> for Deserializer<'a> {
    type Deserializer = Deserializer<'a>;
    fn into_deserializer(self) -> Self::Deserializer {
        self
    }
}

impl<'de: 'a, 'a> serde::Deserializer<'de> for Deserializer<'a> {
    type Error = Error;
    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
    where
        V: serde::de::Visitor<'de>,
    {
        use std::convert::TryInto;
        use ExprF::*;
        match self.0.as_ref().as_ref() {
            NaturalLit(n) => {
                if let Ok(n64) = (*n).try_into() {
                    visitor.visit_u64(n64)
                } else if let Ok(n32) = (*n).try_into() {
                    visitor.visit_u32(n32)
                } else {
                    unimplemented!()
                }
            }
            IntegerLit(n) => {
                if let Ok(n64) = (*n).try_into() {
                    visitor.visit_i64(n64)
                } else if let Ok(n32) = (*n).try_into() {
                    visitor.visit_i32(n32)
                } else {
                    unimplemented!()
                }
            }
            RecordLit(m) => visitor.visit_map(
                serde::de::value::MapDeserializer::new(m.iter().map(
                    |(k, v)| (k.as_ref(), Deserializer(Cow::Borrowed(v))),
                )),
            ),
            _ => unimplemented!(),
        }
    }

    serde::forward_to_deserialize_any! {
        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
        bytes byte_buf option unit unit_struct newtype_struct seq tuple
        tuple_struct map struct enum identifier ignored_any
    }
}