Skip to main content

rigsql_core/
span.rs

1/// Byte-offset span within source text.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3pub struct Span {
4    /// Start byte offset (inclusive).
5    pub start: u32,
6    /// End byte offset (exclusive).
7    pub end: u32,
8}
9
10impl Span {
11    pub fn new(start: u32, end: u32) -> Self {
12        debug_assert!(start <= end);
13        Self { start, end }
14    }
15
16    pub fn len(&self) -> u32 {
17        self.end - self.start
18    }
19
20    pub fn is_empty(&self) -> bool {
21        self.start == self.end
22    }
23
24    /// Merge two spans into one covering both.
25    pub fn merge(self, other: Span) -> Span {
26        Span {
27            start: self.start.min(other.start),
28            end: self.end.max(other.end),
29        }
30    }
31
32    /// Extract the text this span covers from source.
33    pub fn text<'a>(&self, source: &'a str) -> &'a str {
34        &source[self.start as usize..self.end as usize]
35    }
36}