Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove error chain (new) #65

Merged
merged 3 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ name = "cron"
[dependencies]
chrono = "~0.4"
nom = "~4.1"
error-chain="~0.10.0"

[dev-dependencies]
chrono-tz = "0.4"
chrono-tz = "0.4"
29 changes: 24 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
error_chain! {
errors {
Expression(s: String) {
description("invalid expression")
display("invalid expression: {}", s)
use std::{error, fmt};

#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
}

#[derive(Debug)]
pub enum ErrorKind {
Expression(String),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.kind {
ErrorKind::Expression(ref expr) => write!(f, "Invalid expression: {}", expr),
}
}
}

impl error::Error for Error {}

impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error { kind }
}
}
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
//! */
//! ```

#[macro_use]
extern crate error_chain;

#[cfg(test)]
extern crate chrono_tz;

Expand Down
7 changes: 4 additions & 3 deletions src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,12 @@ impl Schedule {
fn from_field_list(fields: Vec<Field>) -> Result<Schedule, Error> {
let number_of_fields = fields.len();
if number_of_fields != 6 && number_of_fields != 7 {
bail!(ErrorKind::Expression(format!(
return Err(ErrorKind::Expression(format!(
"Expression has {} fields. Valid cron \
expressions have 6 or 7.",
number_of_fields
)));
))
.into());
}

let mut iter = fields.into_iter();
Expand Down Expand Up @@ -328,7 +329,7 @@ impl FromStr for Schedule {
schedule.source.replace(expression.to_owned());
Ok(schedule)
} // Extract from nom tuple
Err(_) => bail!(ErrorKind::Expression("Invalid cron expression.".to_owned())), //TODO: Details
Err(_) => Err(ErrorKind::Expression("Invalid cron expression.".to_owned()).into()), //TODO: Details
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/time_unit/days_of_week.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl TimeUnitField for DaysOfWeek {
fn inclusive_max() -> Ordinal {
7
}
fn ordinal_from_name(name: &str) -> Result<Ordinal> {
fn ordinal_from_name(name: &str) -> Result<Ordinal, Error> {
//TODO: Use phf crate
let ordinal = match name.to_lowercase().as_ref() {
"sun" | "sunday" => 1,
Expand All @@ -29,10 +29,13 @@ impl TimeUnitField for DaysOfWeek {
"thu" | "thurs" | "thursday" => 5,
"fri" | "friday" => 6,
"sat" | "saturday" => 7,
_ => bail!(ErrorKind::Expression(format!(
"'{}' is not a valid day of the week.",
name
))),
_ => {
return Err(ErrorKind::Expression(format!(
"'{}' is not a valid day of the week.",
name
))
.into())
}
};
Ok(ordinal)
}
Expand Down
31 changes: 18 additions & 13 deletions src/time_unit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,35 +188,38 @@ where
fn all() -> Self {
Self::from_ordinal_set(Self::supported_ordinals())
}
fn ordinal_from_name(name: &str) -> Result<Ordinal> {
bail!(ErrorKind::Expression(format!(
fn ordinal_from_name(name: &str) -> Result<Ordinal, Error> {
Err(ErrorKind::Expression(format!(
"The '{}' field does not support using names. '{}' \
specified.",
Self::name(),
name
)))
))
.into())
}
fn validate_ordinal(ordinal: Ordinal) -> Result<Ordinal> {
fn validate_ordinal(ordinal: Ordinal) -> Result<Ordinal, Error> {
//println!("validate_ordinal for {} => {}", Self::name(), ordinal);
match ordinal {
i if i < Self::inclusive_min() => bail!(ErrorKind::Expression(format!(
i if i < Self::inclusive_min() => Err(ErrorKind::Expression(format!(
"{} must be greater than or equal to {}. ('{}' \
specified.)",
Self::name(),
Self::inclusive_min(),
i
))),
i if i > Self::inclusive_max() => bail!(ErrorKind::Expression(format!(
))
.into()),
i if i > Self::inclusive_max() => Err(ErrorKind::Expression(format!(
"{} must be less than {}. ('{}' specified.)",
Self::name(),
Self::inclusive_max(),
i
))),
))
.into()),
i => Ok(i),
}
}

fn ordinals_from_specifier(specifier: &Specifier) -> Result<OrdinalSet> {
fn ordinals_from_specifier(specifier: &Specifier) -> Result<OrdinalSet, Error> {
use self::Specifier::*;
//println!("ordinals_from_specifier for {} => {:?}", Self::name(), specifier);
match *specifier {
Expand All @@ -235,25 +238,27 @@ where
Range(start, end) => {
match (Self::validate_ordinal(start), Self::validate_ordinal(end)) {
(Ok(start), Ok(end)) if start <= end => Ok((start..end + 1).collect()),
_ => bail!(ErrorKind::Expression(format!(
_ => Err(ErrorKind::Expression(format!(
"Invalid range for {}: {}-{}",
Self::name(),
start,
end
))),
))
.into()),
}
}
NamedRange(ref start_name, ref end_name) => {
let start = Self::ordinal_from_name(start_name)?;
let end = Self::ordinal_from_name(end_name)?;
match (Self::validate_ordinal(start), Self::validate_ordinal(end)) {
(Ok(start), Ok(end)) if start <= end => Ok((start..end + 1).collect()),
_ => bail!(ErrorKind::Expression(format!(
_ => Err(ErrorKind::Expression(format!(
"Invalid named range for {}: {}-{}",
Self::name(),
start_name,
end_name
))),
))
.into()),
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/time_unit/months.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl TimeUnitField for Months {
fn inclusive_max() -> Ordinal {
12
}
fn ordinal_from_name(name: &str) -> Result<Ordinal> {
fn ordinal_from_name(name: &str) -> Result<Ordinal, Error> {
//TODO: Use phf crate
let ordinal = match name.to_lowercase().as_ref() {
"jan" | "january" => 1,
Expand All @@ -34,10 +34,11 @@ impl TimeUnitField for Months {
"oct" | "october" => 10,
"nov" | "november" => 11,
"dec" | "december" => 12,
_ => bail!(ErrorKind::Expression(format!(
"'{}' is not a valid month name.",
name
))),
_ => {
return Err(
ErrorKind::Expression(format!("'{}' is not a valid month name.", name)).into(),
)
}
};
Ok(ordinal)
}
Expand Down