diff options
author | Nadrieril | 2019-04-10 20:02:56 +0200 |
---|---|---|
committer | Nadrieril | 2019-04-10 20:02:56 +0200 |
commit | 56edb3a50fb4168ed76a3795f0ed774a754b6c32 (patch) | |
tree | 3b45926785054966252c43e904d36ee6d4f6cc24 | |
parent | 730d99adf2d4a7f222a71d687ea942545a7038fd (diff) |
Split traits module into submodules
Diffstat (limited to '')
-rw-r--r-- | dhall/src/traits/deserialize.rs | 43 | ||||
-rw-r--r-- | dhall/src/traits/mod.rs | 4 | ||||
-rw-r--r-- | dhall/src/traits/static_type.rs (renamed from dhall/src/traits.rs) | 50 |
3 files changed, 47 insertions, 50 deletions
diff --git a/dhall/src/traits/deserialize.rs b/dhall/src/traits/deserialize.rs new file mode 100644 index 0000000..8d1f565 --- /dev/null +++ b/dhall/src/traits/deserialize.rs @@ -0,0 +1,43 @@ +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, _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()) + } +} diff --git a/dhall/src/traits/mod.rs b/dhall/src/traits/mod.rs new file mode 100644 index 0000000..4ce8f97 --- /dev/null +++ b/dhall/src/traits/mod.rs @@ -0,0 +1,4 @@ +mod deserialize; +mod static_type; +pub use deserialize::Deserialize; +pub use static_type::{SimpleStaticType, StaticType}; diff --git a/dhall/src/traits.rs b/dhall/src/traits/static_type.rs index 328cbbc..b402ca9 100644 --- a/dhall/src/traits.rs +++ b/dhall/src/traits/static_type.rs @@ -1,8 +1,6 @@ -use crate::error::*; use crate::expr::*; use dhall_core::*; use dhall_generator::*; -use std::borrow::Cow; pub trait StaticType { fn get_static_type() -> Type; @@ -17,54 +15,6 @@ pub trait SimpleStaticType { fn get_simple_static_type() -> SimpleType; } - -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) } |