summaryrefslogtreecommitdiff
path: root/serde_dhall/src/options/de.rs
blob: 3f0686df69744b271be14bff0319e4bacc75a82e (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use dhall::{Ctxt, Parsed};

use crate::options::{HasAnnot, ManualAnnot, NoAnnot, StaticAnnot, TypeAnnot};
use crate::SimpleType;
use crate::{Error, ErrorKind, FromDhall, Result, Value};

#[derive(Debug, Clone)]
enum Source<'a> {
    Str(&'a str),
    File(PathBuf),
    BinaryFile(PathBuf),
    // Url(&'a str),
}

/// Controls how a Dhall value is read.
///
/// This builder exposes the ability to configure how a value is deserialized and what operations
/// are permitted during evaluation.
///
/// Generally speaking, when using [`Deserializer`], you'll create it with [`from_str()`] or
/// [`from_file()`], then chain calls to methods to set each option, then call [`parse()`]. This
/// will give you a [`Result<T>`] where `T` is a deserializable type of your choice.
///
/// [`parse()`]: Deserializer::parse()
/// [`Result<T>`]: Result
///
/// # Examples
///
/// Reading from a file:
///
/// ```no_run
/// # fn main() -> serde_dhall::Result<()> {
/// use serde_dhall::from_file;
///
/// let data = from_file("foo.dhall").parse::<u64>()?;
/// # Ok(())
/// # }
/// ```
///
/// Reading from a file and checking the value against a provided type:
///
/// ```no_run
/// # fn main() -> serde_dhall::Result<()> {
/// use std::collections::HashMap;
/// use serde_dhall::{from_file, from_str};
///
/// let ty = from_str("{ x: Natural, y: Natural }").parse()?;
/// let data = from_file("foo.dhall")
///             .type_annotation(&ty)
///             .parse::<HashMap<String, u64>>()?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Deserializer<'a, A> {
    source: Source<'a>,
    annot: A,
    allow_imports: bool,
    substitutions: HashMap<dhall::syntax::Label, dhall::syntax::Expr>,
    // allow_remote_imports: bool,
    // use_cache: bool,
}

impl<'a> Deserializer<'a, NoAnnot> {
    fn default_with_source(source: Source<'a>) -> Self {
        Deserializer {
            source,
            annot: NoAnnot,
            allow_imports: true,
            substitutions: HashMap::new(),
            // allow_remote_imports: true,
            // use_cache: true,
        }
    }
    fn from_str(s: &'a str) -> Self {
        Self::default_with_source(Source::Str(s))
    }
    fn from_file<P: AsRef<Path>>(path: P) -> Self {
        Self::default_with_source(Source::File(path.as_ref().to_owned()))
    }
    fn from_binary_file<P: AsRef<Path>>(path: P) -> Self {
        Self::default_with_source(Source::BinaryFile(path.as_ref().to_owned()))
    }
    // fn from_url(url: &'a str) -> Self {
    //     Self::default_with_source(Source::Url(url))
    // }

    /// Ensures that the parsed value matches the provided type.
    ///
    /// In many cases the Dhall type that corresponds to a Rust type can be inferred automatically.
    /// See the [`StaticType`] trait and the [`static_type_annotation()`] method for that.
    ///
    /// # Example
    ///
    /// ```
    /// # fn main() -> serde_dhall::Result<()> {
    /// use std::collections::HashMap;
    /// use serde::Deserialize;
    /// use serde_dhall::{from_str, SimpleType};
    ///
    /// // Parse a Dhall type
    /// let type_str = "{ x: Natural, y: Natural }";
    /// let ty = from_str(type_str).parse::<SimpleType>()?;
    ///
    /// // Parse some Dhall data.
    /// let data = "{ x = 1, y = 1 + 1 }";
    /// let point = from_str(data)
    ///     .type_annotation(&ty)
    ///     .parse::<HashMap<String, u64>>()?;
    /// assert_eq!(point.get("y"), Some(&2));
    ///
    /// // Invalid data fails the type validation; deserialization would have succeeded otherwise.
    /// let invalid_data = "{ x = 1, z = 3 }";
    /// assert!(
    ///     from_str(invalid_data)
    ///         .type_annotation(&ty)
    ///         .parse::<HashMap<String, u64>>()
    ///         .is_err()
    /// );
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`StaticType`]: crate::StaticType
    /// [`static_type_annotation()`]: Deserializer::static_type_annotation()
    pub fn type_annotation<'ty>(
        self,
        ty: &'ty SimpleType,
    ) -> Deserializer<'a, ManualAnnot<'ty>> {
        Deserializer {
            annot: ManualAnnot(ty),
            source: self.source,
            allow_imports: self.allow_imports,
            substitutions: self.substitutions,
        }
    }

    /// Ensures that the parsed value matches the type of `T`.
    ///
    /// `T` must implement the [`StaticType`] trait. If it doesn't, you can use
    /// [`type_annotation()`] to provide a type manually.
    ///
    /// # Example
    ///
    /// ```
    /// # fn main() -> serde_dhall::Result<()> {
    /// use serde::Deserialize;
    /// use serde_dhall::StaticType;
    ///
    /// #[derive(Deserialize, StaticType)]
    /// struct Point {
    ///     x: u64,
    ///     y: Option<u64>,
    /// }
    ///
    /// // Some Dhall data
    /// let data = "{ x = 1, y = Some (1 + 1) }";
    ///
    /// // Convert the Dhall string to a Point.
    /// let point = serde_dhall::from_str(data)
    ///     .static_type_annotation()
    ///     .parse::<Point>()?;
    /// assert_eq!(point.x, 1);
    /// assert_eq!(point.y, Some(2));
    ///
    /// // Invalid data fails the type validation; deserialization would have succeeded otherwise.
    /// let invalid_data = "{ x = 1 }";
    /// assert!(
    ///     serde_dhall::from_str(invalid_data)
    ///         .static_type_annotation()
    ///         .parse::<Point>()
    ///         .is_err()
    /// );
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`StaticType`]: crate::StaticType
    /// [`type_annotation()`]: Deserializer::type_annotation()
    pub fn static_type_annotation(self) -> Deserializer<'a, StaticAnnot> {
        Deserializer {
            annot: StaticAnnot,
            source: self.source,
            allow_imports: self.allow_imports,
            substitutions: self.substitutions,
        }
    }
}

