1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use serde::{Deserialize, Serialize};
use std::cmp::min;
use std::ops::Range;

// #[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize)]
// pub struct Span {
//     pub input: CowStr,
//     pub start: usize,
//     pub end: usize,
// }

// impl Span {
//     pub fn new(input: CowStr, start: usize, end: usize) -> Self {
//         Span { input, start, end }
//     }
//
//     pub fn get_with_margin(&self, margin: usize) -> &str {
//         &self.input[self.start.checked_sub(margin).unwrap_or_default()
//             ..min(self.end + margin, self.input.len())]
//     }
// }

// impl<'a> From<pest::Span<'a>> for Span {
//     fn from(value: pest::Span) -> Self {
//         Span {
//             input: value.get_input().into(),
//             start: value.start(),
//             end: value.end(),
//         }
//     }
// }

#[derive(Debug, PartialEq, Default, Clone, Serialize, Deserialize)]
pub struct Span {
    pub range: Range<usize>,
}

impl Span {
    pub fn new(start: usize, end: usize) -> Self {
        Self { range: start..end }
    }

    pub fn get_with_margin<'a>(&self, input: &'a str, margin: usize) -> &'a str {
        &input[self.range.start.checked_sub(margin).unwrap_or_default()
            ..min(self.range.end + margin, input.len())]
    }
}

impl From<pest::Span<'_>> for Span {
    fn from(value: pest::Span) -> Self {
        Self {
            range: value.start()..value.end(),
        }
    }
}