diff --git a/Cargo.toml b/Cargo.toml index a7e7a87..5ca09d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" \ No newline at end of file diff --git a/src/error.rs b/src/error.rs index 8757c94..0f86dec 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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 for Error { + fn from(kind: ErrorKind) -> Error { + Error { kind } + } +} diff --git a/src/lib.rs b/src/lib.rs index 78aa8a6..3f5d9ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,9 +36,6 @@ //! */ //! ``` -#[macro_use] -extern crate error_chain; - #[cfg(test)] extern crate chrono_tz; diff --git a/src/schedule.rs b/src/schedule.rs index da31602..0aaa719 100644 --- a/src/schedule.rs +++ b/src/schedule.rs @@ -123,11 +123,12 @@ impl Schedule { fn from_field_list(fields: Vec) -> Result { 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(); @@ -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 } } } diff --git a/src/time_unit/days_of_week.rs b/src/time_unit/days_of_week.rs index 4764f79..34ea081 100644 --- a/src/time_unit/days_of_week.rs +++ b/src/time_unit/days_of_week.rs @@ -19,7 +19,7 @@ impl TimeUnitField for DaysOfWeek { fn inclusive_max() -> Ordinal { 7 } - fn ordinal_from_name(name: &str) -> Result { + fn ordinal_from_name(name: &str) -> Result { //TODO: Use phf crate let ordinal = match name.to_lowercase().as_ref() { "sun" | "sunday" => 1, @@ -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) } diff --git a/src/time_unit/mod.rs b/src/time_unit/mod.rs index 57682d2..ae34a20 100644 --- a/src/time_unit/mod.rs +++ b/src/time_unit/mod.rs @@ -188,35 +188,38 @@ where fn all() -> Self { Self::from_ordinal_set(Self::supported_ordinals()) } - fn ordinal_from_name(name: &str) -> Result { - bail!(ErrorKind::Expression(format!( + fn ordinal_from_name(name: &str) -> Result { + Err(ErrorKind::Expression(format!( "The '{}' field does not support using names. '{}' \ specified.", Self::name(), name - ))) + )) + .into()) } - fn validate_ordinal(ordinal: Ordinal) -> Result { + fn validate_ordinal(ordinal: Ordinal) -> Result { //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 { + fn ordinals_from_specifier(specifier: &Specifier) -> Result { use self::Specifier::*; //println!("ordinals_from_specifier for {} => {:?}", Self::name(), specifier); match *specifier { @@ -235,12 +238,13 @@ 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) => { @@ -248,12 +252,13 @@ where 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()), } } } diff --git a/src/time_unit/months.rs b/src/time_unit/months.rs index fcd1903..02c93f6 100644 --- a/src/time_unit/months.rs +++ b/src/time_unit/months.rs @@ -19,7 +19,7 @@ impl TimeUnitField for Months { fn inclusive_max() -> Ordinal { 12 } - fn ordinal_from_name(name: &str) -> Result { + fn ordinal_from_name(name: &str) -> Result { //TODO: Use phf crate let ordinal = match name.to_lowercase().as_ref() { "jan" | "january" => 1, @@ -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) }