impl<'a, A> Deserializer<'a, A> {
    /// Sets whether to enable imports.
    ///
    /// By default, imports are enabled.
    ///
    /// # Example
    ///
    /// ```
    /// # fn main() -> serde_dhall::Result<()> {
    /// use serde::Deserialize;
    /// use serde_dhall::SimpleType;
    ///
    /// let data = "12 + ./other_file.dhall : Natural";
    /// assert!(
    ///     serde_dhall::from_str(data)
    ///         .imports(false)
    ///         .parse::<u64>()
    ///         .is_err()
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn imports(self, imports: bool) -> Self {
        Deserializer {
            allow_imports: imports,
            ..self
        }
    }

    // /// TODO
    // pub fn remote_imports(&mut self, imports: bool) -> &mut Self {
    //     self.allow_remote_imports = imports;
    //     if imports {
    //         self.allow_imports = true;
    //     }
    //     self
    // }

    /// injects a collection of names which should be substituted with
    /// the given types, i.e. effectively adds built-in type variables
    /// which do not need to be imported within dhall.
    ///
    /// This is especially useful when deserialising into many nested
    /// structs and enums at once, since it allows exposing the rust
    /// types to dhall without having to redefine them in both languages
    /// and manually keep both definitions in sync.
    ///
    /// # Example
    /// ```
    /// use serde::Deserialize;
    /// use serde_dhall::StaticType;
    /// use std::collections::HashMap;
    ///
    /// #[derive(Deserialize, StaticType, Debug, PartialEq)]
    /// enum Newtype {
    ///   Foo,
    ///   Bar
    /// }
    ///
    /// let mut substs = HashMap::new();
    /// substs.insert(
    ///     "Newtype".to_string(),
    ///     Newtype::static_type()
    /// );
    ///
    /// let data = "Newtype.Bar";
    ///
    /// let deserialized = serde_dhall::from_str(data)
    ///   .with_builtin_types(substs)
    ///   .parse::<Newtype>()
    ///   .unwrap();
    ///
    /// assert_eq!(deserialized, Newtype::Bar);
    ///
    /// ```
    pub fn with_builtin_types(
        self,
        tys: impl IntoIterator<Item = (String, SimpleType)>,
    ) -> Self {
        Deserializer {
            substitutions: tys
                .into_iter()
                .map(|(s, ty)| {
                    (dhall::syntax::Label::from_str(&s), ty.to_expr())
                })
                .chain(
                    self.substitutions
                        .iter()
                        .map(|(n,t)| (n.clone(), t.clone())),
                )
                .collect(),
            ..self
        }
    }

    pub fn with_builtin_type(self, name: String, ty: SimpleType) -> Self {
        Deserializer {
            substitutions: self
                .substitutions
                .iter()
                .map(|(n,t)| (n.clone(),t.clone()))
                .chain(std::iter::once((
                    dhall::syntax::Label::from_str(&name),
                    ty.to_expr(),
                )))
                .collect(),
            ..self
        }
    }

