Improve error conversion in string_parsers.rs

This commit is contained in:
harupy 2023-01-03 12:46:10 +09:00
parent 4222b13e6c
commit 9030679193
2 changed files with 116 additions and 89 deletions

View file

@ -12,6 +12,12 @@ pub struct LexicalError {
pub location: Location,
}
impl LexicalError {
pub fn new(error: LexicalErrorType, location: Location) -> Self {
Self { error, location }
}
}
#[derive(Debug, PartialEq)]
pub enum LexicalErrorType {
StringError,
@ -85,6 +91,21 @@ pub struct FStringError {
pub location: Location,
}
impl FStringError {
pub fn new(error: FStringErrorType, location: Location) -> Self {
Self { error, location }
}
}
impl From<FStringError> for LexicalError {
fn from(err: FStringError) -> Self {
LexicalError {
error: LexicalErrorType::FStringError(err.error),
location: err.location,
}
}
}
#[derive(Debug, PartialEq)]
pub enum FStringErrorType {
UnclosedLbrace,
@ -101,15 +122,6 @@ pub enum FStringErrorType {
UnterminatedString,
}
impl FStringErrorType {
pub fn to_lexical_error(self, location: Location) -> LexicalError {
LexicalError {
error: LexicalErrorType::FStringError(self),
location,
}
}
}
impl fmt::Display for FStringErrorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {