diff --git a/schala-lang/src/error.rs b/schala-lang/src/error.rs index 795aeec..442daa5 100644 --- a/schala-lang/src/error.rs +++ b/schala-lang/src/error.rs @@ -7,24 +7,20 @@ use crate::{ pub struct SchalaError { errors: Vec, - //TODO unify these sometime - formatted_parse_error: Option, } impl SchalaError { pub(crate) fn display(&self) -> String { - if let Some(ref err) = self.formatted_parse_error { - err.clone() - } else { - self.errors[0].text.as_ref().cloned().unwrap_or_default() + match self.errors[0] { + Error::Parse(ref parse_err) => parse_err.to_string(), + Error::Standard { ref text, .. } => text.as_ref().cloned().unwrap_or_default(), } } #[allow(dead_code)] pub(crate) fn from_type_error(err: TypeError) -> Self { Self { - formatted_parse_error: None, - errors: vec![Error { location: None, text: Some(err.msg), stage: Stage::Typechecking }], + errors: vec![Error::Standard { location: None, text: Some(err.msg), stage: Stage::Typechecking }], } } @@ -32,32 +28,29 @@ impl SchalaError { //TODO this could be better let errors = symbol_errs .into_iter() - .map(|_symbol_err| Error { + .map(|_symbol_err| Error::Standard { location: None, text: Some("symbol table error".to_string()), stage: Stage::Symbols, }) .collect(); - Self { errors, formatted_parse_error: None } + Self { errors } } pub(crate) fn from_string(text: String, stage: Stage) -> Self { - Self { formatted_parse_error: None, errors: vec![Error { location: None, text: Some(text), stage }] } + Self { errors: vec![Error::Standard { location: None, text: Some(text), stage }] } } pub(crate) fn from_parse_error(parse_error: ParseError, source_reference: &SourceReference) -> Self { - Self { - formatted_parse_error: Some(format_parse_error(parse_error, source_reference)), - errors: vec![], - } + let formatted_parse_error = format_parse_error(parse_error, source_reference); + Self { errors: vec![Error::Parse(formatted_parse_error)] } } } #[allow(dead_code)] -struct Error { - location: Option, - text: Option, - stage: Stage, +enum Error { + Standard { location: Option, text: Option, stage: Stage }, + Parse(String), } fn format_parse_error(error: ParseError, source_reference: &SourceReference) -> String {