use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Span {
pub start: usize,
pub end: usize,
pub line: usize,
pub column: usize,
}
impl Span {
pub fn new(line: usize, column: usize) -> Self {
Self {
start: 0,
end: 0,
line,
column,
}
}
pub fn at(start: usize, end: usize, line: usize, column: usize) -> Self {
Self {
start,
end,
line,
column,
}
}
pub fn len(&self) -> usize {
self.end.saturating_sub(self.start)
}
pub fn is_empty(&self) -> bool {
self.end <= self.start
}
pub fn merge(self, other: Span) -> Span {
let (lo, _hi) = if self.start <= other.start {
(self, other)
} else {
(other, self)
};
Span {
start: self.start.min(other.start),
end: self.end.max(other.end),
line: lo.line,
column: lo.column,
}
}
}