Skip to content

Commit

Permalink
Merge pull request #27 from william20111/master
Browse files Browse the repository at this point in the history
fixed step_by change rust issue #27741
  • Loading branch information
zslayton authored Jul 7, 2017
2 parents f64904b + 6f0d9e9 commit a4de18f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
#![feature(conservative_impl_trait)]
#![feature(step_by)]
#![feature(iterator_step_by)]

extern crate chrono;
extern crate nom;
Expand Down
37 changes: 21 additions & 16 deletions src/time_unit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@ pub trait TimeUnitField
fn ordinal_from_name(name: &str) -> Result<Ordinal> {
bail!(ErrorKind::Expression(format!("The '{}' field does not support using names. '{}' \
specified.",
Self::name(),
name)))
Self::name(),
name)))
}
fn validate_ordinal(ordinal: Ordinal) -> Result<Ordinal> {
//println!("validate_ordinal for {} => {}", Self::name(), ordinal);
match ordinal {
i if i < Self::inclusive_min() => {
bail!(ErrorKind::Expression(format!("{} must be greater than or equal to {}. ('{}' \
specified.)",
Self::name(),
Self::inclusive_min(),
i)))
Self::name(),
Self::inclusive_min(),
i)))
}
i if i > Self::inclusive_max() => {
bail!(ErrorKind::Expression(format!("{} must be less than {}. ('{}' specified.)",
Self::name(),
Self::inclusive_max(),
i)))
Self::name(),
Self::inclusive_max(),
i)))
}
i => Ok(i),
}
Expand All @@ -70,20 +70,25 @@ pub trait TimeUnitField
All => Ok(Self::supported_ordinals()),
Point(ordinal) => Ok((&[ordinal]).iter().cloned().collect()),
NamedPoint(ref name) => {
Ok((&[Self::ordinal_from_name(name)?]).iter().cloned().collect())
Ok((&[Self::ordinal_from_name(name)?])
.iter()
.cloned()
.collect())
}
Period(start, step) => {
let start = Self::validate_ordinal(start)?;
Ok((start..Self::inclusive_max() + 1).step_by(step).collect())
Ok((start..Self::inclusive_max() + 1)
.step_by(step as usize)
.collect())
}
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!("Invalid range for {}: {}-{}",
Self::name(),
start,
end)))
Self::name(),
start,
end)))
}
}
}
Expand All @@ -94,9 +99,9 @@ pub trait TimeUnitField
(Ok(start), Ok(end)) if start <= end => Ok((start..end + 1).collect()),
_ => {
bail!(ErrorKind::Expression(format!("Invalid named range for {}: {}-{}",
Self::name(),
start_name,
end_name)))
Self::name(),
start_name,
end_name)))
}
}
}
Expand Down

0 comments on commit a4de18f

Please sign in to comment.