oni_comb_parser_rs/utils/
range.rs

1use std::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
2
3pub enum Bound<'a, T: 'a> {
4  Excluded(&'a T),
5  Included(&'a T),
6  Unbounded,
7}
8
9pub trait RangeArgument<T> {
10  fn start(&self) -> Bound<T>;
11  fn end(&self) -> Bound<T>;
12}
13
14// ..
15impl<T> RangeArgument<T> for RangeFull {
16  fn start(&self) -> Bound<T> {
17    Bound::Unbounded
18  }
19
20  fn end(&self) -> Bound<T> {
21    Bound::Unbounded
22  }
23}
24
25// start..end
26impl<T> RangeArgument<T> for Range<T> {
27  fn start(&self) -> Bound<T> {
28    Bound::Included(&self.start)
29  }
30
31  fn end(&self) -> Bound<T> {
32    Bound::Excluded(&self.end)
33  }
34}
35
36// start..=end
37impl<T> RangeArgument<T> for RangeInclusive<T> {
38  fn start(&self) -> Bound<T> {
39    Bound::Included(RangeInclusive::start(self))
40  }
41
42  fn end(&self) -> Bound<T> {
43    Bound::Included(RangeInclusive::end(self))
44  }
45}
46
47// start..
48impl<T> RangeArgument<T> for RangeFrom<T> {
49  fn start(&self) -> Bound<T> {
50    Bound::Included(&self.start)
51  }
52
53  fn end(&self) -> Bound<T> {
54    Bound::Unbounded
55  }
56}
57
58// ..end
59impl<T> RangeArgument<T> for RangeTo<T> {
60  fn start(&self) -> Bound<T> {
61    Bound::Unbounded
62  }
63
64  fn end(&self) -> Bound<T> {
65    Bound::Excluded(&self.end)
66  }
67}
68
69// ..=end
70impl<T> RangeArgument<T> for RangeToInclusive<T> {
71  fn start(&self) -> Bound<T> {
72    Bound::Unbounded
73  }
74
75  fn end(&self) -> Bound<T> {
76    Bound::Included(&self.end)
77  }
78}
79
80impl RangeArgument<usize> for usize {
81  fn start(&self) -> Bound<usize> {
82    Bound::Included(self)
83  }
84
85  fn end(&self) -> Bound<usize> {
86    Bound::Included(self)
87  }
88}