diff options
author | Nadrieril | 2019-08-13 15:25:53 +0200 |
---|---|---|
committer | Nadrieril | 2019-08-13 15:31:15 +0200 |
commit | 1ea8b10051d29c634399304273d6ee565d039bc2 (patch) | |
tree | 78750a34b6dd54f71bdff8fd835fc705a9ec2582 /serde_dhall | |
parent | 74bb40e88c71b80ab785f07fd19da22404ab6e95 (diff) |
Merge `Type` and `Value` in serde_dhall
There was no point in separating them
Diffstat (limited to 'serde_dhall')
-rw-r--r-- | serde_dhall/src/lib.rs | 87 | ||||
-rw-r--r-- | serde_dhall/src/static_type.rs | 26 | ||||
-rw-r--r-- | serde_dhall/tests/traits.rs | 4 |
3 files changed, 47 insertions, 70 deletions
diff --git a/serde_dhall/src/lib.rs b/serde_dhall/src/lib.rs index f8f9ae9..8e1af82 100644 --- a/serde_dhall/src/lib.rs +++ b/serde_dhall/src/lib.rs @@ -106,25 +106,29 @@ //! [serde::Deserialize]: https://docs.serde.rs/serde/trait.Deserialize.html mod serde; -pub(crate) mod static_type; +mod static_type; pub use value::Value; mod value { + use dhall::core::thunk::{Thunk, TypeThunk}; + use dhall::core::value::Value as DhallValue; + use dhall::phase::{NormalizedSubExpr, Parsed, Type, Typed}; + use dhall_syntax::Builtin; + use super::de::{Error, Result}; - use super::Type; - use dhall::phase::{NormalizedSubExpr, Parsed, Typed}; - // A Dhall value + /// A Dhall value + #[derive(Debug, Clone, PartialEq, Eq)] pub struct Value(Typed); impl Value { - pub fn from_str(s: &str, ty: Option<&Type>) -> Result<Self> { + pub fn from_str(s: &str, ty: Option<&Value>) -> Result<Self> { Value::from_str_using_dhall_error_type(s, ty).map_err(Error::Dhall) } fn from_str_using_dhall_error_type( s: &str, - ty: Option<&Type>, + ty: Option<&Value>, ) -> dhall::error::Result<Self> { let resolved = Parsed::parse_str(s)?.resolve()?; let typed = match ty { @@ -136,53 +140,37 @@ mod value { pub(crate) fn to_expr(&self) -> NormalizedSubExpr { self.0.to_expr() } - pub(crate) fn to_typed(&self) -> Typed { - self.0.clone() + pub(crate) fn to_thunk(&self) -> Thunk { + self.0.to_thunk() + } + pub(crate) fn to_type(&self) -> Type { + self.0.to_type() } - } -} - -pub use typ::Type; - -mod typ { - use dhall::core::thunk::{Thunk, TypeThunk}; - use dhall::core::value::Value; - use dhall::phase::{NormalizedSubExpr, Typed}; - use dhall_syntax::Builtin; - - use super::de::Result; - - /// A Dhall expression representing a type. - /// - /// This captures what is usually simply called a "type", like - /// `Bool`, `{ x: Integer }` or `Natural -> Text`. - #[derive(Debug, Clone, PartialEq, Eq)] - pub struct Type(Typed); - impl Type { - pub(crate) fn from_value(v: Value) -> Self { - Type(Typed::from_value_untyped(v)) + pub(crate) fn from_dhall_value(v: DhallValue) -> Self { + Value(Typed::from_value_untyped(v)) } pub(crate) fn make_builtin_type(b: Builtin) -> Self { - Self::from_value(Value::from_builtin(b)) + Self::from_dhall_value(DhallValue::from_builtin(b)) } - pub(crate) fn make_optional_type(t: Type) -> Self { - Self::from_value(Value::AppliedBuiltin( + pub(crate) fn make_optional_type(t: Value) -> Self { + Self::from_dhall_value(DhallValue::AppliedBuiltin( Builtin::Optional, vec![t.to_thunk()], )) } - pub(crate) fn make_list_type(t: Type) -> Self { - Self::from_value(Value::AppliedBuiltin( + pub(crate) fn make_list_type(t: Value) -> Self { + Self::from_dhall_value(DhallValue::AppliedBuiltin( Builtin::List, vec![t.to_thunk()], )) } + // Made public for the StaticType derive macro #[doc(hidden)] pub fn make_record_type( - kts: impl Iterator<Item = (String, Type)>, + kts: impl Iterator<Item = (String, Value)>, ) -> Self { - Self::from_value(Value::RecordType( + Self::from_dhall_value(DhallValue::RecordType( kts.map(|(k, t)| { (k.into(), TypeThunk::from_thunk(t.to_thunk())) }) @@ -191,31 +179,20 @@ mod typ { } #[doc(hidden)] pub fn make_union_type( - kts: impl Iterator<Item = (String, Option<Type>)>, + kts: impl Iterator<Item = (String, Option<Value>)>, ) -> Self { - Self::from_value(Value::UnionType( + Self::from_dhall_value(DhallValue::UnionType( kts.map(|(k, t)| { (k.into(), t.map(|t| TypeThunk::from_thunk(t.to_thunk()))) }) .collect(), )) } - - pub(crate) fn to_thunk(&self) -> Thunk { - self.0.to_thunk() - } - #[allow(dead_code)] - pub(crate) fn to_expr(&self) -> NormalizedSubExpr { - self.0.to_expr() - } - pub(crate) fn to_type(&self) -> dhall::phase::Type { - self.0.to_type() - } } - impl super::de::Deserialize for Type { - fn from_dhall(v: &super::Value) -> Result<Self> { - Ok(Type(v.to_typed())) + impl super::de::Deserialize for Value { + fn from_dhall(v: &Value) -> Result<Self> { + Ok(v.clone()) } } } @@ -260,7 +237,7 @@ pub mod de { pub use super::error::{Error, Result}; pub use super::static_type::StaticType; - pub use super::{Type, Value}; + pub use super::Value; /// A data structure that can be deserialized from a Dhall expression /// @@ -283,7 +260,7 @@ pub mod de { /// /// If a type is provided, this additionally checks that the provided /// expression has that type. - pub fn from_str<T>(s: &str, ty: Option<&Type>) -> Result<T> + pub fn from_str<T>(s: &str, ty: Option<&Value>) -> Result<T> where T: Deserialize, { diff --git a/serde_dhall/src/static_type.rs b/serde_dhall/src/static_type.rs index 13d5d70..67a7bc4 100644 --- a/serde_dhall/src/static_type.rs +++ b/serde_dhall/src/static_type.rs @@ -1,6 +1,6 @@ use dhall_syntax::{Builtin, Integer, Natural}; -use crate::Type; +use crate::Value; /// A Rust type that can be represented as a Dhall type. /// @@ -14,14 +14,14 @@ use crate::Type; /// [StaticType] because each different value would /// have a different Dhall record type. pub trait StaticType { - fn static_type() -> Type; + fn static_type() -> Value; } macro_rules! derive_builtin { ($ty:ty, $builtin:ident) => { impl StaticType for $ty { - fn static_type() -> Type { - Type::make_builtin_type(Builtin::$builtin) + fn static_type() -> Value { + Value::make_builtin_type(Builtin::$builtin) } } }; @@ -38,8 +38,8 @@ where A: StaticType, B: StaticType, { - fn static_type() -> Type { - Type::make_record_type( + fn static_type() -> Value { + Value::make_record_type( vec![ ("_1".to_owned(), A::static_type()), ("_2".to_owned(), B::static_type()), @@ -54,8 +54,8 @@ where T: StaticType, E: StaticType, { - fn static_type() -> Type { - Type::make_union_type( + fn static_type() -> Value { + Value::make_union_type( vec![ ("Ok".to_owned(), Some(T::static_type())), ("Err".to_owned(), Some(E::static_type())), @@ -69,8 +69,8 @@ impl<T> StaticType for Option<T> where T: StaticType, { - fn static_type() -> Type { - Type::make_optional_type(T::static_type()) + fn static_type() -> Value { + Value::make_optional_type(T::static_type()) } } @@ -78,8 +78,8 @@ impl<T> StaticType for Vec<T> where T: StaticType, { - fn static_type() -> Type { - Type::make_list_type(T::static_type()) + fn static_type() -> Value { + Value::make_list_type(T::static_type()) } } @@ -87,7 +87,7 @@ impl<'a, T> StaticType for &'a T where T: StaticType, { - fn static_type() -> Type { + fn static_type() -> Value { T::static_type() } } diff --git a/serde_dhall/tests/traits.rs b/serde_dhall/tests/traits.rs index 99f1109..40ac1d7 100644 --- a/serde_dhall/tests/traits.rs +++ b/serde_dhall/tests/traits.rs @@ -1,9 +1,9 @@ #![feature(proc_macro_hygiene)] -use serde_dhall::de::{from_str, StaticType, Type}; +use serde_dhall::de::{from_str, StaticType, Value}; #[test] fn test_static_type() { - fn parse(s: &str) -> Type { + fn parse(s: &str) -> Value { from_str(s, None).unwrap() } |