From 55c164d4f98d66f2f8cbbe5a8e80901e046a6da4 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 28 May 2018 00:36:54 -0400 Subject: [PATCH] Starting to work on fixes --- src/lib.rs | 19 +++++++++++++------ src/tests.rs | 8 +++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 09873ca..0bfdfbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,7 @@ lazy_static! { pub enum ParseInternalError { // Errors that indicate internal bugs YMDEarlyResolve, - YMDValueUnset, + YMDValueUnset(Vec), ParseIndexError, InvalidDecimal, InvalidInteger, @@ -467,7 +467,7 @@ fn days_in_month(year: i32, month: i32) -> Result { } #[derive(Debug, Hash, PartialEq, Eq)] -enum YMDLabel { +pub enum YMDLabel { Year, Month, Day, @@ -507,6 +507,7 @@ impl YMD { fn append(&mut self, val: i32, label: Option) -> ParseIResult<()> { let mut label = label; + // TODO: Duck typing with val being an array type? if val > 100 { self.century_specified = true; match label { @@ -521,6 +522,8 @@ impl YMD { } } + self._ymd.push(val); + match label { Some(YMDLabel::Month) => { if self.mstridx.is_some() { @@ -609,8 +612,8 @@ impl YMD { .map(|u| strids.insert(YMDLabel::Day, u.clone())); // TODO: More Rustiomatic way of doing this? - if let Ok(ymd) = self.resolve_from_stridxs(&mut strids) { - return Ok(ymd); + if self._ymd.len() == strids.len() && strids.len() > 0 || (self._ymd.len() == 3 && strids.len() == 2) { + return self.resolve_from_stridxs(&mut strids); }; // 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) { if self.mstridx.is_some() { 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 { other = Some(self._ymd[0]); } @@ -710,7 +713,11 @@ impl YMD { // We should be able to justify the UNWRAP, but I haven't // convinced myself of that quite yet. 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 { Ok((year.unwrap(), month.unwrap(), day.unwrap())) } diff --git a/src/tests.rs b/src/tests.rs index 1566cf7..2e810ce 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -43,7 +43,13 @@ macro_rules! test_parse_naive { 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!(s, format!("{}", rs.0)); };