summaryrefslogtreecommitdiff
path: root/dhall/src/traits/deserialize.rs
blob: 1fbdfe19cf13487422709f7bccfadefb33e5dbb9 (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
use crate::error::*;
use crate::expr::*;

pub trait Deserialize<'a>: Sized {
    fn from_str(s: &'a str, ty: Option<&Type>) -> Result<Self>;
}

impl<'a> Deserialize<'a> for Parsed {
    /// Simply parses the provided string. Ignores the
    /// provided type.
    fn from_str(s: &'a str, _: Option<&Type>) -> Result<Self> {
        Ok(Parsed::parse_str(s)?)
    }
}

impl<'a> Deserialize<'a> for Resolved {
    /// Parses and resolves the provided string. Ignores the
    /// provided type.
    fn from_str(s: &'a str, ty: Option<&Type>) -> Result<Self> {
        Ok(Parsed::from_str(s, ty)?.resolve()?)
    }
}

impl<'a> Deserialize<'a> for Typed {
    /// Parses, resolves and typechecks the provided string.
    fn from_str(s: &'a str, ty: Option<&Type>) -> Result<Self> {
        let resolved = Resolved::from_str(s, ty)?;
        match ty {
            None => Ok(resolved.typecheck()?),
            Some(t) => Ok(resolved.typecheck_with(t)?),
        }
    }
}

impl<'a> Deserialize<'a> for Normalized {
    /// Parses, resolves, typechecks and normalizes the provided string.
    fn from_str(s: &'a str, ty: Option<&Type>) -> Result<Self> {
        Ok(Typed::from_str(s, ty)?.normalize())
    }
}

impl<'a> Deserialize<'a> for Type {
    fn from_str(s: &'a str, ty: Option<&Type>) -> Result<Self> {
        Ok(Normalized::from_str(s, ty)?.into_type())
    }
}