1#[derive(Debug, PartialEq, Clone, Copy)]
3pub struct Range {
4 pub start: usize,
6 pub end: usize,
8}
9
10impl Range {
11 pub fn new(start: usize, end: usize) -> Self {
12 Range { start, end }
13 }
14
15 pub fn from_byte_index(pos: usize) -> Self {
16 Range::new(pos, pos)
17 }
18}
19
20impl Ranged for Range {
21 fn range(&self) -> Range {
22 *self
23 }
24}
25
26pub trait Ranged {
28 fn range(&self) -> Range;
30
31 fn start(&self) -> usize {
33 self.range().start
34 }
35
36 fn end(&self) -> usize {
38 self.range().end
39 }
40
41 fn text<'a>(&self, text: &'a str) -> &'a str {
43 let range = self.range();
44 &text[range.start..range.end]
45 }
46
47 fn width(&self) -> usize {
49 let range = self.range();
50 range.end - range.start
51 }
52}