layline_codegen/model/
coverage.rs1use alloc::string::String;
4use alloc::vec;
5use alloc::vec::Vec;
6
7#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11#[non_exhaustive]
12pub struct Coverage {
13 pub from: CoverFrom,
15 pub to: CoverTo,
17}
18
19impl Coverage {
20 #[must_use]
22 pub fn new(from: CoverFrom, to: CoverTo) -> Self {
23 Self { from, to }
24 }
25
26 #[must_use]
28 pub fn whole() -> Self {
29 Self { from: CoverFrom::Start, to: CoverTo::Here }
30 }
31
32 #[must_use]
34 pub fn from_field(field: &str) -> Self {
35 Self { from: CoverFrom::Field(String::from(field)), to: CoverTo::Here }
36 }
37
38 #[must_use]
40 pub fn to(self, to: CoverTo) -> Self {
41 Self { to, ..self }
42 }
43
44 #[must_use]
48 pub fn resolve(
49 &self,
50 own: (u64, u64),
51 at: impl Fn(&str) -> Option<(u64, u64)>,
52 ) -> Option<Covered> {
53 let (start, end) = own;
54 let from = match &self.from {
55 CoverFrom::Start => 0,
56 CoverFrom::Field(f) => at(f)?.0,
57 };
58 let to = match &self.to {
59 CoverTo::Here => start,
60 CoverTo::Before(f) => at(f)?.0,
61 CoverTo::After(f) => at(f)?.1,
62 };
63 Some(Covered::new(from, to, (start, end)))
64 }
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69#[non_exhaustive]
70pub struct Covered {
71 pub from: u64,
73 pub to: u64,
75 pub own: (u64, u64),
77}
78
79impl Covered {
80 #[must_use]
82 pub fn new(from: u64, to: u64, own: (u64, u64)) -> Self {
83 Self { from, to, own }
84 }
85
86 #[must_use]
88 pub fn excises(&self) -> bool {
89 self.from <= self.own.0 && self.to > self.own.0
90 }
91
92 #[must_use]
94 pub fn runs(&self) -> Vec<(u64, u64)> {
95 if self.excises() {
96 vec![(self.from, self.own.0), (self.own.1, self.to)]
97 } else {
98 vec![(self.from, self.to)]
99 }
100 }
101}
102
103#[derive(Debug, Clone, PartialEq, Eq, Hash)]
105#[non_exhaustive]
106pub enum CoverFrom {
107 Start,
109 Field(String),
111}
112
113#[derive(Debug, Clone, PartialEq, Eq, Hash)]
115#[non_exhaustive]
116pub enum CoverTo {
117 Here,
119 Before(String),
121 After(String),
123}