use crate::expr::*; use crate::traits::StaticType; use crate::typecheck::{TypeError, TypeMessage}; use dhall_core::context::Context; use dhall_core::{Const, ExprF}; use std::borrow::Cow; pub trait DynamicType { fn get_type<'a>(&'a self) -> Result>, TypeError>; } impl DynamicType for T { fn get_type<'a>(&'a self) -> Result>, TypeError> { Ok(Cow::Owned(T::get_static_type())) } } impl<'a> DynamicType for Type<'a> { fn get_type(&self) -> Result>, TypeError> { 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<'a> DynamicType for Normalized<'a> { fn get_type(&self) -> Result>, TypeError> { match &self.1 { Some(t) => Ok(Cow::Borrowed(t)), None => Err(TypeError::new( &Context::new(), self.0.absurd(), TypeMessage::Untyped, )), } } } impl<'a> DynamicType for Typed<'a> { fn get_type(&self) -> Result>, TypeError> { match &self.1 { Some(t) => Ok(Cow::Borrowed(t)), None => Err(TypeError::new( &Context::new(), self.0.clone(), TypeMessage::Untyped, )), } } }