-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4558 from leon3s/fix-test-with-parse-error
test: replace panic in favor of ParseError
- Loading branch information
Showing
4 changed files
with
150 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/// Represents an error encountered while parsing a test expression | ||
#[derive(Debug)] | ||
pub enum ParseError { | ||
ExpectedValue, | ||
Expected(String), | ||
ExtraArgument(String), | ||
MissingArgument(String), | ||
UnknownOperator(String), | ||
InvalidInteger(String), | ||
} | ||
|
||
/// A Result type for parsing test expressions | ||
pub type ParseResult<T> = Result<T, ParseError>; | ||
|
||
/// Implement Display trait for ParseError to make it easier to print useful errors. | ||
impl std::fmt::Display for ParseError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::Expected(s) => write!(f, "expected {}", s), | ||
Self::ExpectedValue => write!(f, "expected value"), | ||
Self::MissingArgument(s) => write!(f, "missing argument after {}", s), | ||
Self::ExtraArgument(s) => write!(f, "extra argument {}", s), | ||
Self::UnknownOperator(s) => write!(f, "unknown operator {}", s), | ||
Self::InvalidInteger(s) => write!(f, "invalid integer {}", s), | ||
} | ||
} | ||
} | ||
|
||
/// Implement UError trait for ParseError to make it easier to return useful error codes from main(). | ||
impl uucore::error::UError for ParseError { | ||
fn code(&self) -> i32 { | ||
2 | ||
} | ||
} | ||
|
||
/// Implement standard Error trait for UError | ||
impl std::error::Error for ParseError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.