use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Coverage {
pub from: CoverFrom,
pub to: CoverTo,
}
impl Coverage {
#[must_use]
pub fn new(from: CoverFrom, to: CoverTo) -> Self {
Self { from, to }
}
#[must_use]
pub fn whole() -> Self {
Self { from: CoverFrom::Start, to: CoverTo::Here }
}
#[must_use]
pub fn from_field(field: &str) -> Self {
Self { from: CoverFrom::Field(String::from(field)), to: CoverTo::Here }
}
#[must_use]
pub fn to(self, to: CoverTo) -> Self {
Self { to, ..self }
}
#[must_use]
pub fn resolve(
&self,
own: (u64, u64),
at: impl Fn(&str) -> Option<(u64, u64)>,
) -> Option<Covered> {
let (start, end) = own;
let from = match &self.from {
CoverFrom::Start => 0,
CoverFrom::Field(f) => at(f)?.0,
};
let to = match &self.to {
CoverTo::Here => start,
CoverTo::Before(f) => at(f)?.0,
CoverTo::After(f) => at(f)?.1,
};
Some(Covered::new(from, to, (start, end)))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Covered {
pub from: u64,
pub to: u64,
pub own: (u64, u64),
}
impl Covered {
#[must_use]
pub fn new(from: u64, to: u64, own: (u64, u64)) -> Self {
Self { from, to, own }
}
#[must_use]
pub fn excises(&self) -> bool {
self.from <= self.own.0 && self.to > self.own.0
}
#[must_use]
pub fn runs(&self) -> Vec<(u64, u64)> {
if self.excises() {
vec![(self.from, self.own.0), (self.own.1, self.to)]
} else {
vec![(self.from, self.to)]
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CoverFrom {
Start,
Field(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CoverTo {
Here,
Before(String),
After(String),
}