use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Span {
pub start_line: u32,
pub start_col: u32,
pub end_line: u32,
pub end_col: u32,
}
impl Span {
pub fn new(start_line: u32, start_col: u32, end_line: u32, end_col: u32) -> Self {
Self {
start_line,
start_col,
end_line,
end_col,
}
}
pub fn point(line: u32, col: u32) -> Self {
Self::new(line, col, line, col)
}
pub fn line_count(&self) -> u32 {
self.end_line.saturating_sub(self.start_line) + 1
}
pub fn contains(&self, line: u32, col: u32) -> bool {
if line < self.start_line || line > self.end_line {
return false;
}
if line == self.start_line && col < self.start_col {
return false;
}
if line == self.end_line && col > self.end_col {
return false;
}
true
}
}
impl std::fmt::Display for Span {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}:{}-{}:{}",
self.start_line, self.start_col, self.end_line, self.end_col
)
}
}