summaryrefslogtreecommitdiff
path: root/serde_dhall/src/deserialize.rs
blob: 3fb06f9075f4d88d19681c3b5595fa7f0895c58b (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
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
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fmt;

use serde::de::value::{
    MapAccessDeserializer, MapDeserializer, SeqDeserializer,
};
use serde::de::VariantAccess as _;

use dhall::syntax::NumKind;

use crate::value::SimpleValue;
use crate::{Error, ErrorKind, Value};

pub trait Sealed {}

/// A data structure that can be deserialized from a Dhall expression.
///
/// This is automatically implemented for any type that [serde] can deserialize.
/// In fact, this trait cannot be implemented manually. To implement it for your type,
/// use serde's derive mechanism.
///
/// # Example
///
/// ```rust
/// # fn main() -> serde_dhall::Result<()> {
/// use serde::Deserialize;
///
/// // Use serde's derive
/// #[derive(Deserialize)]
/// struct Point {
///     x: u64,
///     y: u64,
/// }
///
/// // Convert a Dhall string to a Point.
/// let point: Point = serde_dhall::from_str("{ x = 1, y = 1 + 1 }").parse()?;
/// # Ok(())
/// # }
/// ```
///
/// [serde]: https://serde.rs
pub trait FromDhall: Sealed + Sized {
    #[doc(hidden)]
    fn from_dhall(v: &Value) -> crate::Result<Self>;
}

impl<T> Sealed for T where T: serde::de::DeserializeOwned {}

/// Deserialize a Rust value from a Dhall [`SimpleValue`].
///
/// # Example
///
/// ```rust
/// # fn main() -> serde_dhall::Result<()> {
/// use std::collections::BTreeMap;
/// use serde::Deserialize;
///
/// // We use serde's derive feature
/// #[derive(Deserialize)]
/// struct Point {
///     x: u64,
///     y: u64,
/// }
///
/// // Some Dhall data
/// let mut data = BTreeMap::new();
/// data.insert(
///     "x".to_string(),
///     serde_dhall::SimpleValue::Num(serde_dhall::NumKind::Natural(1))
/// );
/// data.insert(
///     "y".to_string(),
///     serde_dhall::SimpleValue::Num(serde_dhall::NumKind::Natural(2))
/// );
/// let data = serde_dhall::SimpleValue::Record(data);
///
/// // Parse the Dhall value as a Point.
/// let point: Point = serde_dhall::from_simple_value(data)?;
///
/// assert_eq!(point.x, 1);
/// assert_eq!(point.y, 2);
/// # Ok(())
/// # }
/// ```
///
pub fn from_simple_value<T>(v: SimpleValue) -> crate::Result<T>
where
    T: serde::de::DeserializeOwned,
{
    T::deserialize(Deserializer(Cow::Owned(v)))
}

impl<T> FromDhall for T
where
    T: serde::de::DeserializeOwned,
{
    fn from_dhall(v: &Value) -> crate::Result<Self> {
        let sval = v.to_simple_value().ok_or_else(|| {
            Error(ErrorKind::Deserialize(format!(
                "this cannot be deserialized into the serde data model: {}",
                v
            )))
        })?;
        from_simple_value(sval)
    }
}

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

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) -> crate::Result<V::Value>
    where
        V: serde::de::Visitor<'de>,
    {
        use NumKind::*;
        use SimpleValue::*;

        let val = |x| Deserializer(Cow::Borrowed(x));
        match self.0.as_ref() {
            Num(Bool(x)) => visitor.visit_bool(*x),
            Num(Natural(x)) => visitor.visit_u64(*x),
            Num(Integer(x)) => visitor.visit_i64(*x),
            Num(Double(x)) => visitor.visit_f64((*x).into()),
            Text(x) => visitor.visit_str(x),
            List(xs) => {
                visitor.visit_seq(SeqDeserializer::new(xs.iter().map(val)))
            }
            Optional(None) => visitor.visit_none(),
            Optional(Some(x)) => visitor.visit_some(val(x)),
            Record(m) => visitor.visit_map(MapDeserializer::new(
                m.iter().map(|(k, v)| (k.as_ref(), val(v))),
            )),
            Union(field_name, Some(x)) => visitor.visit_enum(
                MapAccessDeserializer::new(MapDeserializer::new(
                    Some((field_name.as_str(), val(x))).into_iter(),
                )),
            ),
            Union(field_name, None) => visitor.visit_enum(
                MapAccessDeserializer::new(MapDeserializer::new(
                    Some((field_name.as_str(), ())).into_iter(),
                )),
            ),
        }
    }

    fn deserialize_tuple<V>(
        self,
        _: usize,
        visitor: V,
    ) -> crate::Result<V::Value>
    where
        V: serde::de::Visitor<'de>,
    {
        let val = |x| Deserializer(Cow::Borrowed(x));
        match self.0.as_ref() {
            // Blindly takes keys in sorted order.
            SimpleValue::Record(m) => visitor
                .visit_seq(SeqDeserializer::new(m.iter().map(|(_, v)| val(v)))),
            _ => self.deserialize_any(visitor),
        }
    }

    fn deserialize_unit<V>(self, visitor: V) -> crate::Result<V::Value>
    where
        V: serde::de::Visitor<'de>,
    {
        match self.0.as_ref() {
            SimpleValue::Record(m) if m.is_empty() => visitor.visit_unit(),
            _ => self.deserialize_any(visitor),
        }
    }

    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_struct newtype_struct seq
        tuple_struct map struct enum identifier ignored_any
    }
}

