From 51dcf19eb6f8d2127f3dedfc1d7d5326d7a8017b Mon Sep 17 00:00:00 2001 From: Greg Brown Date: Tue, 27 Apr 2021 13:07:27 +0100 Subject: Start rewrite using query system. --- src/chomp/ast-old/error.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/chomp/ast-old/error.rs (limited to 'src/chomp/ast-old/error.rs') diff --git a/src/chomp/ast-old/error.rs b/src/chomp/ast-old/error.rs new file mode 100644 index 0000000..9057ec3 --- /dev/null +++ b/src/chomp/ast-old/error.rs @@ -0,0 +1,52 @@ +use super::{Expression, Lambda, NamedExpression}; +use proc_macro2::Span; +use std::{ + error::Error, + fmt::{self, Display}, +}; + +#[derive(Debug)] +pub enum TranslateError { + CallNotAFunction { + on: Expression, + span: Option, + }, + WrongArgCount { + lambda: Lambda, + args: Vec, + span: Option, + }, +} + +impl From for syn::Error { + fn from(e: TranslateError) -> Self { + let msg = e.to_string(); + let span = match e { + TranslateError::CallNotAFunction { span, .. } + | TranslateError::WrongArgCount { span, .. } => span, + }; + + Self::new(span.unwrap_or_else(Span::call_site), msg) + } +} + +impl Display for TranslateError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::CallNotAFunction { .. } => { + write!(f, "call expected a function") + } + Self::WrongArgCount { lambda, args, .. } => match (lambda.args.len(), args.len()) { + (1, n) => write!(f, "this function takes 1 argument but {} were supplied", n), + (m, _) => write!(f, "this function takes {} arguments but 1 was supplied", m), + (m, n) => write!( + f, + "this function takes {} arguments but {} were supplied", + m, n + ), + }, + } + } +} + +impl Error for TranslateError {} -- cgit v1.2.3