summaryrefslogtreecommitdiff
path: root/dhall/src/traits
diff options
context:
space:
mode:
Diffstat (limited to 'dhall/src/traits')
-rw-r--r--dhall/src/traits/dynamic_type.rs56
-rw-r--r--dhall/src/traits/mod.rs2
2 files changed, 58 insertions, 0 deletions
diff --git a/dhall/src/traits/dynamic_type.rs b/dhall/src/traits/dynamic_type.rs
new file mode 100644
index 0000000..25fe52d
--- /dev/null
+++ b/dhall/src/traits/dynamic_type.rs
@@ -0,0 +1,56 @@
+use crate::expr::*;
+use crate::traits::StaticType;
+use crate::typecheck::{TypeError, TypeMessage};
+use dhall_core::context::Context;
+use dhall_core::{Const, ExprF, X};
+use std::borrow::Cow;
+
+pub trait DynamicType {
+ fn get_type<'a>(&'a self) -> Result<Cow<'a, Type>, TypeError<X>>;
+}
+
+impl<T: StaticType> DynamicType for T {
+ fn get_type<'a>(&'a self) -> Result<Cow<'a, Type>, TypeError<X>> {
+ Ok(Cow::Owned(T::get_static_type()))
+ }
+}
+
+impl DynamicType for Type {
+ fn get_type(&self) -> Result<Cow<'_, Type>, TypeError<X>> {
+ use TypeInternal::*;
+ match &self.0 {
+ Expr(e) => e.get_type(),
+ SuperType => Err(TypeError::new(
+ &Context::new(),
+ dhall_core::rc(ExprF::Const(Const::Sort)),
+ TypeMessage::Untyped,
+ )),
+ }
+ }
+}
+
+impl DynamicType for Normalized {
+ fn get_type(&self) -> Result<Cow<'_, Type>, TypeError<X>> {
+ match &self.1 {
+ Some(t) => Ok(Cow::Borrowed(t)),
+ None => Err(TypeError::new(
+ &Context::new(),
+ self.0.clone(),
+ TypeMessage::Untyped,
+ )),
+ }
+ }
+}
+
+impl DynamicType for Typed {
+ fn get_type(&self) -> Result<Cow<'_, Type>, TypeError<X>> {
+ match &self.1 {
+ Some(t) => Ok(Cow::Borrowed(t)),
+ None => Err(TypeError::new(
+ &Context::new(),
+ self.0.clone(),
+ TypeMessage::Untyped,
+ )),
+ }
+ }
+}
diff --git a/dhall/src/traits/mod.rs b/dhall/src/traits/mod.rs
index 4ce8f97..315e17a 100644
--- a/dhall/src/traits/mod.rs
+++ b/dhall/src/traits/mod.rs
@@ -1,4 +1,6 @@
mod deserialize;
+mod dynamic_type;
mod static_type;
pub use deserialize::Deserialize;
+pub use dynamic_type::DynamicType;
pub use static_type::{SimpleStaticType, StaticType};