struct SimpleValueVisitor;

impl<'de> serde::de::Visitor<'de> for SimpleValueVisitor {
    type Value = SimpleValue;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("any valid Dhall value")
    }

    fn visit_bool<E>(self, value: bool) -> Result<SimpleValue, E> {
        Ok(SimpleValue::Num(NumKind::Bool(value)))
    }

    fn visit_i64<E>(self, value: i64) -> Result<SimpleValue, E> {
        Ok(SimpleValue::Num(NumKind::Integer(value)))
    }

    fn visit_u64<E>(self, value: u64) -> Result<SimpleValue, E> {
        Ok(SimpleValue::Num(NumKind::Natural(value)))
    }

    fn visit_f64<E>(self, value: f64) -> Result<SimpleValue, E> {
        Ok(SimpleValue::Num(NumKind::Double(value.into())))
    }

    fn visit_str<E>(self, value: &str) -> Result<SimpleValue, E> {
        Ok(SimpleValue::Text(String::from(value)))
    }

    fn visit_string<E>(self, value: String) -> Result<SimpleValue, E> {
        Ok(SimpleValue::Text(value))
    }

    fn visit_none<E>(self) -> Result<SimpleValue, E> {
        Ok(SimpleValue::Optional(None))
    }

    fn visit_some<D>(self, val: D) -> Result<SimpleValue, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let val = val.deserialize_any(SimpleValueVisitor)?;
        Ok(SimpleValue::Optional(Some(Box::new(val))))
    }

    fn visit_enum<V>(self, visitor: V) -> Result<SimpleValue, V::Error>
    where
        V: serde::de::EnumAccess<'de>,
    {
        let (name, variant): (String, _) = visitor.variant()?;
        // Serde does not allow me to check what kind of variant it is. This will work for dhall
        // values, because there are only two possible kinds of cvariants, but doesn't work in
        // general. Given that the `serde_value` crate ignores enums, I assume this is not fixable
        // :(.
        let val = variant.newtype_variant().ok();
        Ok(SimpleValue::Union(name, val))
    }

    fn visit_seq<V>(self, mut visitor: V) -> Result<SimpleValue, V::Error>
    where
        V: serde::de::SeqAccess<'de>,
    {
        let mut vec = Vec::new();
        while let Some(elem) = visitor.next_element()? {
            vec.push(elem);
        }
        Ok(SimpleValue::List(vec))
    }

    fn visit_map<V>(self, mut visitor: V) -> Result<SimpleValue, V::Error>
    where
        V: serde::de::MapAccess<'de>,
    {
        let mut record = BTreeMap::default();
        while let Some((key, value)) = visitor.next_entry()? {
            record.insert(key, value);
        }
        Ok(SimpleValue::Record(record))
    }
}

impl<'de> serde::de::Deserialize<'de> for SimpleValue {
    fn deserialize<D>(deserializer: D) -> Result<SimpleValue, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_any(SimpleValueVisitor)
    }
}