#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Span {
pub start: usize,
pub end: usize,
pub line: u32,
pub column: u32,
}
impl Span {
#[must_use]
pub const fn new(start: usize, end: usize, line: u32, column: u32) -> Self {
Self {
start,
end,
line,
column,
}
}
#[must_use]
pub const fn contains(&self, offset: usize) -> bool {
offset >= self.start && offset < self.end
}
#[must_use]
pub fn merge(&self, other: &Self) -> Self {
use core::cmp::Ordering;
Self {
start: self.start.min(other.start),
end: self.end.max(other.end),
line: self.line.min(other.line),
column: match self.line.cmp(&other.line) {
Ordering::Less => self.column,
Ordering::Greater => other.column,
Ordering::Equal => self.column.min(other.column),
},
}
}
}