Skip to content

Commit

Permalink
Use thiserror instead of manually implementing Error
Browse files Browse the repository at this point in the history
  • Loading branch information
gngeorgiev committed Nov 21, 2021
1 parent 2cd8012 commit cedfba2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 59 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ lazy_static = "1.4.0"
regex = "1.5.4"
unicase = "2.6.0"
serde = { version = "1.0.130", features = ["derive"], optional = true }
thiserror = "1.0.30"

[dev_dependencies]
serde_json = "1.0"

[features]
default = []
default = []
4 changes: 2 additions & 2 deletions src/comparator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::builder::Options;
use crate::error::{Error, ErrorKind};
use crate::error::Error;
use crate::expressions::{
COMPARATOR, COMPARATOR_LOOSE, COMP_REPLACE_CARETS, COMP_REPLACE_CARETS_LOOSE,
COMP_REPLACE_STARS, COMP_REPLACE_TILDES, COMP_REPLACE_TILDES_LOOSE, COMP_REPLACE_XRANGES,
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Comparator {
};
let cap = match cap {
Some(cap) => cap,
None => return Err(Error::new(ErrorKind::InvalidComparator(comp.clone()))),
None => return Err(Error::InvalidComparator(comp)),
};

let operator = match cap.get(1) {
Expand Down
60 changes: 7 additions & 53 deletions src/error.rs
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),
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mod version;

pub use builder::{Builder, Options, OptionsBuilder, Parseable};
pub use compare_fns::*;
pub use error::{Error, ErrorKind};
pub use error::Error;
pub use operator::Operator;
pub use range::Range;
pub use version::Version;
Expand Down
4 changes: 2 additions & 2 deletions src/range.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::builder::{Builder, Options, Parseable};
use crate::comparator::{Comparator, ComparatorPair};
use crate::error::{Error, ErrorKind};
use crate::error::Error;
use crate::expressions::{
COMPARATOR_LOOSE, COMP_REPLACE_CARETS, RANGE_HYPHEN, RANGE_HYPHEN_LOOSE, RANGE_OR,
RANGE_TRIM_CARET, RANGE_TRIM_OPERATORS, RANGE_TRIM_TILDE, SPLIT_SPACES,
Expand Down Expand Up @@ -129,7 +129,7 @@ impl<'p> Parseable<'p> for Range {
let comparators: Vec<Vec<Comparator>> = comparators_result?.into_iter().flatten().collect();

if comparators.is_empty() {
Err(Error::new(ErrorKind::InvalidRange(range_input.to_owned())))
Err(Error::InvalidRange(range_input.into()))
} else {
Ok(Range { comparators, opts })
}
Expand Down

0 comments on commit cedfba2

Please sign in to comment.