Skip to content

Commit

Permalink
Merge pull request #4409 from harupy/improve-error-conversion-in-stri…
Browse files Browse the repository at this point in the history
…ng-parser

Improve error conversion in `string_parsers.rs`
  • Loading branch information
youknowone committed Jan 3, 2023
2 parents 71ba422 + 3b0fd61 commit 2858c31
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 93 deletions.
30 changes: 21 additions & 9 deletions parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 2858c31

Please sign in to comment.