1
0
mirror of https://github.com/bspeice/dtparse synced 2024-12-22 12:28:08 -05:00
This commit is contained in:
Bradlee Speice 2018-06-25 23:08:03 -04:00
parent 4b92fe9123
commit 24816dc3ea
2 changed files with 14 additions and 10 deletions

View File

@ -29,6 +29,9 @@ use std::vec::Vec;
mod weekday; mod weekday;
#[cfg(test)]
mod tests;
use weekday::day_of_week; use weekday::day_of_week;
use weekday::DayOfWeek; use weekday::DayOfWeek;
@ -64,7 +67,7 @@ impl From<ParseIntError> for ParseInternalError {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum ParseError { pub enum ParseError {
AmbiguousWeekday, AmbiguousWeekday,
InternalError(ParseInternalError), InternalError(ParseInternalError),
@ -471,7 +474,6 @@ fn days_in_month(year: i32, month: i32) -> Result<u32, ParseError> {
1 | 3 | 5 | 7 | 8 | 10 | 12 => Ok(31), 1 | 3 | 5 | 7 | 8 | 10 | 12 => Ok(31),
4 | 6 | 9 | 11 => Ok(30), 4 | 6 | 9 | 11 => Ok(30),
_ => { _ => {
println!("Invalid month: {}", month);
Err(ParseError::InvalidMonth) Err(ParseError::InvalidMonth)
} }
} }
@ -523,8 +525,6 @@ impl YMD {
fn append(&mut self, val: i32, token: &str, label: Option<YMDLabel>) -> ParseIResult<()> { fn append(&mut self, val: i32, token: &str, label: Option<YMDLabel>) -> ParseIResult<()> {
let mut label = label; let mut label = label;
println!("Val: {}, Token: {}", val, token);
// Python auto-detects strings using the '__len__' function here. // Python auto-detects strings using the '__len__' function here.
// We instead take in both and handle as necessary. // We instead take in both and handle as necessary.
if Decimal::from_str(token).is_ok() && token.len() > 2 { if Decimal::from_str(token).is_ok() && token.len() > 2 {
@ -725,7 +725,7 @@ impl YMD {
} }
} }
#[derive(Default, Debug)] #[derive(Default, Debug, PartialEq)]
pub struct ParsingResult { pub struct ParsingResult {
year: Option<i32>, year: Option<i32>,
month: Option<i32>, month: Option<i32>,
@ -989,7 +989,6 @@ impl Parser {
let d_offset = if res.weekday.is_some() && res.day.is_none() { let d_offset = if res.weekday.is_some() && res.day.is_none() {
// TODO: Unwrap not justified // TODO: Unwrap not justified
let dow = day_of_week(y as u32, m, default.day()).unwrap(); let dow = day_of_week(y as u32, m, default.day()).unwrap();
println!("dow: {:?}", dow);
// UNWRAP: We've already check res.weekday() is some // UNWRAP: We've already check res.weekday() is some
let actual_weekday = (res.weekday.unwrap() + 1) % 7; let actual_weekday = (res.weekday.unwrap() + 1) % 7;
@ -1003,11 +1002,9 @@ impl Parser {
let mut d = NaiveDate::from_ymd( let mut d = NaiveDate::from_ymd(
y, y,
m, m,
min(res.day.unwrap_or(default.day() as i32) as u32, days_in_month(y, m as i32).unwrap()) min(res.day.unwrap_or(default.day() as i32) as u32, days_in_month(y, m as i32)?)
); );
println!("d: {:?}, d_offset: {:?}", d, d_offset);
let d = d + d_offset; let d = d + d_offset;
let t = NaiveTime::from_hms_micro( let t = NaiveTime::from_hms_micro(
@ -1291,7 +1288,6 @@ impl Parser {
let sec_remainder = value - value.floor(); let sec_remainder = value - value.floor();
if sec_remainder != *ZERO { if sec_remainder != *ZERO {
println!("{}", *SIXTY * sec_remainder);
second = Some((*SIXTY * sec_remainder).floor().to_i64().unwrap() as i32); second = Some((*SIXTY * sec_remainder).floor().to_i64().unwrap() as i32);
} }

8
src/tests.rs Normal file
View File

@ -0,0 +1,8 @@
use ParseError;
use parse;
#[test]
fn test_fuzz() {
assert_eq!(parse("\x2D\x38\x31\x39\x34\x38\x34"), Err(ParseError::InvalidMonth));
}