From 539ab468dfeca7a7b863d5166b2516a2573c044e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 11 Nov 2019 10:33:45 +0000 Subject: Implement basicest Display for TypeError --- dhall/src/error/mod.rs | 61 +++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 43 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/error/mod.rs b/dhall/src/error/mod.rs index 6d4e120..5338e2d 100644 --- a/dhall/src/error/mod.rs +++ b/dhall/src/error/mod.rs @@ -41,7 +41,7 @@ pub enum EncodeError { /// A structured type error that includes context #[derive(Debug)] pub struct TypeError { - type_message: TypeMessage, + message: TypeMessage, context: TypecheckContext, } @@ -90,58 +90,33 @@ pub(crate) enum TypeMessage { impl TypeError { pub(crate) fn new( context: &TypecheckContext, - type_message: TypeMessage, + message: TypeMessage, ) -> Self { TypeError { context: context.clone(), - type_message, + message, } } } -impl std::error::Error for TypeMessage { - fn description(&self) -> &str { +impl std::fmt::Display for TypeError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { use TypeMessage::*; - match *self { - // UnboundVariable => "Unbound variable", - InvalidInputType(_) => "Invalid function input", - InvalidOutputType(_) => "Invalid function output", - NotAFunction(_) => "Not a function", - TypeMismatch(_, _, _) => "Wrong type of function argument", - _ => "Unhandled error", - } + let msg = match self.message { + UnboundVariable(_) => "Unbound variable".to_string(), + InvalidInputType(_) => "Invalid function input".to_string(), + InvalidOutputType(_) => "Invalid function output".to_string(), + NotAFunction(_) => "Not a function".to_string(), + TypeMismatch(_, _, _) => { + "Wrong type of function argument".to_string() + } + _ => "Unhandled error".to_string(), + }; + write!(f, "{}", msg) } } -impl std::fmt::Display for TypeMessage { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - // UnboundVariable(_) => { - // f.write_str(include_str!("errors/UnboundVariable.txt")) - // } - // TypeMismatch(e0, e1, e2) => { - // let template = include_str!("errors/TypeMismatch.txt"); - // let s = template - // .replace("$txt0", &format!("{}", e0.as_expr())) - // .replace("$txt1", &format!("{}", e1.as_expr())) - // .replace("$txt2", &format!("{}", e2.as_expr())) - // .replace( - // "$txt3", - // &format!( - // "{}", - // e2.get_type() - // .unwrap() - // .as_normalized() - // .unwrap() - // .as_expr() - // ), - // ); - // f.write_str(&s) - // } - _ => f.write_str("Unhandled error message"), - } - } -} +impl std::error::Error for TypeError {} impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { @@ -151,7 +126,7 @@ impl std::fmt::Display for Error { Error::Decode(err) => write!(f, "{:?}", err), Error::Encode(err) => write!(f, "{:?}", err), Error::Resolve(err) => write!(f, "{:?}", err), - Error::Typecheck(err) => write!(f, "{:?}", err), + Error::Typecheck(err) => write!(f, "Type error: {}", err), } } } -- cgit v1.2.3