From d7031ee279fc6449a88f1a21f8580457b8eaf7f4 Mon Sep 17 00:00:00 2001 From: "Mark H. Colburn" Date: Mon, 25 Jun 2018 10:12:08 -0700 Subject: [PATCH 1/4] Added dmy! macro. --- src/lib.rs | 65 ++++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 85c043f..9386274 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -647,6 +647,16 @@ impl YMD { let mut day: Option = None; let mut other: Option = None; + macro_rules! dmy { + ($d:expr, $m:expr, $y:expr) => { + { + day = $d; + month = $m; + year = $y; + } + }; + } + let mut strids: HashMap = HashMap::new(); self.ystridx .map(|u| strids.insert(YMDLabel::Year, u.clone())); @@ -668,7 +678,8 @@ impl YMD { return Err(ParseInternalError::ValueError( "More than three YMD values".to_owned(), )); - } else if len_ymd == 1 || (self.mstridx.is_some() && len_ymd == 2) { + } + if len_ymd == 1 || (self.mstridx.is_some() && len_ymd == 2) { if self.mstridx.is_some() { month = Some(self._ymd[self.mstridx.unwrap()]); other = if len_ymd == 1 { @@ -689,71 +700,47 @@ impl YMD { } } else if len_ymd == 2 { if self._ymd[0] > 31 { - year = Some(self._ymd[0]); - month = Some(self._ymd[1]); + dmy!(None, Some(self._ymd[1]), Some(self._ymd[0])); } else if self._ymd[1] > 31 { - month = Some(self._ymd[0]); - year = Some(self._ymd[1]); + dmy!(None, Some(self._ymd[0]), Some(self._ymd[1])); } else if dayfirst && self._ymd[1] <= 12 { - day = Some(self._ymd[0]); - month = Some(self._ymd[1]); + dmy!(Some(self._ymd[0]), Some(self._ymd[1]), None); } else { - month = Some(self._ymd[0]); - day = Some(self._ymd[1]); + dmy!(Some(self._ymd[1]), Some(self._ymd[0]), None); } } else if len_ymd == 3 { if self.mstridx == Some(0) { if self._ymd[1] > 31 { - month = Some(self._ymd[0]); - year = Some(self._ymd[1]); - day = Some(self._ymd[2]); + dmy!(Some(self._ymd[2]), Some(self._ymd[0]), Some(self._ymd[1])); } else { - month = Some(self._ymd[0]); - day = Some(self._ymd[1]); - year = Some(self._ymd[2]); + dmy!(Some(self._ymd[1]), Some(self._ymd[0]), Some(self._ymd[2])); } } else if self.mstridx == Some(1) { if self._ymd[0] > 31 || (yearfirst && self._ymd[2] <= 31) { - year = Some(self._ymd[0]); - month = Some(self._ymd[1]); - day = Some(self._ymd[2]); + dmy!(Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0])); } else { - day = Some(self._ymd[0]); - month = Some(self._ymd[1]); - year = Some(self._ymd[2]); + dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); } } else if self.mstridx == Some(2) { // It was in the original docs, so: WTF!? if self._ymd[1] > 31 { - day = Some(self._ymd[0]); - year = Some(self._ymd[1]); - month = Some(self._ymd[2]); + dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); } else { - year = Some(self._ymd[0]); - day = Some(self._ymd[1]); - month = Some(self._ymd[2]); + dmy!(Some(self._ymd[1]), Some(self._ymd[2]), Some(self._ymd[0])); } } else { if self._ymd[0] > 31 || self.ystridx == Some(0) || (yearfirst && self._ymd[1] <= 12 && self._ymd[2] <= 31) { if dayfirst && self._ymd[2] <= 12 { - year = Some(self._ymd[0]); - day = Some(self._ymd[1]); - month = Some(self._ymd[2]); + dmy!(Some(self._ymd[1]), Some(self._ymd[2]), Some(self._ymd[0])); } else { - year = Some(self._ymd[0]); - month = Some(self._ymd[1]); - day = Some(self._ymd[2]); + dmy!(Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0])); } } else if self._ymd[0] > 12 || (dayfirst && self._ymd[1] <= 12) { - day = Some(self._ymd[0]); - month = Some(self._ymd[1]); - year = Some(self._ymd[2]); + dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); } else { - month = Some(self._ymd[0]); - day = Some(self._ymd[1]); - year = Some(self._ymd[2]); + dmy!(Some(self._ymd[1]), Some(self._ymd[0]), Some(self._ymd[2])); } } } From f819ec8e40705144b5823357338efc63b22a3923 Mon Sep 17 00:00:00 2001 From: "Mark H. Colburn" Date: Mon, 25 Jun 2018 10:49:30 -0700 Subject: [PATCH 2/4] Refactor if logic for resolve_ymd. --- src/lib.rs | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9386274..e9ef025 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -651,7 +651,7 @@ impl YMD { ($d:expr, $m:expr, $y:expr) => { { day = $d; - month = $m; + month = $m; year = $y; } }; @@ -679,34 +679,35 @@ impl YMD { "More than three YMD values".to_owned(), )); } - if len_ymd == 1 || (self.mstridx.is_some() && len_ymd == 2) { + if len_ymd == 1 { if self.mstridx.is_some() { month = Some(self._ymd[self.mstridx.unwrap()]); - other = if len_ymd == 1 { - Some(self._ymd[0]) + let other = self._ymd[0]; + if other > 31 { + year = Some(other); } else { - Some(self._ymd[1 - self.mstridx.unwrap()]) - }; - } else { - other = Some(self._ymd[0]); - } - - if len_ymd > 1 || self.mstridx.is_some() { - if other.unwrap_or(0) > 31 { - year = other; - } else { - day = other; + day = Some(other); } } } else if len_ymd == 2 { - if self._ymd[0] > 31 { - dmy!(None, Some(self._ymd[1]), Some(self._ymd[0])); - } else if self._ymd[1] > 31 { - dmy!(None, Some(self._ymd[0]), Some(self._ymd[1])); - } else if dayfirst && self._ymd[1] <= 12 { - dmy!(Some(self._ymd[0]), Some(self._ymd[1]), None); + if self.mstridx.is_some() { + month = Some(self._ymd[self.mstridx.unwrap()]); + let other = self._ymd[1 - self.mstridx.unwrap()]; + if other > 31 { + year = Some(other); + } else { + day = Some(other); + } } else { - dmy!(Some(self._ymd[1]), Some(self._ymd[0]), None); + if self._ymd[0] > 31 { + dmy!(None, Some(self._ymd[1]), Some(self._ymd[0])); + } else if self._ymd[1] > 31 { + dmy!(None, Some(self._ymd[0]), Some(self._ymd[1])); + } else if dayfirst && self._ymd[1] <= 12 { + dmy!(Some(self._ymd[0]), Some(self._ymd[1]), None); + } else { + dmy!(Some(self._ymd[1]), Some(self._ymd[0]), None); + } } } else if len_ymd == 3 { if self.mstridx == Some(0) { From 37eefb23206b9931b5d3c313ca9807cb141b4b59 Mon Sep 17 00:00:00 2001 From: "Mark H. Colburn" Date: Mon, 25 Jun 2018 11:56:24 -0700 Subject: [PATCH 3/4] Refactor match statement in resolve_ymd. --- src/lib.rs | 92 ++++++++++++++++++++++-------------------------------- 1 file changed, 38 insertions(+), 54 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e9ef025..d94c88b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -645,16 +645,9 @@ impl YMD { let mut year: Option = None; let mut month: Option = None; let mut day: Option = None; - let mut other: Option = None; macro_rules! dmy { - ($d:expr, $m:expr, $y:expr) => { - { - day = $d; - month = $m; - year = $y; - } - }; + ($d:expr, $m:expr, $y:expr) => { return Ok(($y, $m, $d)); } } let mut strids: HashMap = HashMap::new(); @@ -672,81 +665,72 @@ impl YMD { return self.resolve_from_stridxs(&mut strids); }; - // TODO: More Rustiomatic? Too many blocks for my liking - // Also having the array unpacking syntax is nice if len_ymd > 3 { return Err(ParseInternalError::ValueError( "More than three YMD values".to_owned(), )); } - if len_ymd == 1 { - if self.mstridx.is_some() { - month = Some(self._ymd[self.mstridx.unwrap()]); - let other = self._ymd[0]; - if other > 31 { - year = Some(other); + match (len_ymd, self.mstridx) { + (1, Some(val)) | + (2, Some(val)) => { + month = Some(self._ymd[val]); + let other = if len_ymd == 1 { + self._ymd[0] } else { - day = Some(other); - } - } - } else if len_ymd == 2 { - if self.mstridx.is_some() { - month = Some(self._ymd[self.mstridx.unwrap()]); - let other = self._ymd[1 - self.mstridx.unwrap()]; + self._ymd[1 - val] + }; if other > 31 { - year = Some(other); - } else { - day = Some(other); + dmy!(day, month, Some(other)); } - } else { + dmy!(Some(other), month, year); + }, + (2, None) => { if self._ymd[0] > 31 { dmy!(None, Some(self._ymd[1]), Some(self._ymd[0])); - } else if self._ymd[1] > 31 { - dmy!(None, Some(self._ymd[0]), Some(self._ymd[1])); - } else if dayfirst && self._ymd[1] <= 12 { - dmy!(Some(self._ymd[0]), Some(self._ymd[1]), None); - } else { - dmy!(Some(self._ymd[1]), Some(self._ymd[0]), None); } - } - } else if len_ymd == 3 { - if self.mstridx == Some(0) { + if self._ymd[1] > 31 { + dmy!(None, Some(self._ymd[0]), Some(self._ymd[1])); + } + if dayfirst && self._ymd[1] <= 12 { + dmy!(Some(self._ymd[0]), Some(self._ymd[1]), None); + } + dmy!(Some(self._ymd[1]), Some(self._ymd[0]), None); + }, + (3, Some(0)) => { if self._ymd[1] > 31 { dmy!(Some(self._ymd[2]), Some(self._ymd[0]), Some(self._ymd[1])); - } else { - dmy!(Some(self._ymd[1]), Some(self._ymd[0]), Some(self._ymd[2])); } - } else if self.mstridx == Some(1) { + dmy!(Some(self._ymd[1]), Some(self._ymd[0]), Some(self._ymd[2])); + }, + (3, Some(1)) => { if self._ymd[0] > 31 || (yearfirst && self._ymd[2] <= 31) { dmy!(Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0])); - } else { - dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); } - } else if self.mstridx == Some(2) { + dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); + }, + (3, Some(2)) => { // It was in the original docs, so: WTF!? if self._ymd[1] > 31 { dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); - } else { - dmy!(Some(self._ymd[1]), Some(self._ymd[2]), Some(self._ymd[0])); } - } else { + dmy!(Some(self._ymd[1]), Some(self._ymd[2]), Some(self._ymd[0])); + }, + (3, None) => { if self._ymd[0] > 31 || self.ystridx == Some(0) || (yearfirst && self._ymd[1] <= 12 && self._ymd[2] <= 31) { if dayfirst && self._ymd[2] <= 12 { dmy!(Some(self._ymd[1]), Some(self._ymd[2]), Some(self._ymd[0])); - } else { - dmy!(Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0])); } - } else if self._ymd[0] > 12 || (dayfirst && self._ymd[1] <= 12) { - dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); - } else { - dmy!(Some(self._ymd[1]), Some(self._ymd[0]), Some(self._ymd[2])); + dmy!(Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0])); } - } + if self._ymd[0] > 12 || (dayfirst && self._ymd[1] <= 12) { + dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); + } + dmy!(Some(self._ymd[1]), Some(self._ymd[0]), Some(self._ymd[2])); + }, + (_, _) => { dmy!(None, None, None); }, } - - Ok((year, month, day)) } } From 60879fd0fd99d231ce91ac5915be29c5c55a949c Mon Sep 17 00:00:00 2001 From: "Mark H. Colburn" Date: Mon, 25 Jun 2018 12:22:59 -0700 Subject: [PATCH 4/4] Remove dmy! macro. --- src/lib.rs | 50 +++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d94c88b..6fc5df0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -642,13 +642,6 @@ impl YMD { dayfirst: bool, ) -> ParseIResult<(Option, Option, Option)> { let len_ymd = self._ymd.len(); - let mut year: Option = None; - let mut month: Option = None; - let mut day: Option = None; - - macro_rules! dmy { - ($d:expr, $m:expr, $y:expr) => { return Ok(($y, $m, $d)); } - } let mut strids: HashMap = HashMap::new(); self.ystridx @@ -659,8 +652,8 @@ impl YMD { .map(|u| strids.insert(YMDLabel::Day, u.clone())); // TODO: More Rustiomatic way of doing this? - if self._ymd.len() == strids.len() && strids.len() > 0 - || (self._ymd.len() == 3 && strids.len() == 2) + if len_ymd == strids.len() && strids.len() > 0 + || (len_ymd == 3 && strids.len() == 2) { return self.resolve_from_stridxs(&mut strids); }; @@ -670,66 +663,65 @@ impl YMD { "More than three YMD values".to_owned(), )); } + match (len_ymd, self.mstridx) { (1, Some(val)) | (2, Some(val)) => { - month = Some(self._ymd[val]); let other = if len_ymd == 1 { self._ymd[0] } else { self._ymd[1 - val] }; if other > 31 { - dmy!(day, month, Some(other)); + return Ok((Some(other), Some(self._ymd[val]), None)); } - dmy!(Some(other), month, year); + return Ok((None, Some(self._ymd[val]), Some(other))); }, (2, None) => { if self._ymd[0] > 31 { - dmy!(None, Some(self._ymd[1]), Some(self._ymd[0])); + return Ok((Some(self._ymd[0]), Some(self._ymd[1]), None)); } if self._ymd[1] > 31 { - dmy!(None, Some(self._ymd[0]), Some(self._ymd[1])); + return Ok((Some(self._ymd[1]), Some(self._ymd[0]), None)); } if dayfirst && self._ymd[1] <= 12 { - dmy!(Some(self._ymd[0]), Some(self._ymd[1]), None); + return Ok((None, Some(self._ymd[1]), Some(self._ymd[0]))); } - dmy!(Some(self._ymd[1]), Some(self._ymd[0]), None); + return Ok((None, Some(self._ymd[0]), Some(self._ymd[1]))); }, (3, Some(0)) => { if self._ymd[1] > 31 { - dmy!(Some(self._ymd[2]), Some(self._ymd[0]), Some(self._ymd[1])); + return Ok((Some(self._ymd[1]), Some(self._ymd[0]), Some(self._ymd[2]))); } - dmy!(Some(self._ymd[1]), Some(self._ymd[0]), Some(self._ymd[2])); + return Ok((Some(self._ymd[2]), Some(self._ymd[0]), Some(self._ymd[1]))); }, (3, Some(1)) => { if self._ymd[0] > 31 || (yearfirst && self._ymd[2] <= 31) { - dmy!(Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0])); + return Ok((Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2]))); } - dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); + return Ok((Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0]))); }, (3, Some(2)) => { // It was in the original docs, so: WTF!? if self._ymd[1] > 31 { - dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); + return Ok((Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0]))); } - dmy!(Some(self._ymd[1]), Some(self._ymd[2]), Some(self._ymd[0])); + return Ok((Some(self._ymd[0]), Some(self._ymd[2]), Some(self._ymd[1]))); }, (3, None) => { if self._ymd[0] > 31 || self.ystridx == Some(0) || (yearfirst && self._ymd[1] <= 12 && self._ymd[2] <= 31) { if dayfirst && self._ymd[2] <= 12 { - dmy!(Some(self._ymd[1]), Some(self._ymd[2]), Some(self._ymd[0])); + return Ok((Some(self._ymd[0]), Some(self._ymd[2]), Some(self._ymd[1]))); } - dmy!(Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0])); + return Ok((Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2]))); + } else if self._ymd[0] > 12 || (dayfirst && self._ymd[1] <= 12) { + return Ok((Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0]))); } - if self._ymd[0] > 12 || (dayfirst && self._ymd[1] <= 12) { - dmy!(Some(self._ymd[0]), Some(self._ymd[1]), Some(self._ymd[2])); - } - dmy!(Some(self._ymd[1]), Some(self._ymd[0]), Some(self._ymd[2])); + return Ok((Some(self._ymd[2]), Some(self._ymd[0]), Some(self._ymd[1]))); }, - (_, _) => { dmy!(None, None, None); }, + (_, _) => { return Ok((None, None, None)); }, } } }