2021-09-20 21:37:26 +00:00
|
|
|
use miette::SourceSpan;
|
|
|
|
|
2021-06-30 01:42:56 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
pub struct Span {
|
|
|
|
pub start: usize,
|
|
|
|
pub end: usize,
|
|
|
|
}
|
|
|
|
|
2021-09-20 21:37:26 +00:00
|
|
|
impl From<Span> for SourceSpan {
|
|
|
|
fn from(s: Span) -> Self {
|
|
|
|
Self::new(s.start.into(), (s.end - s.start).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 01:42:56 +00:00
|
|
|
impl Span {
|
2021-07-03 03:35:15 +00:00
|
|
|
pub fn new(start: usize, end: usize) -> Span {
|
|
|
|
Span { start, end }
|
2021-06-30 01:42:56 +00:00
|
|
|
}
|
2021-07-01 00:01:04 +00:00
|
|
|
|
|
|
|
pub fn unknown() -> Span {
|
2021-07-03 03:35:15 +00:00
|
|
|
Span { start: 0, end: 0 }
|
2021-07-01 00:01:04 +00:00
|
|
|
}
|
2021-07-22 20:45:23 +00:00
|
|
|
|
|
|
|
pub fn offset(&self, offset: usize) -> Span {
|
|
|
|
Span {
|
|
|
|
start: self.start - offset,
|
|
|
|
end: self.end - offset,
|
|
|
|
}
|
|
|
|
}
|
2021-06-30 01:42:56 +00:00
|
|
|
}
|
2021-09-02 01:29:43 +00:00
|
|
|
|
|
|
|
pub fn span(spans: &[Span]) -> Span {
|
|
|
|
let length = spans.len();
|
|
|
|
|
|
|
|
if length == 0 {
|
|
|
|
Span::unknown()
|
|
|
|
} else if length == 1 {
|
|
|
|
spans[0]
|
|
|
|
} else {
|
|
|
|
Span {
|
|
|
|
start: spans[0].start,
|
|
|
|
end: spans[length - 1].end,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|