summaryrefslogtreecommitdiff
path: root/dhall/src/traits/dynamic_type.rs
diff options
context:
space:
mode:
Diffstat (limited to 'dhall/src/traits/dynamic_type.rs')
-rw-r--r--dhall/src/traits/dynamic_type.rs56
1 files changed, 56 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,
+ )),
+ }
+ }
+}