Skip to main content

ling_ast/
span.rs

1//! Source span — byte-offset range within a source file.
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
6pub struct Span {
7    pub start: u32,
8    pub end:   u32,
9}
10
11impl Span {
12    pub const DUMMY: Self = Self { start: 0, end: 0 };
13
14    pub fn new(start: u32, end: u32) -> Self { Self { start, end } }
15
16    pub fn len(self) -> u32 { self.end.saturating_sub(self.start) }
17    pub fn is_empty(self) -> bool { self.len() == 0 }
18
19    pub fn union(self, other: Self) -> Self {
20        Self { start: self.start.min(other.start), end: self.end.max(other.end) }
21    }
22}
23
24impl std::fmt::Display for Span {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        write!(f, "{}..{}", self.start, self.end)
27    }
28}