rscel/compiler/
source_range.rs

1use serde::{Deserialize, Serialize};
2
3use super::source_location::SourceLocation;
4
5#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub struct SourceRange {
7    start: SourceLocation,
8    end: SourceLocation,
9}
10
11impl SourceRange {
12    pub fn new(start: SourceLocation, end: SourceLocation) -> SourceRange {
13        SourceRange { start, end }
14    }
15
16    pub fn start(&self) -> SourceLocation {
17        self.start
18    }
19
20    pub fn end(&self) -> SourceLocation {
21        self.end
22    }
23
24    pub fn surrounding(self, other: SourceRange) -> SourceRange {
25        SourceRange::new(self.start.min(other.start), self.end.max(other.end))
26    }
27}
28
29#[cfg(test)]
30mod test {
31    use crate::compiler::source_location::SourceLocation;
32
33    use super::SourceRange;
34
35    #[test]
36    fn test_surrounding() {
37        let p = SourceRange::new(SourceLocation::new(0, 3), SourceLocation::new(0, 5)).surrounding(
38            SourceRange::new(SourceLocation::new(0, 4), SourceLocation::new(0, 7)),
39        );
40
41        assert_eq!(
42            p,
43            SourceRange::new(SourceLocation::new(0, 3), SourceLocation::new(0, 7))
44        );
45    }
46}