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
use crate::value::Value;

pub(crate) const EMPTY: usize = 0x01;
pub(crate) const LB_INC: usize = 0x02;
pub(crate) const UB_INC: usize = 0x04;
pub(crate) const LB_INF: usize = 0x08;
pub(crate) const UB_INF: usize = 0x10;


#[derive(Clone, Debug, PartialEq)]
pub struct Range<T> {
    pub(crate) lower: Option<T>,
    pub(crate) upper: Option<T>,
    pub(crate) inc_lower: bool,
    pub(crate) inc_upper: bool,
    pub(crate) empty: bool,
}

impl<T> From<std::ops::Range<T>> for Range<T> {
    fn from(src: std::ops::Range<T>) -> Range<T> {
        Range {
            lower: Some(src.start),
            upper: Some(src.end),
            inc_lower: true,
            inc_upper: false,
            empty: false,
        }
    }
}

impl<T: Into<Value>> From<std::ops::Range<T>> for Value {
    fn from(src: std::ops::Range<T>) -> Value {
        Range::from(src).into_value()
    }
}

impl<T> Range<T> {
    /// Constructor of the empty range
    pub fn empty() -> Range<T> {
        Range {
            lower: None,
            upper: None,
            inc_lower: true,
            inc_upper: false,
            empty: true,
        }
    }
    pub fn lower(&self) -> Option<&T> {
        self.lower.as_ref()
    }
    pub fn upper(&self) -> Option<&T> {
        self.upper.as_ref()
    }
    pub fn inc_lower(&self) -> bool {
        self.inc_lower
    }
    pub fn inc_upper(&self) -> bool {
        self.inc_upper
    }
    pub fn is_empty(&self) -> bool {
        self.empty
    }
}

impl<T: Into<Value>> Range<T> {
    pub fn into_value(self) -> Value {
        Value::Range(Range {
            lower: self.lower.map(|v| Box::new(v.into())),
            upper: self.upper.map(|v| Box::new(v.into())),
            inc_lower: self.inc_lower,
            inc_upper: self.inc_upper,
            empty: self.empty,
        })
    }
}