oni_comb_parser_rs/utils/
range.rs

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};

pub enum Bound<'a, T: 'a> {
  Excluded(&'a T),
  Included(&'a T),
  Unbounded,
}

pub trait RangeArgument<T> {
  fn start(&self) -> Bound<T>;
  fn end(&self) -> Bound<T>;
}

// ..
impl<T> RangeArgument<T> for RangeFull {
  fn start(&self) -> Bound<T> {
    Bound::Unbounded
  }

  fn end(&self) -> Bound<T> {
    Bound::Unbounded
  }
}

// start..end
impl<T> RangeArgument<T> for Range<T> {
  fn start(&self) -> Bound<T> {
    Bound::Included(&self.start)
  }

  fn end(&self) -> Bound<T> {
    Bound::Excluded(&self.end)
  }
}

// start..=end
impl<T> RangeArgument<T> for RangeInclusive<T> {
  fn start(&self) -> Bound<T> {
    Bound::Included(RangeInclusive::start(self))
  }

  fn end(&self) -> Bound<T> {
    Bound::Included(RangeInclusive::end(self))
  }
}

// start..
impl<T> RangeArgument<T> for RangeFrom<T> {
  fn start(&self) -> Bound<T> {
    Bound::Included(&self.start)
  }

  fn end(&self) -> Bound<T> {
    Bound::Unbounded
  }
}

// ..end
impl<T> RangeArgument<T> for RangeTo<T> {
  fn start(&self) -> Bound<T> {
    Bound::Unbounded
  }

  fn end(&self) -> Bound<T> {
    Bound::Excluded(&self.end)
  }
}

// ..=end
impl<T> RangeArgument<T> for RangeToInclusive<T> {
  fn start(&self) -> Bound<T> {
    Bound::Unbounded
  }

  fn end(&self) -> Bound<T> {
    Bound::Included(&self.end)
  }
}

impl RangeArgument<usize> for usize {
  fn start(&self) -> Bound<usize> {
    Bound::Included(self)
  }

  fn end(&self) -> Bound<usize> {
    Bound::Included(self)
  }
}