gel_protocol/model/
range.rs

1use crate::value::Value;
2
3pub(crate) const EMPTY: usize = 0x01;
4pub(crate) const LB_INC: usize = 0x02;
5pub(crate) const UB_INC: usize = 0x04;
6pub(crate) const LB_INF: usize = 0x08;
7pub(crate) const UB_INF: usize = 0x10;
8
9#[derive(Clone, Debug, PartialEq)]
10#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct Range<T> {
12    pub(crate) lower: Option<T>,
13    pub(crate) upper: Option<T>,
14    pub(crate) inc_lower: bool,
15    pub(crate) inc_upper: bool,
16    pub(crate) empty: bool,
17}
18
19impl<T> From<std::ops::Range<T>> for Range<T> {
20    fn from(src: std::ops::Range<T>) -> Range<T> {
21        Range {
22            lower: Some(src.start),
23            upper: Some(src.end),
24            inc_lower: true,
25            inc_upper: false,
26            empty: false,
27        }
28    }
29}
30
31impl<T: Into<Value>> From<std::ops::Range<T>> for Value {
32    fn from(src: std::ops::Range<T>) -> Value {
33        Range::from(src).into_value()
34    }
35}
36
37impl<T> Range<T> {
38    /// Constructor of the empty range
39    pub fn empty() -> Range<T> {
40        Range {
41            lower: None,
42            upper: None,
43            inc_lower: true,
44            inc_upper: false,
45            empty: true,
46        }
47    }
48    pub fn lower(&self) -> Option<&T> {
49        self.lower.as_ref()
50    }
51    pub fn upper(&self) -> Option<&T> {
52        self.upper.as_ref()
53    }
54    pub fn inc_lower(&self) -> bool {
55        self.inc_lower
56    }
57    pub fn inc_upper(&self) -> bool {
58        self.inc_upper
59    }
60    pub fn is_empty(&self) -> bool {
61        self.empty
62    }
63}
64
65impl<T: Into<Value>> Range<T> {
66    pub fn into_value(self) -> Value {
67        Value::Range(Range {
68            lower: self.lower.map(|v| Box::new(v.into())),
69            upper: self.upper.map(|v| Box::new(v.into())),
70            inc_lower: self.inc_lower,
71            inc_upper: self.inc_upper,
72            empty: self.empty,
73        })
74    }
75}