Skip to main content

layline_codegen/model/
coverage.rs

1//! The bytes a checksum covers.
2
3use alloc::string::String;
4use alloc::vec;
5use alloc::vec::Vec;
6
7/// The bytes a checksum covers, as `over = from..to`.
8///
9/// The range may include bytes after the checksum, but never the checksum itself.
10#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11#[non_exhaustive]
12pub struct Coverage {
13    /// Where the range starts.
14    pub from: CoverFrom,
15    /// Where the range ends.
16    pub to: CoverTo,
17}
18
19impl Coverage {
20    /// `over = <from>..<to>`.
21    #[must_use]
22    pub fn new(from: CoverFrom, to: CoverTo) -> Self {
23        Self { from, to }
24    }
25
26    /// `over = ..`: from the start of the message to the checksum.
27    #[must_use]
28    pub fn whole() -> Self {
29        Self { from: CoverFrom::Start, to: CoverTo::Here }
30    }
31
32    /// `over = field..`: from `field` to the checksum.
33    #[must_use]
34    pub fn from_field(field: &str) -> Self {
35        Self { from: CoverFrom::Field(String::from(field)), to: CoverTo::Here }
36    }
37
38    /// Sets the end.
39    #[must_use]
40    pub fn to(self, to: CoverTo) -> Self {
41        Self { to, ..self }
42    }
43
44    /// Resolves the range for a checksum at `own`.
45    ///
46    /// `at` returns a field's `(start, end)`. Returns `None` if a name does not resolve.
47    #[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/// A resolved [`Coverage`], in the caller's units.
68#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69#[non_exhaustive]
70pub struct Covered {
71    /// Start of the range.
72    pub from: u64,
73    /// End of the range, exclusive.
74    pub to: u64,
75    /// The checksum's own `(start, end)`.
76    pub own: (u64, u64),
77}
78
79impl Covered {
80    /// The range `from..to` for a checksum at `own`.
81    #[must_use]
82    pub fn new(from: u64, to: u64, own: (u64, u64)) -> Self {
83        Self { from, to, own }
84    }
85
86    /// Whether the checksum sits inside the range and must be skipped.
87    #[must_use]
88    pub fn excises(&self) -> bool {
89        self.from <= self.own.0 && self.to > self.own.0
90    }
91
92    /// The covered spans, without the checksum's own bytes.
93    #[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/// Where a checksum's range starts.
104#[derive(Debug, Clone, PartialEq, Eq, Hash)]
105#[non_exhaustive]
106pub enum CoverFrom {
107    /// The first byte of the message.
108    Start,
109    /// The first byte of a named field, such as the one after a sync word.
110    Field(String),
111}
112
113/// Where a checksum's range ends.
114#[derive(Debug, Clone, PartialEq, Eq, Hash)]
115#[non_exhaustive]
116pub enum CoverTo {
117    /// Just before the checksum: `over = a..`. The default.
118    Here,
119    /// Just before a named field: `over = a..b`.
120    Before(String),
121    /// Just after a named field: `over = a..=b`.
122    After(String),
123}