diff --git a/Cargo.lock b/Cargo.lock index ee3b1c4..1a9c930 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -78,6 +78,7 @@ dependencies = [ "regex", "serde", "serde_json", + "thiserror", "unicase", ] @@ -123,6 +124,26 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "thiserror" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicase" version = "2.6.0" diff --git a/Cargo.toml b/Cargo.toml index ac39a98..e764ef3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [] \ No newline at end of file +default = [] diff --git a/src/comparator.rs b/src/comparator.rs index 52cfb64..0e2c831 100644 --- a/src/comparator.rs +++ b/src/comparator.rs @@ -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, @@ -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) { diff --git a/src/error.rs b/src/error.rs index 7c2c224..35eb930 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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 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), } diff --git a/src/lib.rs b/src/lib.rs index 6064087..4e96d71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/range.rs b/src/range.rs index 87447e2..9299c5a 100644 --- a/src/range.rs +++ b/src/range.rs @@ -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, @@ -129,7 +129,7 @@ impl<'p> Parseable<'p> for Range { let comparators: Vec> = 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 }) }