summaryrefslogtreecommitdiff
path: root/serde_dhall/src/options.rs
blob: 19b8587d5f1406251a46c70441ca436de5ea49ff (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
use std::path::{Path, PathBuf};

use dhall::Parsed;

use crate::simple::Type as SimpleType;
use crate::{Deserialize, Error, Result, StaticType, Value};

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

/// Options and flags which can be used to configure 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. The functions in the crate root are aliases for
/// commonly used options using this builder.
///
/// Generally speaking, when using `Options`, 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
/// `serde_dhall::Result<T>` where `T` a deserializable type of your choice.
///
/// # Examples
///
/// Reading from a file:
///
/// ```no_run
/// # fn main() -> serde_dhall::Result<()> {
/// use serde_dhall::options;
///
/// let data = options::from_file("foo.dhall").parse()?;
/// # Ok(())
/// # }
/// ```
///
/// Reading from a file and checking the value against a provided type:
///
/// ```no_run
/// # fn main() -> serde_dhall::Result<()> {
/// use serde_dhall::options;
///
/// let ty = options::from_str("{ x: Natural, y: Natural }").parse()?;
/// let data = options::from_file("foo.dhall")
///             .type_annotation(&ty)
///             .parse()?;
/// # Ok(())
/// # }
/// ```
/// /// TODO
#[derive(Debug, Clone)]
pub struct Options<'a, T> {
    source: Source<'a>,
    annot: Option<SimpleType>,
    allow_imports: bool,
    // allow_remote_imports: bool,
    // use_cache: bool,
    target_type: std::marker::PhantomData<T>,
}

impl<'a, T> Options<'a, T> {
    fn default_with_source(source: Source<'a>) -> Self {
        Options {
            source,
            annot: None,
            allow_imports: true,
            // allow_remote_imports: true,
            // use_cache: true,
            target_type: std::marker::PhantomData,
        }
    }
    /// TODO
    fn from_str(s: &'a str) -> Self {
        Self::default_with_source(Source::Str(s))
    }
    /// TODO
    fn from_file<P: AsRef<Path>>(path: P) -> Self {
        Self::default_with_source(Source::File(path.as_ref().to_owned()))
    }
    // fn from_url(url: &'a str) -> Self {
    //     Self::default_with_source(Source::Url(url))
    // }

    /// TODO
    pub fn imports(&mut self, imports: bool) -> &mut Self {
        self.allow_imports = imports;
        self
    }
    // pub fn remote_imports(&mut self, imports: bool) -> &mut Self {
    //     self.allow_remote_imports = imports;
    //     if imports {
    //         self.allow_imports = true;
    //     }
    //     self
    // }
    // /// TODO
    pub fn type_annotation(&mut self, ty: &SimpleType) -> &mut Self {
        self.annot = Some(ty.clone());
        self
    }
    /// TODO
    pub fn static_type_annotation(&mut self) -> &mut Self
    where
        T: StaticType,
    {
        self.annot = Some(T::static_type());
        self
    }

    fn _parse(&self) -> dhall::error::Result<Value> {
        let parsed = match &self.source {
            Source::Str(s) => Parsed::parse_str(s)?,
            Source::File(p) => Parsed::parse_file(p.as_ref())?,
        };
        let resolved = if self.allow_imports {
            parsed.resolve()?
        } else {
            parsed.skip_resolve()?
        };
        let typed = match &self.annot {
            None => resolved.typecheck()?,
            Some(ty) => resolved.typecheck_with(&ty.to_value().hir)?,
        };
        Ok(Value::from_nir(typed.normalize().as_nir()))
    }
    /// TODO
    pub fn parse(&self) -> Result<T>
    where
        T: Deserialize,
    {
        let val = self._parse().map_err(Error::Dhall)?;
        T::from_dhall(&val)
    }
}

/// TODO
pub fn from_str<'a, T>(s: &'a str) -> Options<'a, T> {
    Options::from_str(s)
}
/// TODO
pub fn from_file<'a, T, P: AsRef<Path>>(path: P) -> Options<'a, T> {
    Options::from_file(path)
}
// pub fn from_url<'a, T>(url: &'a str) -> Options<'a, T> {
//     Options::from_url(url)
// }