summaryrefslogtreecommitdiff
path: root/dhall/src/traits
diff options
context:
space:
mode:
authorNadrieril2019-04-10 19:18:25 +0200
committerNadrieril2019-04-10 19:18:25 +0200
commitff12918696181f1b0f2b8272944044e27c89e319 (patch)
tree7651c4fdb7bb68b980d5657aefe8e5f92dacfc93 /dhall/src/traits
parent58f10a2a274fe858da6cc73d4a33718bfc46d52b (diff)
Add a new Deserialize trait for reading dhall values
Diffstat (limited to '')
-rw-r--r--dhall/src/traits.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/dhall/src/traits.rs b/dhall/src/traits.rs
index 4925457..370632e 100644
--- a/dhall/src/traits.rs
+++ b/dhall/src/traits.rs
@@ -19,6 +19,56 @@ pub trait SimpleStaticType {
fn get_simple_static_type() -> SimpleType;
}
+pub type Error = ();
+pub type Result<T> = std::result::Result<T, Error>;
+
+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, _ty: Option<&Type>) -> Result<Self> {
+ Ok(Parsed::parse_str(s).map_err(|_| ())?)
+ }
+}
+
+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()
+ .map_err(|_| ())?
+ )
+ }
+}
+
+impl<'a> Deserialize<'a> for Typed {
+ /// Parses, resolves and typechecks the provided string.
+ fn from_str(s: &'a str, ty: Option<&Type>) -> Result<Self> {
+ // TODO: compare with provided type
+ Ok(Resolved::from_str(s, ty)?
+ .typecheck()
+ .map_err(|_| ())?
+ )
+ }
+}
+
+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())
+ }
+}
+
fn mktype(x: SubExpr<X, X>) -> SimpleType {
SimpleType(x)
}