-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
thiserror
instead of manually implementing Error
- Loading branch information
1 parent
2cd8012
commit cedfba2
Showing
6 changed files
with
35 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,58 +1,12 @@ | ||
use std::error::Error as StdError; | ||
use std::fmt; | ||
use std::num::ParseIntError; | ||
|
||
/// An error returned during parsing of [Versions](crate::Version) or [Ranges](crate::Range). | ||
/// Use the [kind](Error::kind) method to get the type of error. | ||
#[derive(Debug)] | ||
pub struct Error { | ||
_kind: ErrorKind, | ||
} | ||
|
||
impl Error { | ||
/// Construct a new error from a specific [kind](Error::kind) | ||
pub fn new(kind: ErrorKind) -> Self { | ||
Error { _kind: kind } | ||
} | ||
|
||
/// Get the kind of the error | ||
pub fn kind(&self) -> ErrorKind { | ||
self._kind.clone() | ||
} | ||
} | ||
|
||
impl StdError for Error { | ||
fn source(&self) -> Option<&(dyn StdError + 'static)> { | ||
None | ||
} | ||
} | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
ParseInt(#[from] std::num::ParseIntError), | ||
|
||
impl From<ParseIntError> for Error { | ||
fn from(e: ParseIntError) -> Self { | ||
Error::new(ErrorKind::ParseInt(e)) | ||
} | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}", self.kind()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum ErrorKind { | ||
#[error("invalid comparator: {0}")] | ||
InvalidComparator(String), | ||
InvalidRange(String), | ||
ParseInt(ParseIntError), | ||
} | ||
|
||
impl fmt::Display for ErrorKind { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let msg = match self { | ||
ErrorKind::InvalidComparator(comp) => format!("Invalid comparator {}", comp), | ||
ErrorKind::InvalidRange(range) => format!("Invalid range {}", range), | ||
ErrorKind::ParseInt(e) => format!("{}", e), | ||
}; | ||
write!(f, "{}", msg) | ||
} | ||
#[error("invalid range: {0}")] | ||
InvalidRange(String), | ||
} |
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
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