    fn _parse<T>(&self) -> dhall::error::Result<Result<Value>>
    where
        A: TypeAnnot,
        T: HasAnnot<A>,
    {
        Ctxt::with_new(|cx| {
            let parsed = match &self.source {
                Source::Str(s) => Parsed::parse_str(s)?,
                Source::File(p) => Parsed::parse_file(p.as_ref())?,
                Source::BinaryFile(p) => Parsed::parse_binary_file(p.as_ref())?,
            };

            let parsed_with_substs = self
                .substitutions
                .iter()
                .fold(parsed, |acc, (name, subst)| {
                    acc.substitute_name(name.clone(), subst.clone())
                });

            let resolved = if self.allow_imports {
                parsed_with_substs.resolve(cx)?
            } else {
                parsed_with_substs.skip_resolve(cx)?
            };
            let typed = match &T::get_annot(self.annot) {
                None => resolved.typecheck(cx)?,
                Some(ty) => resolved.typecheck_with(cx, &ty.to_hir())?,
            };
            Ok(Value::from_nir_and_ty(
                cx,
                typed.normalize(cx).as_nir(),
                typed.ty().as_nir(),
            ))
        })
    }

    /// Parses the chosen dhall value with the options provided.
    ///
    /// If you enabled static annotations, `T` is required to implement [`StaticType`].
    ///
    ///
    /// # Example
    ///
    /// ```
    /// # fn main() -> serde_dhall::Result<()> {
    /// let data = serde_dhall::from_str("6 * 7").parse::<u64>()?;
    /// assert_eq!(data, 42);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`StaticType`]: crate::StaticType
    pub fn parse<T>(&self) -> Result<T>
    where
        A: TypeAnnot,
        T: FromDhall + HasAnnot<A>,
    {
        let val = self
            ._parse::<T>()
            .map_err(ErrorKind::Dhall)
            .map_err(Error)??;
        T::from_dhall(&val)
    }
}

/// Deserialize a value from a string of Dhall text.
///
/// This returns a [`Deserializer`] object. Call the [`parse()`] method to get the deserialized
/// value, or use other [`Deserializer`] methods to control the deserialization process.
///
/// Imports will be resolved relative to the current directory.
///
/// # Example
///
/// ```rust
/// # fn main() -> serde_dhall::Result<()> {
/// use serde::Deserialize;
///
/// // We use serde's derive feature
/// #[derive(Deserialize)]
/// struct Point {
///     x: u64,
///     y: u64,
/// }
///
/// // Some Dhall data
/// let data = "{ x = 1, y = 1 + 1 } : { x: Natural, y: Natural }";
///
/// // Parse the Dhall string as a Point.
/// let point: Point = serde_dhall::from_str(data).parse()?;
///
/// assert_eq!(point.x, 1);
/// assert_eq!(point.y, 2);
/// # Ok(())
/// # }
/// ```
///
/// [`parse()`]: Deserializer::parse()
pub fn from_str(s: &str) -> Deserializer<'_, NoAnnot> {
    Deserializer::from_str(s)
}

/// Deserialize a value from a Dhall file.
///
/// This returns a [`Deserializer`] object. Call the [`parse()`] method to get the deserialized
/// value, or use other [`Deserializer`] methods to control the deserialization process.
///
/// Imports will be resolved relative to the provided file's path.
///
/// # Example
///
/// ```no_run
/// # fn main() -> serde_dhall::Result<()> {
/// use serde::Deserialize;
///
/// // We use serde's derive feature
/// #[derive(Deserialize)]
/// struct Point {
///     x: u64,
///     y: u64,
/// }
///
/// // Parse the Dhall file as a Point.
/// let point: Point = serde_dhall::from_file("foo.dhall").parse()?;
/// # Ok(())
/// # }
/// ```
///
/// [`parse()`]: Deserializer::parse()
pub fn from_file<'a, P: AsRef<Path>>(path: P) -> Deserializer<'a, NoAnnot> {
    Deserializer::from_file(path)
}

/// Deserialize a value from a CBOR-encoded Dhall binary file. The binary format is specified by
/// the Dhall standard specification and is mostly used for caching expressions. Using the format
/// is not recommended because errors won't have a file to refer to and thus will be hard to fix.
///
/// This returns a [`Deserializer`] object. Call the [`parse()`] method to get the deserialized
/// value, or use other [`Deserializer`] methods to control the deserialization process.
///
/// Imports will be resolved relative to the provided file's path.
///
/// # Example
///
/// ```no_run
/// # fn main() -> serde_dhall::Result<()> {
/// use serde::Deserialize;
///
/// // We use serde's derive feature
/// #[derive(Deserialize)]
/// struct Point {
///     x: u64,
///     y: u64,
/// }
///
/// // Parse the Dhall file as a Point.
/// let point: Point = serde_dhall::from_binary_file("foo.dhallb").parse()?;
/// # Ok(())
/// # }
/// ```
///
/// [`parse()`]: Deserializer::parse()
pub fn from_binary_file<'a, P: AsRef<Path>>(
    path: P,
) -> Deserializer<'a, NoAnnot> {
    Deserializer::from_binary_file(path)
}

// pub fn from_url(url: &str) -> Deserializer<'_, NoAnnot> {
//     Deserializer::from_url(url)
// }