summaryrefslogtreecommitdiff
path: root/dhall/src/api/traits/deserialize.rs
diff options
context:
space:
mode:
authorNadrieril2019-05-07 18:12:04 +0200
committerNadrieril2019-05-07 18:12:04 +0200
commit3da450aa3fae23214aa982643b9bc4dd0ea4eaa6 (patch)
tree02e00cd008d2e7dc899b9211379596fe792f41c8 /dhall/src/api/traits/deserialize.rs
parentd8a3e831fb67f86269c4baa99f9f0798a73a7247 (diff)
parent14dfeb8e7d2aa87a361a711a485243449426b144 (diff)
Merge branch 'reorganize'
Diffstat (limited to 'dhall/src/api/traits/deserialize.rs')
-rw-r--r--dhall/src/api/traits/deserialize.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/dhall/src/api/traits/deserialize.rs b/dhall/src/api/traits/deserialize.rs
new file mode 100644
index 0000000..9673cf9
--- /dev/null
+++ b/dhall/src/api/traits/deserialize.rs
@@ -0,0 +1,53 @@
+use crate::error::*;
+use crate::phase::*;
+
+/// A data structure that can be deserialized from a Dhall expression
+///
+/// This is automatically implemented for any type that [serde][serde]
+/// can deserialize.
+///
+/// This trait cannot be implemented manually.
+pub trait Deserialize<'de>: Sized {
+ /// See [dhall::de::from_str][crate::de::from_str]
+ fn from_str(s: &'de str, ty: Option<&Type>) -> Result<Self>;
+}
+
+impl<'de> Deserialize<'de> for Parsed {
+ /// Simply parses the provided string. Ignores the
+ /// provided type.
+ fn from_str(s: &'de str, _: Option<&Type>) -> Result<Self> {
+ Ok(Parsed::parse_str(s)?)
+ }
+}
+
+impl<'de> Deserialize<'de> for Resolved {
+ /// Parses and resolves the provided string. Ignores the
+ /// provided type.
+ fn from_str(s: &'de str, ty: Option<&Type>) -> Result<Self> {
+ Ok(Parsed::from_str(s, ty)?.resolve()?)
+ }
+}
+
+impl<'de> Deserialize<'de> for Typed {
+ /// Parses, resolves and typechecks the provided string.
+ fn from_str(s: &'de 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<'de> Deserialize<'de> for Normalized {
+ /// Parses, resolves, typechecks and normalizes the provided string.
+ fn from_str(s: &'de str, ty: Option<&Type>) -> Result<Self> {
+ Ok(Typed::from_str(s, ty)?.normalize())
+ }
+}
+
+impl<'de> Deserialize<'de> for Type {
+ fn from_str(s: &'de str, ty: Option<&Type>) -> Result<Self> {
+ Ok(Normalized::from_str(s, ty)?.to_type())
+ }
+}