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
//! Holds the interval type and associated methods.

use crate::impls::ITrait;
use crate::ops::IntervalOps;
use crate::IntervalType;
use std::fmt::Display;

/// The interval type, main type of this library.
pub struct Interval<T: ITrait> {
    pub(crate) lower: Option<T>,
    pub(crate) upper: Option<T>,
    pub(crate) itype: IntervalType,
    pub(crate) current: Option<T>,
}

impl<T: ITrait> PartialEq for Interval<T> {
    fn eq(&self, other: &Self) -> bool {
        self.to_string() == other.to_string()
    }
}

impl<T: ITrait> Eq for Interval<T> {}

impl<T: ITrait> Display for Interval<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.empty() {
            return write!(f, "()");
        };
        use IntervalType::*;
        match self.itype {
            Open => write!(
                f,
                "({}, {})",
                self.lower.as_ref().unwrap(),
                self.upper.as_ref().unwrap()
            ),
            Closed => write!(
                f,
                "[{}, {}]",
                self.lower.as_ref().unwrap(),
                self.upper.as_ref().unwrap()
            ),
            Empty => write!(f, "()"),
            Singleton => write!(f, "[{}]", self.lower.as_ref().unwrap()),
            OpenClosed => write!(
                f,
                "({}, {}]",
                self.lower.as_ref().unwrap(),
                self.upper.as_ref().unwrap()
            ),
            ClosedOpen => write!(
                f,
                "[{}, {})",
                self.lower.as_ref().unwrap(),
                self.upper.as_ref().unwrap()
            ),
        }
    }
}