1
0
mirror of https://github.com/bspeice/dtparse synced 2024-12-22 04:18:09 -05:00

Starting to work on fixes

This commit is contained in:
Bradlee Speice 2018-05-28 00:36:54 -04:00
parent 1488fbaeca
commit 55c164d4f9
2 changed files with 20 additions and 7 deletions

View File

@ -42,7 +42,7 @@ lazy_static! {
pub enum ParseInternalError { pub enum ParseInternalError {
// Errors that indicate internal bugs // Errors that indicate internal bugs
YMDEarlyResolve, YMDEarlyResolve,
YMDValueUnset, YMDValueUnset(Vec<YMDLabel>),
ParseIndexError, ParseIndexError,
InvalidDecimal, InvalidDecimal,
InvalidInteger, InvalidInteger,
@ -467,7 +467,7 @@ fn days_in_month(year: i32, month: i32) -> Result<i32, ParseError> {
} }
#[derive(Debug, Hash, PartialEq, Eq)] #[derive(Debug, Hash, PartialEq, Eq)]
enum YMDLabel { pub enum YMDLabel {
Year, Year,
Month, Month,
Day, Day,
@ -507,6 +507,7 @@ impl YMD {
fn append(&mut self, val: i32, label: Option<YMDLabel>) -> ParseIResult<()> { fn append(&mut self, val: i32, label: Option<YMDLabel>) -> ParseIResult<()> {
let mut label = label; let mut label = label;
// TODO: Duck typing with val being an array type?
if val > 100 { if val > 100 {
self.century_specified = true; self.century_specified = true;
match label { match label {
@ -521,6 +522,8 @@ impl YMD {
} }
} }
self._ymd.push(val);
match label { match label {
Some(YMDLabel::Month) => { Some(YMDLabel::Month) => {
if self.mstridx.is_some() { if self.mstridx.is_some() {
@ -609,8 +612,8 @@ impl YMD {
.map(|u| strids.insert(YMDLabel::Day, u.clone())); .map(|u| strids.insert(YMDLabel::Day, u.clone()));
// TODO: More Rustiomatic way of doing this? // TODO: More Rustiomatic way of doing this?
if let Ok(ymd) = self.resolve_from_stridxs(&mut strids) { if self._ymd.len() == strids.len() && strids.len() > 0 || (self._ymd.len() == 3 && strids.len() == 2) {
return Ok(ymd); return self.resolve_from_stridxs(&mut strids);
}; };
// TODO: More Rustiomatic? Too many blocks for my liking // TODO: More Rustiomatic? Too many blocks for my liking
@ -622,7 +625,7 @@ impl YMD {
} else if len_ymd == 1 || (self.mstridx.is_some() && len_ymd == 2) { } else if len_ymd == 1 || (self.mstridx.is_some() && len_ymd == 2) {
if self.mstridx.is_some() { if self.mstridx.is_some() {
month = Some(self._ymd[self.mstridx.unwrap()]); month = Some(self._ymd[self.mstridx.unwrap()]);
other = Some(self._ymd[self.mstridx.unwrap() - 1]); other = if len_ymd == 1 { Some(self._ymd[0]) } else { Some(self._ymd[1 - self.mstridx.unwrap()]) };
} else { } else {
other = Some(self._ymd[0]); other = Some(self._ymd[0]);
} }
@ -710,7 +713,11 @@ impl YMD {
// We should be able to justify the UNWRAP, but I haven't // We should be able to justify the UNWRAP, but I haven't
// convinced myself of that quite yet. // convinced myself of that quite yet.
if !year.and(month).and(day).is_some() { if !year.and(month).and(day).is_some() {
Err(ParseInternalError::YMDValueUnset) let mut ymd_unset = Vec::new();
if year.is_none() { ymd_unset.push(YMDLabel::Year) }
else if month.is_none() { ymd_unset.push(YMDLabel::Month) }
else { ymd_unset.push(YMDLabel::Day) }
Err(ParseInternalError::YMDValueUnset(ymd_unset))
} else { } else {
Ok((year.unwrap(), month.unwrap(), day.unwrap())) Ok((year.unwrap(), month.unwrap(), day.unwrap()))
} }

View File

@ -43,7 +43,13 @@ macro_rules! test_parse_naive {
println!("{}", s); println!("{}", s);
let rs = parse($s).unwrap(); let r_rs = parse($s);
if r_rs.is_err() {
println!("{:?}", r_rs);
assert!(false);
}
let rs = r_rs.unwrap();
assert_eq!(rs.1, None); assert_eq!(rs.1, None);
assert_eq!(s, format!("{}", rs.0)); assert_eq!(s, format!("{}", rs.0));
}; };