From fa89e9cb319b353332c9e835944e7f86a6604c42 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 18 Mar 2020 21:37:14 +0000 Subject: Move Value, SimpleValue and SimpleType to serde --- serde_dhall/src/value.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 serde_dhall/src/value.rs (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs new file mode 100644 index 0000000..a24f211 --- /dev/null +++ b/serde_dhall/src/value.rs @@ -0,0 +1,73 @@ +use dhall::semantics::Hir; +use dhall::syntax::Expr; +use dhall::{Normalized, Parsed}; + +use crate::simple::{SimpleType, SimpleValue}; +use crate::Error; + +/// A Dhall value. +#[derive(Debug, Clone)] +pub struct Value { + /// Invariant: in normal form + pub(crate) hir: Hir, + /// Cached conversions because they are annoying to construct from Hir. + /// At most one of them will be `Some`. + pub(crate) as_simple_val: Option, + pub(crate) as_simple_ty: Option, +} + +impl Value { + /// Parse a string into a Value, and optionally ensure that the value matches the provided type + /// annotation. + pub fn from_str_with_annot( + s: &str, + ty: Option<&Self>, + ) -> Result { + Self::from_str_with_annot_dhall_error(s, ty).map_err(Error::Dhall) + } + fn from_normalized(x: &Normalized) -> Self { + Value { + hir: x.to_hir(), + as_simple_val: SimpleValue::from_nir(x.as_nir()), + as_simple_ty: SimpleType::from_nir(x.as_nir()), + } + } + + fn from_str_with_annot_dhall_error( + s: &str, + ty: Option<&Self>, + ) -> Result { + let resolved = Parsed::parse_str(s)?.resolve()?; + let typed = match ty { + None => resolved.typecheck()?, + Some(ty) => resolved.typecheck_with(&ty.hir)?, + }; + Ok(Self::from_normalized(&typed.normalize())) + } + + /// Converts a Value into a SimpleValue. + pub fn to_simple_value(&self) -> Option { + self.as_simple_val.clone() + } + /// Converts a Value into a SimpleType. + pub fn to_simple_type(&self) -> Option { + self.as_simple_ty.clone() + } + + /// Converts a value back to the corresponding AST expression. + pub fn to_expr(&self) -> Expr { + self.hir.to_expr(Default::default()) + } +} + +impl Eq for Value {} +impl PartialEq for Value { + fn eq(&self, other: &Self) -> bool { + self.hir == other.hir + } +} +impl std::fmt::Display for Value { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + self.to_expr().fmt(f) + } +} -- cgit v1.2.3 From a1f370cb68974c1e69f8f85345e91ec763b23ae2 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 18 Mar 2020 22:28:41 +0000 Subject: Expose simple::Val/Ty properly in the API --- serde_dhall/src/value.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index a24f211..62632e5 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -1,11 +1,11 @@ -use dhall::semantics::Hir; +use dhall::semantics::{Hir, Nir}; use dhall::syntax::Expr; -use dhall::{Normalized, Parsed}; +use dhall::Parsed; -use crate::simple::{SimpleType, SimpleValue}; +use crate::simple::{Type as SimpleType, Value as SimpleValue}; use crate::Error; -/// A Dhall value. +/// An arbitrary Dhall value. #[derive(Debug, Clone)] pub struct Value { /// Invariant: in normal form @@ -21,28 +21,27 @@ impl Value { /// annotation. pub fn from_str_with_annot( s: &str, - ty: Option<&Self>, + ty: Option<&SimpleType>, ) -> Result { Self::from_str_with_annot_dhall_error(s, ty).map_err(Error::Dhall) } - fn from_normalized(x: &Normalized) -> Self { + fn from_nir(x: &Nir) -> Self { Value { - hir: x.to_hir(), - as_simple_val: SimpleValue::from_nir(x.as_nir()), - as_simple_ty: SimpleType::from_nir(x.as_nir()), + hir: x.to_hir_noenv(), + as_simple_val: SimpleValue::from_nir(x), + as_simple_ty: SimpleType::from_nir(x), } } - fn from_str_with_annot_dhall_error( s: &str, - ty: Option<&Self>, + ty: Option<&SimpleType>, ) -> Result { let resolved = Parsed::parse_str(s)?.resolve()?; let typed = match ty { None => resolved.typecheck()?, - Some(ty) => resolved.typecheck_with(&ty.hir)?, + Some(ty) => resolved.typecheck_with(&ty.to_value().hir)?, }; - Ok(Self::from_normalized(&typed.normalize())) + Ok(Self::from_nir(typed.normalize().as_nir())) } /// Converts a Value into a SimpleValue. @@ -55,7 +54,7 @@ impl Value { } /// Converts a value back to the corresponding AST expression. - pub fn to_expr(&self) -> Expr { + pub(crate) fn to_expr(&self) -> Expr { self.hir.to_expr(Default::default()) } } -- cgit v1.2.3 From 85e2e8ee5e83dadd05b6974ba6c951350cb97a61 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 21 Mar 2020 21:30:30 +0000 Subject: Introduce option builder --- serde_dhall/src/value.rs | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index 62632e5..d4ded90 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -1,9 +1,8 @@ use dhall::semantics::{Hir, Nir}; use dhall::syntax::Expr; -use dhall::Parsed; use crate::simple::{Type as SimpleType, Value as SimpleValue}; -use crate::Error; +use crate::{sealed::Sealed, Deserialize, Error}; /// An arbitrary Dhall value. #[derive(Debug, Clone)] @@ -17,32 +16,13 @@ pub struct Value { } impl Value { - /// Parse a string into a Value, and optionally ensure that the value matches the provided type - /// annotation. - pub fn from_str_with_annot( - s: &str, - ty: Option<&SimpleType>, - ) -> Result { - Self::from_str_with_annot_dhall_error(s, ty).map_err(Error::Dhall) - } - fn from_nir(x: &Nir) -> Self { + pub(crate) fn from_nir(x: &Nir) -> Self { Value { hir: x.to_hir_noenv(), as_simple_val: SimpleValue::from_nir(x), as_simple_ty: SimpleType::from_nir(x), } } - fn from_str_with_annot_dhall_error( - s: &str, - ty: Option<&SimpleType>, - ) -> Result { - let resolved = Parsed::parse_str(s)?.resolve()?; - let typed = match ty { - None => resolved.typecheck()?, - Some(ty) => resolved.typecheck_with(&ty.to_value().hir)?, - }; - Ok(Self::from_nir(typed.normalize().as_nir())) - } /// Converts a Value into a SimpleValue. pub fn to_simple_value(&self) -> Option { @@ -70,3 +50,11 @@ impl std::fmt::Display for Value { self.to_expr().fmt(f) } } + +impl Sealed for Value {} + +impl Deserialize for Value { + fn from_dhall(v: &Value) -> Result { + Ok(v.clone()) + } +} -- cgit v1.2.3 From 1a98b506055779e1a60558d9c5a56b071b3d61a0 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 22 Mar 2020 21:20:58 +0000 Subject: Reorganize API and internals of serde_dhall a bit --- serde_dhall/src/value.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index d4ded90..a119028 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -2,7 +2,7 @@ use dhall::semantics::{Hir, Nir}; use dhall::syntax::Expr; use crate::simple::{Type as SimpleType, Value as SimpleValue}; -use crate::{sealed::Sealed, Deserialize, Error}; +use crate::{Deserialize, Error, Sealed}; /// An arbitrary Dhall value. #[derive(Debug, Clone)] -- cgit v1.2.3 From 002d7c2a74647312a821598ac3d9f5521296873d Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 22 Mar 2020 21:25:44 +0000 Subject: Add a bunch of TODOs --- serde_dhall/src/value.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index a119028..2557efe 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -5,6 +5,7 @@ use crate::simple::{Type as SimpleType, Value as SimpleValue}; use crate::{Deserialize, Error, Sealed}; /// An arbitrary Dhall value. +/// TODO #[derive(Debug, Clone)] pub struct Value { /// Invariant: in normal form @@ -25,10 +26,12 @@ impl Value { } /// Converts a Value into a SimpleValue. + /// TODO pub fn to_simple_value(&self) -> Option { self.as_simple_val.clone() } /// Converts a Value into a SimpleType. + /// TODO pub fn to_simple_type(&self) -> Option { self.as_simple_ty.clone() } -- cgit v1.2.3 From fd4a81b1a92c1859941538b7f2212c621f4b43fd Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 22 Mar 2020 21:29:36 +0000 Subject: Hide SimpleValue from api --- serde_dhall/src/value.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index 2557efe..41a96ea 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -26,8 +26,7 @@ impl Value { } /// Converts a Value into a SimpleValue. - /// TODO - pub fn to_simple_value(&self) -> Option { + pub(crate) fn to_simple_value(&self) -> Option { self.as_simple_val.clone() } /// Converts a Value into a SimpleType. -- cgit v1.2.3 From a70922c6c6beb58a45da80f15576b54fb915ec28 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 22 Mar 2020 22:11:00 +0000 Subject: Rework SimpleType --- serde_dhall/src/value.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index 41a96ea..e8aae49 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -1,7 +1,7 @@ use dhall::semantics::{Hir, Nir}; use dhall::syntax::Expr; -use crate::simple::{Type as SimpleType, Value as SimpleValue}; +use crate::simple::{SimpleType, Value as SimpleValue}; use crate::{Deserialize, Error, Sealed}; /// An arbitrary Dhall value. -- cgit v1.2.3 From f598e3612a854076b570bbc9c88abcdc1da528bd Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 22 Mar 2020 22:17:22 +0000 Subject: Rework SimpleValue --- serde_dhall/src/value.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index e8aae49..ed932f5 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -1,7 +1,7 @@ use dhall::semantics::{Hir, Nir}; use dhall::syntax::Expr; -use crate::simple::{SimpleType, Value as SimpleValue}; +use crate::simple::{SimpleType, SimpleValue}; use crate::{Deserialize, Error, Sealed}; /// An arbitrary Dhall value. -- cgit v1.2.3 From 263fbebda7bc3c0f8c4497e1e508125ca91382f1 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 22 Mar 2020 22:22:46 +0000 Subject: Hide Value from API --- serde_dhall/src/value.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index ed932f5..88727bc 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -4,8 +4,8 @@ use dhall::syntax::Expr; use crate::simple::{SimpleType, SimpleValue}; use crate::{Deserialize, Error, Sealed}; +#[doc(hidden)] /// An arbitrary Dhall value. -/// TODO #[derive(Debug, Clone)] pub struct Value { /// Invariant: in normal form @@ -29,9 +29,9 @@ impl Value { pub(crate) fn to_simple_value(&self) -> Option { self.as_simple_val.clone() } + /// Converts a Value into a SimpleType. - /// TODO - pub fn to_simple_type(&self) -> Option { + pub(crate) fn to_simple_type(&self) -> Option { self.as_simple_ty.clone() } -- cgit v1.2.3 From 3a5f2044954aae7278f16e30561a81626dba6923 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 22 Mar 2020 22:27:56 +0000 Subject: Move mod simple into value --- serde_dhall/src/value.rs | 252 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 237 insertions(+), 15 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index 88727bc..f542f0a 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -1,19 +1,101 @@ -use dhall::semantics::{Hir, Nir}; -use dhall::syntax::Expr; +use std::collections::{BTreeMap, HashMap}; -use crate::simple::{SimpleType, SimpleValue}; -use crate::{Deserialize, Error, Sealed}; +use dhall::semantics::{Hir, HirKind, Nir, NirKind}; +use dhall::syntax::{Builtin, Expr, ExprKind, NumKind, Span}; + +use crate::{Deserialize, Error, Result, Sealed}; #[doc(hidden)] /// An arbitrary Dhall value. #[derive(Debug, Clone)] pub struct Value { /// Invariant: in normal form - pub(crate) hir: Hir, + hir: Hir, /// Cached conversions because they are annoying to construct from Hir. /// At most one of them will be `Some`. - pub(crate) as_simple_val: Option, - pub(crate) as_simple_ty: Option, + as_simple_val: Option, + as_simple_ty: Option, +} + +/// A simple value of the kind that can be decoded with serde +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum SimpleValue { + Num(NumKind), + Text(String), + Optional(Option>), + List(Vec), + Record(BTreeMap), + Union(String, Option>), +} + +/// The type of a value that can be decoded by Serde, like `{ x: Bool, y: List Natural }`. +/// +/// A `SimpleType` is used when deserializing values to ensure they are of the expected type. +/// Rather than letting `serde` handle potential type mismatches, this uses the type-checking +/// capabilities of Dhall to catch errors early and cleanly indicate in the user's code where the +/// mismatch happened. +/// +/// You would typically not manipulate `SimpleType`s by hand but rather let Rust infer it for your +/// datatype using the [`StaticType`][TODO] trait, and methods that require it like +/// [`from_file_static_type`][TODO] and [`Options::static_type_annotation`][TODO]. If you need to supply a +/// `SimpleType` manually however, you can deserialize it like any other Dhall value using the +/// functions provided by this crate. +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> serde_dhall::Result<()> { +/// use std::collections::HashMap; +/// use serde_dhall::SimpleType; +/// +/// let ty: SimpleType = +/// serde_dhall::from_str("{ x: Natural, y: Natural }")?; +/// +/// let mut map = HashMap::new(); +/// map.insert("x".to_string(), SimpleType::Natural); +/// map.insert("y".to_string(), SimpleType::Natural); +/// assert_eq!(ty, SimpleType::Record(map)); +/// # Ok(()) +/// # } +/// ``` +/// +/// ```rust +/// # fn main() -> serde_dhall::Result<()> { +/// use serde_dhall::{SimpleType, StaticType}; +/// +/// #[derive(StaticType)] +/// struct Foo { +/// x: bool, +/// y: Vec, +/// } +/// +/// let ty: SimpleType = +/// serde_dhall::from_str("{ x: Bool, y: List Natural }")?; +/// +/// assert_eq!(ty, Foo::static_type()); +/// # Ok(()) +/// # } +/// ``` +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SimpleType { + /// Corresponds to the Dhall type `Bool` + Bool, + /// Corresponds to the Dhall type `Natural` + Natural, + /// Corresponds to the Dhall type `Integer` + Integer, + /// Corresponds to the Dhall type `Double` + Double, + /// Corresponds to the Dhall type `Text` + Text, + /// Corresponds to the Dhall type `Optional T` + Optional(Box), + /// Corresponds to the Dhall type `List T` + List(Box), + /// Corresponds to the Dhall type `{ x : T, y : U }` + Record(HashMap), + /// Corresponds to the Dhall type `< x : T | y : U >` + Union(HashMap>), } impl Value { @@ -25,6 +107,10 @@ impl Value { } } + pub(crate) fn as_hir(&self) -> &Hir { + &self.hir + } + /// Converts a Value into a SimpleValue. pub(crate) fn to_simple_value(&self) -> Option { self.as_simple_val.clone() @@ -41,22 +127,158 @@ impl Value { } } -impl Eq for Value {} -impl PartialEq for Value { - fn eq(&self, other: &Self) -> bool { - self.hir == other.hir +impl SimpleValue { + pub(crate) fn from_nir(nir: &Nir) -> Option { + Some(match nir.kind() { + NirKind::Num(lit) => SimpleValue::Num(lit.clone()), + NirKind::TextLit(x) => SimpleValue::Text( + x.as_text() + .expect("Normal form should ensure the text is a string"), + ), + NirKind::EmptyOptionalLit(_) => SimpleValue::Optional(None), + NirKind::NEOptionalLit(x) => { + SimpleValue::Optional(Some(Box::new(Self::from_nir(x)?))) + } + NirKind::EmptyListLit(_) => SimpleValue::List(vec![]), + NirKind::NEListLit(xs) => SimpleValue::List( + xs.iter().map(Self::from_nir).collect::>()?, + ), + NirKind::RecordLit(kvs) => SimpleValue::Record( + kvs.iter() + .map(|(k, v)| Some((k.into(), Self::from_nir(v)?))) + .collect::>()?, + ), + NirKind::UnionLit(field, x, _) => SimpleValue::Union( + field.into(), + Some(Box::new(Self::from_nir(x)?)), + ), + NirKind::UnionConstructor(field, ty) + if ty.get(field).map(|f| f.is_some()) == Some(false) => + { + SimpleValue::Union(field.into(), None) + } + _ => return None, + }) } } -impl std::fmt::Display for Value { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - self.to_expr().fmt(f) + +impl SimpleType { + pub(crate) fn from_nir(nir: &Nir) -> Option { + Some(match nir.kind() { + NirKind::BuiltinType(b) => match b { + Builtin::Bool => SimpleType::Bool, + Builtin::Natural => SimpleType::Natural, + Builtin::Integer => SimpleType::Integer, + Builtin::Double => SimpleType::Double, + Builtin::Text => SimpleType::Text, + _ => unreachable!(), + }, + NirKind::OptionalType(t) => { + SimpleType::Optional(Box::new(Self::from_nir(t)?)) + } + NirKind::ListType(t) => { + SimpleType::List(Box::new(Self::from_nir(t)?)) + } + NirKind::RecordType(kts) => SimpleType::Record( + kts.iter() + .map(|(k, v)| Some((k.into(), Self::from_nir(v)?))) + .collect::>()?, + ), + NirKind::UnionType(kts) => SimpleType::Union( + kts.iter() + .map(|(k, v)| { + Some(( + k.into(), + v.as_ref() + .map(|v| Ok(Self::from_nir(v)?)) + .transpose()?, + )) + }) + .collect::>()?, + ), + _ => return None, + }) + } + + pub(crate) fn to_value(&self) -> Value { + Value { + hir: self.to_hir(), + as_simple_val: None, + as_simple_ty: Some(self.clone()), + } + } + pub(crate) fn to_hir(&self) -> Hir { + let hir = |k| Hir::new(HirKind::Expr(k), Span::Artificial); + hir(match self { + SimpleType::Bool => ExprKind::Builtin(Builtin::Bool), + SimpleType::Natural => ExprKind::Builtin(Builtin::Natural), + SimpleType::Integer => ExprKind::Builtin(Builtin::Integer), + SimpleType::Double => ExprKind::Builtin(Builtin::Double), + SimpleType::Text => ExprKind::Builtin(Builtin::Text), + SimpleType::Optional(t) => ExprKind::App( + hir(ExprKind::Builtin(Builtin::Optional)), + t.to_hir(), + ), + SimpleType::List(t) => { + ExprKind::App(hir(ExprKind::Builtin(Builtin::List)), t.to_hir()) + } + SimpleType::Record(kts) => ExprKind::RecordType( + kts.into_iter() + .map(|(k, t)| (k.as_str().into(), t.to_hir())) + .collect(), + ), + SimpleType::Union(kts) => ExprKind::UnionType( + kts.into_iter() + .map(|(k, t)| { + (k.as_str().into(), t.as_ref().map(|t| t.to_hir())) + }) + .collect(), + ), + }) } } impl Sealed for Value {} +impl Sealed for SimpleValue {} +impl Sealed for SimpleType {} impl Deserialize for Value { - fn from_dhall(v: &Value) -> Result { + fn from_dhall(v: &Value) -> Result { Ok(v.clone()) } } +impl Deserialize for SimpleValue { + fn from_dhall(v: &Value) -> Result { + v.to_simple_value().ok_or_else(|| { + Error::Deserialize(format!( + "this cannot be deserialized into a simple type: {}", + v + )) + }) + } +} +impl Deserialize for SimpleType { + fn from_dhall(v: &Value) -> Result { + v.to_simple_type().ok_or_else(|| { + Error::Deserialize(format!( + "this cannot be deserialized into a simple type: {}", + v + )) + }) + } +} + +impl Eq for Value {} +impl PartialEq for Value { + fn eq(&self, other: &Self) -> bool { + self.hir == other.hir + } +} +impl std::fmt::Display for Value { + fn fmt( + &self, + f: &mut std::fmt::Formatter, + ) -> std::result::Result<(), std::fmt::Error> { + self.to_expr().fmt(f) + } +} -- cgit v1.2.3 From 25c879e802e90a447e10e5f9a0f522217e34f20d Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 22 Mar 2020 22:59:11 +0000 Subject: Document more --- serde_dhall/src/value.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index f542f0a..bdc914f 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -36,11 +36,15 @@ pub(crate) enum SimpleValue { /// mismatch happened. /// /// You would typically not manipulate `SimpleType`s by hand but rather let Rust infer it for your -/// datatype using the [`StaticType`][TODO] trait, and methods that require it like -/// [`from_file_static_type`][TODO] and [`Options::static_type_annotation`][TODO]. If you need to supply a +/// datatype using the [`StaticType`] trait, and methods that require it like +/// [`from_file_static_type`] and [`Options::static_type_annotation`]. If you need to supply a /// `SimpleType` manually however, you can deserialize it like any other Dhall value using the /// functions provided by this crate. /// +/// [`StaticType`]: trait.StaticType.html +/// [`from_file_static_type`]: fn.from_file_static_type.html +/// [`Options::static_type_annotation`]: options/struct.Options.html#method.static_type_annotation +/// /// # Examples /// /// ```rust @@ -72,7 +76,7 @@ pub(crate) enum SimpleValue { /// let ty: SimpleType = /// serde_dhall::from_str("{ x: Bool, y: List Natural }")?; /// -/// assert_eq!(ty, Foo::static_type()); +/// assert_eq!(Foo::static_type(), ty); /// # Ok(()) /// # } /// ``` -- cgit v1.2.3 From f9848b54fe2e64901042fba66fb471999f415ff1 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 24 Mar 2020 18:53:25 +0000 Subject: Hide serde Error internals --- serde_dhall/src/value.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index bdc914f..3543f4d 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, HashMap}; use dhall::semantics::{Hir, HirKind, Nir, NirKind}; use dhall::syntax::{Builtin, Expr, ExprKind, NumKind, Span}; -use crate::{Deserialize, Error, Result, Sealed}; +use crate::{Deserialize, Error, ErrorKind, Result, Sealed}; #[doc(hidden)] /// An arbitrary Dhall value. @@ -254,20 +254,20 @@ impl Deserialize for Value { impl Deserialize for SimpleValue { fn from_dhall(v: &Value) -> Result { v.to_simple_value().ok_or_else(|| { - Error::Deserialize(format!( + Error(ErrorKind::Deserialize(format!( "this cannot be deserialized into a simple type: {}", v - )) + ))) }) } } impl Deserialize for SimpleType { fn from_dhall(v: &Value) -> Result { v.to_simple_type().ok_or_else(|| { - Error::Deserialize(format!( + Error(ErrorKind::Deserialize(format!( "this cannot be deserialized into a simple type: {}", v - )) + ))) }) } } -- cgit v1.2.3 From 15a0902b50ff6d1b36dbf7a220b6ff42ceefada6 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 5 Apr 2020 11:43:22 +0100 Subject: Rename Deserialize trait to FromDhall --- serde_dhall/src/value.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index 3543f4d..fee9d73 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, HashMap}; use dhall::semantics::{Hir, HirKind, Nir, NirKind}; use dhall::syntax::{Builtin, Expr, ExprKind, NumKind, Span}; -use crate::{Deserialize, Error, ErrorKind, Result, Sealed}; +use crate::{FromDhall, Error, ErrorKind, Result, Sealed}; #[doc(hidden)] /// An arbitrary Dhall value. @@ -246,12 +246,12 @@ impl Sealed for Value {} impl Sealed for SimpleValue {} impl Sealed for SimpleType {} -impl Deserialize for Value { +impl FromDhall for Value { fn from_dhall(v: &Value) -> Result { Ok(v.clone()) } } -impl Deserialize for SimpleValue { +impl FromDhall for SimpleValue { fn from_dhall(v: &Value) -> Result { v.to_simple_value().ok_or_else(|| { Error(ErrorKind::Deserialize(format!( @@ -261,7 +261,7 @@ impl Deserialize for SimpleValue { }) } } -impl Deserialize for SimpleType { +impl FromDhall for SimpleType { fn from_dhall(v: &Value) -> Result { v.to_simple_type().ok_or_else(|| { Error(ErrorKind::Deserialize(format!( -- cgit v1.2.3 From 6dab8cb06e52efdb18b9dcf975e0a2d50454d704 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 5 Apr 2020 15:42:45 +0100 Subject: Document Deserializer methods --- serde_dhall/src/value.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index fee9d73..ea7c20a 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, HashMap}; use dhall::semantics::{Hir, HirKind, Nir, NirKind}; use dhall::syntax::{Builtin, Expr, ExprKind, NumKind, Span}; -use crate::{FromDhall, Error, ErrorKind, Result, Sealed}; +use crate::{Error, ErrorKind, FromDhall, Result, Sealed}; #[doc(hidden)] /// An arbitrary Dhall value. -- cgit v1.2.3 From 678d254a06dbb75f5398abaacee41d1712bf7194 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 5 Apr 2020 15:53:15 +0100 Subject: Make Deserializer functions the only functions --- serde_dhall/src/value.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index ea7c20a..f21e836 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -28,7 +28,7 @@ pub(crate) enum SimpleValue { Union(String, Option>), } -/// The type of a value that can be decoded by Serde, like `{ x: Bool, y: List Natural }`. +/// The type of a value that can be decoded by Serde, e.g. `{ x: Bool, y: List Natural }`. /// /// A `SimpleType` is used when deserializing values to ensure they are of the expected type. /// Rather than letting `serde` handle potential type mismatches, this uses the type-checking @@ -53,7 +53,7 @@ pub(crate) enum SimpleValue { /// use serde_dhall::SimpleType; /// /// let ty: SimpleType = -/// serde_dhall::from_str("{ x: Natural, y: Natural }")?; +/// serde_dhall::from_str("{ x: Natural, y: Natural }").parse()?; /// /// let mut map = HashMap::new(); /// map.insert("x".to_string(), SimpleType::Natural); @@ -74,7 +74,7 @@ pub(crate) enum SimpleValue { /// } /// /// let ty: SimpleType = -/// serde_dhall::from_str("{ x: Bool, y: List Natural }")?; +/// serde_dhall::from_str("{ x: Bool, y: List Natural }").parse()?; /// /// assert_eq!(Foo::static_type(), ty); /// # Ok(()) -- cgit v1.2.3 From c4d9e73126131d31e707822b0fd8b0710363c863 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 5 Apr 2020 17:12:08 +0100 Subject: Final doc tweaks --- serde_dhall/src/value.rs | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index f21e836..7baeee9 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -28,7 +28,7 @@ pub(crate) enum SimpleValue { Union(String, Option>), } -/// The type of a value that can be decoded by Serde, e.g. `{ x: Bool, y: List Natural }`. +/// The type of a value that can be decoded by `serde_dhall`, e.g. `{ x: Bool, y: List Natural }`. /// /// A `SimpleType` is used when deserializing values to ensure they are of the expected type. /// Rather than letting `serde` handle potential type mismatches, this uses the type-checking @@ -36,35 +36,17 @@ pub(crate) enum SimpleValue { /// mismatch happened. /// /// You would typically not manipulate `SimpleType`s by hand but rather let Rust infer it for your -/// datatype using the [`StaticType`] trait, and methods that require it like -/// [`from_file_static_type`] and [`Options::static_type_annotation`]. If you need to supply a -/// `SimpleType` manually however, you can deserialize it like any other Dhall value using the -/// functions provided by this crate. +/// datatype by deriving the [`StaticType`] trait, and using +/// [`Deserializer::static_type_annotation`]. If you need to supply a `SimpleType` manually, you +/// can either deserialize it like any other Dhall value, or construct it manually. /// /// [`StaticType`]: trait.StaticType.html -/// [`from_file_static_type`]: fn.from_file_static_type.html -/// [`Options::static_type_annotation`]: options/struct.Options.html#method.static_type_annotation +/// [`Deserializer::static_type_annotation`]: options/struct.Deserializer.html#method.static_type_annotation /// /// # Examples /// /// ```rust /// # fn main() -> serde_dhall::Result<()> { -/// use std::collections::HashMap; -/// use serde_dhall::SimpleType; -/// -/// let ty: SimpleType = -/// serde_dhall::from_str("{ x: Natural, y: Natural }").parse()?; -/// -/// let mut map = HashMap::new(); -/// map.insert("x".to_string(), SimpleType::Natural); -/// map.insert("y".to_string(), SimpleType::Natural); -/// assert_eq!(ty, SimpleType::Record(map)); -/// # Ok(()) -/// # } -/// ``` -/// -/// ```rust -/// # fn main() -> serde_dhall::Result<()> { /// use serde_dhall::{SimpleType, StaticType}; /// /// #[derive(StaticType)] @@ -80,6 +62,23 @@ pub(crate) enum SimpleValue { /// # Ok(()) /// # } /// ``` +/// +/// ```rust +/// # fn main() -> serde_dhall::Result<()> { +/// use std::collections::HashMap; +/// use serde_dhall::SimpleType; +/// +/// let ty: SimpleType = +/// serde_dhall::from_str("{ x: Natural, y: Natural }").parse()?; +/// +/// let mut map = HashMap::new(); +/// map.insert("x".to_string(), SimpleType::Natural); +/// map.insert("y".to_string(), SimpleType::Natural); +/// assert_eq!(ty, SimpleType::Record(map)); +/// # Ok(()) +/// # } +/// ``` +/// #[derive(Debug, Clone, PartialEq, Eq)] pub enum SimpleType { /// Corresponds to the Dhall type `Bool` -- cgit v1.2.3 From 820214615547101f8f2b5de209b5189968bddfee Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 5 Apr 2020 17:37:15 +0100 Subject: Fix clippy warnings --- serde_dhall/src/value.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'serde_dhall/src/value.rs') diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index 7baeee9..d6631da 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -226,12 +226,12 @@ impl SimpleType { ExprKind::App(hir(ExprKind::Builtin(Builtin::List)), t.to_hir()) } SimpleType::Record(kts) => ExprKind::RecordType( - kts.into_iter() + kts.iter() .map(|(k, t)| (k.as_str().into(), t.to_hir())) .collect(), ), SimpleType::Union(kts) => ExprKind::UnionType( - kts.into_iter() + kts.iter() .map(|(k, t)| { (k.as_str().into(), t.as_ref().map(|t| t.to_hir())) }) -- cgit v1.2.3