use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Change<T> {
pub before: T,
pub after: T,
}
impl<T> Change<T> {
pub fn between(before: T, after: T) -> Option<Self>
where
T: PartialEq,
{
(before != after).then_some(Self { before, after })
}
pub fn new(before: T, after: T) -> Self {
Self { before, after }
}
pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Change<U> {
Change {
before: f(self.before),
after: f(self.after),
}
}
}
impl<T> Change<Option<T>> {
pub fn is_gain(&self) -> bool {
self.before.is_none() && self.after.is_some()
}
pub fn is_loss(&self) -> bool {
self.before.is_some() && self.after.is_none()
}
pub fn is_replacement(&self) -> bool {
self.before.is_some() && self.after.is_some()
}
}
impl<T: fmt::Display> fmt::Display for Change<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} -> {}", self.before, self.after)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Presence {
Both,
Added {
before: Coverage,
},
Removed {
after: Coverage,
},
}
impl Presence {
pub fn is_in_both(&self) -> bool {
matches!(self, Presence::Both)
}
pub fn is_added(&self) -> bool {
matches!(self, Presence::Added { .. })
}
pub fn is_removed(&self) -> bool {
matches!(self, Presence::Removed { .. })
}
pub fn counterpart_coverage(&self) -> Option<Coverage> {
match self {
Presence::Both => None,
Presence::Added { before } => Some(*before),
Presence::Removed { after } => Some(*after),
}
}
pub fn is_confirmed(&self) -> bool {
self.counterpart_coverage()
.is_none_or(|coverage| coverage == Coverage::Covered)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Coverage {
Covered,
Withheld,
OutOfScope,
Unreached,
Unstated,
}
impl Coverage {
pub const ALL: &'static [Self] = &[
Self::Covered,
Self::Withheld,
Self::OutOfScope,
Self::Unreached,
Self::Unstated,
];
pub fn is_covered(&self) -> bool {
matches!(self, Coverage::Covered)
}
pub fn is_excluded(&self) -> bool {
matches!(self, Coverage::Withheld | Coverage::OutOfScope)
}
}
impl fmt::Display for Coverage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Coverage::Covered => write!(f, "covered"),
Coverage::Withheld => write!(f, "withheld"),
Coverage::OutOfScope => write!(f, "out of scope"),
Coverage::Unreached => write!(f, "unreached"),
Coverage::Unstated => write!(f, "unstated"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_value_that_did_not_move_is_not_a_change() {
assert!(Change::between(1, 1).is_none());
assert!(Change::between(Some("ssh"), Some("ssh")).is_none());
assert!(Change::between(None::<&str>, None).is_none());
}
#[test]
fn a_value_that_moved_carries_both_sides() {
let change = Change::between(1, 2).expect("a change");
assert_eq!(change.before, 1);
assert_eq!(change.after, 2);
}
#[test]
fn an_optional_field_reads_as_gain_loss_or_replacement() {
let gained = Change::new(None, Some("ssh"));
assert!(gained.is_gain() && !gained.is_loss() && !gained.is_replacement());
let lost = Change::new(Some("ssh"), None);
assert!(lost.is_loss() && !lost.is_gain() && !lost.is_replacement());
let replaced = Change::new(Some("ssh"), Some("http"));
assert!(replaced.is_replacement() && !replaced.is_gain() && !replaced.is_loss());
}
#[test]
fn mapping_a_change_keeps_both_sides_in_place() {
let mapped = Change::new(1u8, 2u8).map(|n| n * 10);
assert_eq!((mapped.before, mapped.after), (10, 20));
}
#[test]
fn a_record_in_both_scans_raises_no_coverage_question() {
assert!(Presence::Both.is_in_both());
assert_eq!(Presence::Both.counterpart_coverage(), None);
assert!(Presence::Both.is_confirmed());
}
#[test]
fn an_appearance_is_confirmed_only_where_the_baseline_covered_the_target() {
let looked = Presence::Added {
before: Coverage::Covered,
};
assert!(looked.is_added() && looked.is_confirmed());
for unconfirmed in [Coverage::Withheld, Coverage::OutOfScope, Coverage::Unstated] {
let presence = Presence::Added {
before: unconfirmed,
};
assert!(
!presence.is_confirmed(),
"{unconfirmed:?} does not confirm an appearance"
);
}
}
#[test]
fn a_disappearance_asks_the_same_question_of_the_later_scan() {
let looked = Presence::Removed {
after: Coverage::Covered,
};
assert!(looked.is_removed() && looked.is_confirmed());
let did_not = Presence::Removed {
after: Coverage::OutOfScope,
};
assert!(!did_not.is_confirmed());
}
#[test]
fn only_covered_ground_counts_as_covered() {
assert!(Coverage::Covered.is_covered());
for other in [Coverage::Withheld, Coverage::OutOfScope, Coverage::Unstated] {
assert!(!other.is_covered(), "{other:?} is not coverage");
}
assert!(Coverage::Withheld.is_excluded());
assert!(!Coverage::Unstated.is_excluded());
}
}