1
0
mirror of https://github.com/bspeice/dtparse synced 2024-11-14 09:58:09 -05:00

Refactor match statement in resolve_ymd.

This commit is contained in:
Mark H. Colburn 2018-06-25 11:56:24 -07:00
parent f819ec8e40
commit 37eefb2320

View File

@ -645,16 +645,9 @@ impl YMD {
let mut year: Option<i32> = None; let mut year: Option<i32> = None;
let mut month: Option<i32> = None; let mut month: Option<i32> = None;
let mut day: Option<i32> = None; let mut day: Option<i32> = None;
let mut other: Option<i32> = None;
macro_rules! dmy { macro_rules! dmy {
($d:expr, $m:expr, $y:expr) => { ($d:expr, $m:expr, $y:expr) => { return Ok(($y, $m, $d)); }
{
day = $d;
month = $m;
year = $y;
}
};
} }
let mut strids: HashMap<YMDLabel, usize> = HashMap::new(); let mut strids: HashMap<YMDLabel, usize> = HashMap::new();
@ -672,84 +665,75 @@ impl YMD {
return self.resolve_from_stridxs(&mut strids); 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 { if len_ymd > 3 {
return Err(ParseInternalError::ValueError( return Err(ParseInternalError::ValueError(
"More than three YMD values".to_owned(), "More than three YMD values".to_owned(),
)); ));
} }
if len_ymd == 1 { match (len_ymd, self.mstridx) {
if self.mstridx.is_some() { (1, Some(val)) |
month = Some(self._ymd[self.mstridx.unwrap()]); (2, Some(val)) => {
let other = self._ymd[0]; month = Some(self._ymd[val]);
let other = if len_ymd == 1 {
self._ymd[0]
} else {
self._ymd[1 - val]
};
if other > 31 { if other > 31 {
year = Some(other); dmy!(day, month, Some(other));
} else {
day = Some(other);
} }
} dmy!(Some(other), month, year);
} else if len_ymd == 2 { },
if self.mstridx.is_some() { (2, None) => {
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 {
if self._ymd[0] > 31 { if self._ymd[0] > 31 {
dmy!(None, Some(self._ymd[1]), Some(self._ymd[0])); dmy!(None, Some(self._ymd[1]), Some(self._ymd[0]));
} else if self._ymd[1] > 31 { }
if self._ymd[1] > 31 {
dmy!(None, Some(self._ymd[0]), Some(self._ymd[1])); dmy!(None, Some(self._ymd[0]), Some(self._ymd[1]));
} else if dayfirst && self._ymd[1] <= 12 { }
if dayfirst && self._ymd[1] <= 12 {
dmy!(Some(self._ymd[0]), Some(self._ymd[1]), None); dmy!(Some(self._ymd[0]), Some(self._ymd[1]), None);
} else { }
dmy!(Some(self._ymd[1]), Some(self._ymd[0]), None); dmy!(Some(self._ymd[1]), Some(self._ymd[0]), None);
} },
} (3, Some(0)) => {
} else if len_ymd == 3 {
if self.mstridx == Some(0) {
if self._ymd[1] > 31 { if self._ymd[1] > 31 {
dmy!(Some(self._ymd[2]), Some(self._ymd[0]), Some(self._ymd[1])); 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) { if self._ymd[0] > 31 || (yearfirst && self._ymd[2] <= 31) {
dmy!(Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0])); 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!? // It was in the original docs, so: WTF!?
if self._ymd[1] > 31 { if self._ymd[1] > 31 {
dmy!(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]));
} 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) if self._ymd[0] > 31 || self.ystridx == Some(0)
|| (yearfirst && self._ymd[1] <= 12 && self._ymd[2] <= 31) || (yearfirst && self._ymd[1] <= 12 && self._ymd[2] <= 31)
{ {
if dayfirst && self._ymd[2] <= 12 { if dayfirst && self._ymd[2] <= 12 {
dmy!(Some(self._ymd[1]), Some(self._ymd[2]), Some(self._ymd[0])); 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])); dmy!(Some(self._ymd[2]), Some(self._ymd[1]), Some(self._ymd[0]));
} }
} else if self._ymd[0] > 12 || (dayfirst && self._ymd[1] <= 12) { 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[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[1]), Some(self._ymd[0]), Some(self._ymd[2]));
},
(_, _) => { dmy!(None, None, None); },
} }
} }
} }
Ok((year, month, day))
}
}
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct ParsingResult { pub struct ParsingResult {
year: Option<i32>, year: Option<i32>,