From 60425d58151fef142b066d523dc4d5e832070b9c Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 14:30:15 +0000 Subject: Add new Value type in API --- serde_dhall/src/lib.rs | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) (limited to 'serde_dhall/src/lib.rs') diff --git a/serde_dhall/src/lib.rs b/serde_dhall/src/lib.rs index d019238..1f7ecf8 100644 --- a/serde_dhall/src/lib.rs +++ b/serde_dhall/src/lib.rs @@ -183,38 +183,26 @@ pub use value::Value; // A Dhall value. #[doc(hidden)] pub mod value { - use dhall::{Normalized, NormalizedExpr, Parsed, SimpleType, SimpleValue}; + use dhall::{SimpleType, SimpleValue}; use super::de::Error; use super::Type; /// A Dhall value #[derive(Debug, Clone, PartialEq, Eq)] - pub struct Value(pub(crate) Normalized); + pub struct Value(pub(crate) dhall::Value); impl Value { pub fn from_str(s: &str, ty: Option<&Type>) -> super::de::Result { - Value::from_str_using_dhall_error_type(s, ty).map_err(Error::Dhall) + let ty = ty.map(|t| t.to_dhall_value()); + let val = dhall::Value::from_str_with_annot(s, ty.as_ref()) + .map_err(Error::Dhall)?; + Ok(Value(val)) } - fn from_str_using_dhall_error_type( - s: &str, - ty: Option<&Type>, - ) -> dhall::error::Result { - let resolved = Parsed::parse_str(s)?.resolve()?; - let typed = match ty { - None => resolved.typecheck()?, - Some(t) => resolved.typecheck_with(&t.to_normalized())?, - }; - Ok(Value(typed.normalize())) - } - pub(crate) fn to_simple_value( - &self, - ) -> Result { + pub(crate) fn to_simple_value(&self) -> Option { self.0.to_simple_value() } - pub(crate) fn to_simple_type( - &self, - ) -> Result { + pub(crate) fn to_simple_type(&self) -> Option { self.0.to_simple_type() } } @@ -226,12 +214,21 @@ pub mod value { Ok(v.clone()) } } + + impl std::fmt::Display for Value { + fn fmt( + &self, + f: &mut std::fmt::Formatter, + ) -> Result<(), std::fmt::Error> { + self.0.fmt(f) + } + } } // A Dhall type. #[doc(hidden)] pub mod ty { - use dhall::{Normalized, STyKind, SimpleType}; + use dhall::{STyKind, SimpleType}; use super::de::Error; @@ -240,8 +237,8 @@ pub mod ty { pub struct Type(SimpleType); impl Type { - pub(crate) fn to_normalized(&self) -> Normalized { - self.0.clone().into_normalized() + pub(crate) fn to_dhall_value(&self) -> dhall::Value { + self.0.clone().into_normalized().to_value() } pub(crate) fn from_simple_type(ty: SimpleType) -> Self { @@ -279,10 +276,10 @@ pub mod ty { impl super::de::Deserialize for Type { fn from_dhall(v: &super::Value) -> super::de::Result { - let sty = v.to_simple_type().map_err(|expr| { + let sty = v.to_simple_type().ok_or_else(|| { Error::Deserialize(format!( "this cannot be deserialized into a simple type: {}", - expr + v )) })?; Ok(Type(sty)) -- cgit v1.2.3