From 23e12ffc4421414abbd089759dab9c50aefeac0c Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 26 Mar 2019 22:35:46 +0100 Subject: Derive DhallType for structs --- dhall/src/dhall_type.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ dhall/src/lib.rs | 2 ++ 2 files changed, 79 insertions(+) create mode 100644 dhall/src/dhall_type.rs (limited to 'dhall/src') diff --git a/dhall/src/dhall_type.rs b/dhall/src/dhall_type.rs new file mode 100644 index 0000000..8abef32 --- /dev/null +++ b/dhall/src/dhall_type.rs @@ -0,0 +1,77 @@ +use dhall_core::*; +use dhall_generator::*; + +#[derive(Debug, Clone)] +pub enum DhallConversionError {} + +pub trait DhallType: Sized { + fn dhall_type() -> DhallExpr; + // fn as_dhall(&self) -> DhallExpr; + // fn from_dhall(e: DhallExpr) -> Result; +} + +impl DhallType for bool { + fn dhall_type() -> DhallExpr { + dhall_expr!(Bool) + } +} + +impl DhallType for Natural { + fn dhall_type() -> DhallExpr { + dhall_expr!(Natural) + } +} + +impl DhallType for Integer { + fn dhall_type() -> DhallExpr { + dhall_expr!(Integer) + } +} + +impl DhallType for String { + fn dhall_type() -> DhallExpr { + dhall_expr!(Text) + } +} + +impl DhallType for (A, B) { + fn dhall_type() -> DhallExpr { + let ta = A::dhall_type(); + let tb = B::dhall_type(); + dhall_expr!({ _1: ta, _2: tb }) + } +} + +impl DhallType for Option { + fn dhall_type() -> DhallExpr { + let t = T::dhall_type(); + dhall_expr!(Optional t) + } +} + +impl DhallType for Vec { + fn dhall_type() -> DhallExpr { + let t = T::dhall_type(); + dhall_expr!(List t) + } +} + +impl<'a, T: DhallType> DhallType for &'a T { + fn dhall_type() -> DhallExpr { + T::dhall_type() + } +} + +impl DhallType for std::marker::PhantomData { + fn dhall_type() -> DhallExpr { + dhall_expr!({}) + } +} + +impl DhallType for Result { + fn dhall_type() -> DhallExpr { + let tt = T::dhall_type(); + let te = E::dhall_type(); + dhall_expr!(< Ok: tt | Err: te>) + } +} diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index d9f9edb..c0c1d6f 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -10,8 +10,10 @@ mod normalize; pub use crate::normalize::*; pub mod binary; +mod dhall_type; pub mod imports; pub mod typecheck; +pub use dhall_type::*; pub use crate::imports::*; -- cgit v1.2.3