Simplify DoneDate when formatting

This commit is contained in:
Joscha 2022-01-09 16:40:17 +01:00
parent 20fc4bd3cc
commit 170e291ec5
4 changed files with 39 additions and 28 deletions

View file

@ -149,32 +149,16 @@ impl From<DoneDate> for Dates {
impl From<Dates> for DoneDate {
fn from(dates: Dates) -> Self {
if dates.root == dates.other {
match dates.times {
Some(times) if times.root == times.other => Self::DateTime {
root: dates.root,
root_time: times.root,
},
Some(times) => Self::DateTimeToTime {
root: dates.root,
root_time: times.root,
other_time: times.other,
},
None => Self::Date { root: dates.root },
}
} else {
match dates.times {
Some(times) => Self::DateTimeToDateTime {
root: dates.root,
root_time: times.root,
other: dates.other,
other_time: times.other,
},
None => Self::DateToDate {
root: dates.root,
other: dates.other,
},
}
let (root, other) = dates.dates();
match dates.times() {
Some((root_time, other_time)) => Self::DateTimeToDateTime {
root,
root_time,
other,
other_time,
},
None => Self::DateToDate { root, other },
}
.simplified()
}
}

View file

@ -307,6 +307,33 @@ impl DoneDate {
DoneDate::DateTimeToDateTime { root, .. } => root,
}
}
/// Remove redundancies like the same date or time specified twice.
pub fn simplified(self) -> Self {
let result = match self {
Self::DateToDate { root, other } if root == other => Self::Date { root },
Self::DateTimeToDateTime {
root,
root_time,
other,
other_time,
} if root == other => Self::DateTimeToTime {
root,
root_time,
other_time,
},
other => other,
};
match result {
Self::DateTimeToTime {
root,
root_time,
other_time,
} if root_time == other_time => Self::DateTime { root, root_time },
other => other,
}
}
}
#[derive(Debug)]

View file

@ -239,8 +239,7 @@ impl fmt::Display for Statement {
impl fmt::Display for DoneDate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO Remove redundant dates
match self {
match self.simplified() {
DoneDate::Date { root } => write!(f, "{}", root),
DoneDate::DateTime { root, root_time } => write!(f, "{} {}", root, root_time),
DoneDate::DateToDate { root, other } => write!(f, "{} -- {}", root, other),