tenda_common/
span.rs

1use crate::source::IdentifiedSource;
2
3pub trait Span: Clone + std::fmt::Debug + PartialEq + tenda_reporting::Span {
4    fn start(&self) -> usize;
5    fn end(&self) -> usize;
6    fn source(&self) -> IdentifiedSource;
7    fn extract(&self, source: &str) -> String;
8}
9
10#[derive(Debug, Clone, PartialEq)]
11pub struct SourceSpan {
12    start: usize,
13    end: usize,
14    source: IdentifiedSource,
15    label: Option<String>,
16}
17
18impl SourceSpan {
19    pub fn new(start: usize, end: usize, source: IdentifiedSource) -> Self {
20        SourceSpan {
21            start,
22            end,
23            source,
24            label: None,
25        }
26    }
27
28    pub fn with_label(mut self, label: String) -> Self {
29        self.label = Some(label);
30        self
31    }
32
33    pub fn label(&self) -> Option<&String> {
34        self.label.as_ref()
35    }
36}
37
38impl tenda_reporting::Span for SourceSpan {
39    type SourceId = IdentifiedSource;
40
41    fn source(&self) -> &Self::SourceId {
42        &self.source
43    }
44
45    fn start(&self) -> usize {
46        self.start
47    }
48
49    fn end(&self) -> usize {
50        self.end
51    }
52}
53
54impl Span for SourceSpan {
55    fn start(&self) -> usize {
56        self.start
57    }
58
59    fn end(&self) -> usize {
60        self.end
61    }
62
63    fn source(&self) -> IdentifiedSource {
64        self.source
65    }
66
67    fn extract(&self, source: &str) -> String {
68        source[self.start..self.end].to_string()
69    }
70}