summaryrefslogtreecommitdiff
path: root/dhall
diff options
context:
space:
mode:
authorNadrieril2019-03-26 22:35:46 +0100
committerNadrieril2019-03-26 22:35:46 +0100
commit23e12ffc4421414abbd089759dab9c50aefeac0c (patch)
treeda1cfb1f2744fd85f1f3f6b25858db99311a315f /dhall
parentfd293b7919d84faa2ac0df05ddd25c0386dc4c67 (diff)
Derive DhallType for structs
Diffstat (limited to 'dhall')
-rw-r--r--dhall/src/dhall_type.rs77
-rw-r--r--dhall/src/lib.rs2
-rw-r--r--dhall/tests/dhall_type.rs37
3 files changed, 106 insertions, 10 deletions
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<Self, DhallConversionError>;
+}
+
+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<A: DhallType, B: DhallType> 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<T: DhallType> DhallType for Option<T> {
+ fn dhall_type() -> DhallExpr {
+ let t = T::dhall_type();
+ dhall_expr!(Optional t)
+ }
+}
+
+impl<T: DhallType> DhallType for Vec<T> {
+ 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<T> DhallType for std::marker::PhantomData<T> {
+ fn dhall_type() -> DhallExpr {
+ dhall_expr!({})
+ }
+}
+
+impl<T: DhallType, E: DhallType> DhallType for Result<T, E> {
+ 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::*;
diff --git a/dhall/tests/dhall_type.rs b/dhall/tests/dhall_type.rs
index cbb71a4..0e27ad0 100644
--- a/dhall/tests/dhall_type.rs
+++ b/dhall/tests/dhall_type.rs
@@ -1,15 +1,32 @@
#![feature(proc_macro_hygiene)]
-use dhall_core::*;
+use dhall::*;
use dhall_generator::*;
-#[derive(DhallType)]
-struct A {
- _field1: bool,
- // field2: Option<bool>,
-}
-
#[test]
-fn test_dhall_type_a() {
- assert_eq!(A::dhall_type(), dhall_expr!(False));
- // assert_eq!(A::dhall_type(), dhall_expr!({ field1: Bool }));
+fn test_dhall_type() {
+ assert_eq!(bool::dhall_type(), dhall_expr!(Bool));
+ assert_eq!(String::dhall_type(), dhall_expr!(Text));
+ assert_eq!(
+ <(bool, Option<String>)>::dhall_type(),
+ dhall_expr!({ _1: Bool, _2: Optional Text })
+ );
+
+ #[derive(DhallType)]
+ #[allow(dead_code)]
+ struct A {
+ field1: bool,
+ field2: Option<bool>,
+ }
+ assert_eq!(
+ A::dhall_type(),
+ dhall_expr!({ field1: Bool, field2: Optional Bool })
+ );
+
+ #[derive(DhallType)]
+ #[allow(dead_code)]
+ struct B<'a, T: 'a> {
+ field1: &'a T,
+ field2: Option<T>,
+ }
+ assert_eq!(<B<'static, bool>>::dhall_type(), A::dhall_type());
}