From dae106b3de0888e8a704c0efa3f9d991590f7858 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 9 May 2019 21:54:45 +0200 Subject: Rewrite the StaticType trait and everything around it --- dhall/src/api/mod.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 4 deletions(-) (limited to 'dhall/src/api/mod.rs') diff --git a/dhall/src/api/mod.rs b/dhall/src/api/mod.rs index 0a0ef93..72789d9 100644 --- a/dhall/src/api/mod.rs +++ b/dhall/src/api/mod.rs @@ -1,13 +1,88 @@ mod serde; +pub(crate) mod static_type; pub(crate) mod traits; +// pub struct Value(crate::phase::Normalized); + +pub use typ::Type; + +mod typ { + use crate::core::thunk::{Thunk, TypeThunk}; + use crate::core::value::Value; + use crate::phase::{NormalizedSubExpr, Typed}; + use dhall_syntax::Builtin; + + /// 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 make_builtin_type(b: Builtin) -> Self { + Self::from_value(Value::from_builtin(b)) + } + pub(crate) fn make_optional_type(t: Type) -> Self { + Self::from_value(Value::AppliedBuiltin( + Builtin::Optional, + vec![t.to_thunk()], + )) + } + pub(crate) fn make_list_type(t: Type) -> Self { + Self::from_value(Value::AppliedBuiltin( + Builtin::List, + vec![t.to_thunk()], + )) + } + #[doc(hidden)] + pub fn from_normalized_expr(e: NormalizedSubExpr) -> Self { + Type(Typed::from_normalized_expr_untyped(e)) + } + #[doc(hidden)] + pub fn make_record_type( + kts: impl Iterator, + ) -> Self { + Self::from_value(Value::RecordType( + kts.map(|(k, t)| { + (k.into(), TypeThunk::from_thunk(t.to_thunk())) + }) + .collect(), + )) + } + #[doc(hidden)] + pub fn make_union_type( + kts: impl Iterator)>, + ) -> Self { + Self::from_value(Value::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() + } + } +} + /// Deserialization of Dhall expressions into Rust pub mod de { + pub use super::static_type::StaticType; + pub use super::Type; #[doc(hidden)] - pub use crate::phase::SimpleType; - pub use crate::traits::{Deserialize, SimpleStaticType, StaticType}; + pub use crate::traits::Deserialize; #[doc(hidden)] - pub use dhall_proc_macros::SimpleStaticType; + pub use dhall_proc_macros::StaticType; /// Deserialize an instance of type T from a string of Dhall text. /// @@ -35,6 +110,8 @@ pub mod de { pub fn from_str_auto_type<'a, T: Deserialize<'a> + StaticType>( s: &'a str, ) -> crate::error::Result { - from_str(s, Some(&::get_static_type())) + // from_str(s, Some(&::static_type())) + // TODO + from_str(s, None) } } -- cgit v1.2.3