diff options
author | Nadrieril | 2019-04-11 13:14:13 +0200 |
---|---|---|
committer | Nadrieril | 2019-04-11 13:14:13 +0200 |
commit | d3f14966120fae672dc890b718ebada74ebed533 (patch) | |
tree | 887b4af84bc2c69886616a4a9c8aa27fa846238a /dhall/src/traits/static_type.rs | |
parent | 982f86c34f69bb78b45a4b8b37a5bf5731f881eb (diff) | |
parent | 82d62c4d7d423817a4fd9d6294d27d18d60bcd22 (diff) |
Merge branch 'serde'
Diffstat (limited to 'dhall/src/traits/static_type.rs')
-rw-r--r-- | dhall/src/traits/static_type.rs | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/dhall/src/traits/static_type.rs b/dhall/src/traits/static_type.rs new file mode 100644 index 0000000..6c41e3f --- /dev/null +++ b/dhall/src/traits/static_type.rs @@ -0,0 +1,128 @@ +use crate::expr::*; +use dhall_core::*; +use dhall_generator::*; + +pub trait StaticType { + fn get_static_type() -> Type; +} + +/// Trait for rust types that can be represented in dhall in +/// a single way, independent of the value. A typical example is `Option<bool>`, +/// represented by the dhall expression `Optional Bool`. A typical counterexample +/// is `HashMap<Text, bool>` because dhall cannot represent records with a +/// variable number of fields. +pub trait SimpleStaticType { + fn get_simple_static_type() -> SimpleType; +} + +fn mktype(x: SubExpr<X, X>) -> SimpleType { + SimpleType(x) +} + +impl<T: SimpleStaticType> StaticType for T { + fn get_static_type() -> Type { + crate::expr::Normalized( + T::get_simple_static_type().into(), + Type::const_type(), + ) + .into_type() + } +} + +impl StaticType for SimpleType { + fn get_static_type() -> Type { + Type::const_type() + } +} + +impl SimpleStaticType for bool { + fn get_simple_static_type() -> SimpleType { + mktype(dhall_expr!(Bool)) + } +} + +impl SimpleStaticType for Natural { + fn get_simple_static_type() -> SimpleType { + mktype(dhall_expr!(Natural)) + } +} + +impl SimpleStaticType for u32 { + fn get_simple_static_type() -> SimpleType { + mktype(dhall_expr!(Natural)) + } +} + +impl SimpleStaticType for u64 { + fn get_simple_static_type() -> SimpleType { + mktype(dhall_expr!(Natural)) + } +} + +impl SimpleStaticType for Integer { + fn get_simple_static_type() -> SimpleType { + mktype(dhall_expr!(Integer)) + } +} + +impl SimpleStaticType for i32 { + fn get_simple_static_type() -> SimpleType { + mktype(dhall_expr!(Integer)) + } +} + +impl SimpleStaticType for i64 { + fn get_simple_static_type() -> SimpleType { + mktype(dhall_expr!(Integer)) + } +} + +impl SimpleStaticType for String { + fn get_simple_static_type() -> SimpleType { + mktype(dhall_expr!(Text)) + } +} + +impl<A: SimpleStaticType, B: SimpleStaticType> SimpleStaticType for (A, B) { + fn get_simple_static_type() -> SimpleType { + let ta: SubExpr<_, _> = A::get_simple_static_type().into(); + let tb: SubExpr<_, _> = B::get_simple_static_type().into(); + mktype(dhall_expr!({ _1: ta, _2: tb })) + } +} + +impl<T: SimpleStaticType> SimpleStaticType for Option<T> { + fn get_simple_static_type() -> SimpleType { + let t: SubExpr<_, _> = T::get_simple_static_type().into(); + mktype(dhall_expr!(Optional t)) + } +} + +impl<T: SimpleStaticType> SimpleStaticType for Vec<T> { + fn get_simple_static_type() -> SimpleType { + let t: SubExpr<_, _> = T::get_simple_static_type().into(); + mktype(dhall_expr!(List t)) + } +} + +impl<'a, T: SimpleStaticType> SimpleStaticType for &'a T { + fn get_simple_static_type() -> SimpleType { + T::get_simple_static_type() + } +} + +impl<T> SimpleStaticType for std::marker::PhantomData<T> { + fn get_simple_static_type() -> SimpleType { + mktype(dhall_expr!({})) + } +} + +impl<T: SimpleStaticType, E: SimpleStaticType> SimpleStaticType + for std::result::Result<T, E> +{ + fn get_simple_static_type() -> SimpleType { + let tt: SubExpr<_, _> = T::get_simple_static_type().into(); + let te: SubExpr<_, _> = E::get_simple_static_type().into(); + mktype(dhall_expr!(< Ok: tt | Err: te>)) + } +} |