timekeep_rs/
bound.rs

1//! A module containing the `Bound` struct and its implementations.
2//! A bound can either include or not include the value: `Included(T)` and `Excluded(T)`.
3//! 
4//! # Examples
5//! ```
6//! use timekeep_rs::Bound;
7//! 
8//! let included_bound = Bound::Included(5);
9//! let excluded_bound = Bound::Excluded(5);
10//! ```
11//!
12
13#[derive(Debug, PartialOrd, PartialEq, Clone, Copy)]
14/// Represents a boundary of an interval.
15/// Can be either inclusive (closed) or exclusive (open).
16pub enum Bound<T> {
17    /// Represents an inclusive boundary, meaning the value itself is included in the interval.
18    Included(T),
19    /// Represents an exclusive boundary, meaning the value itself is excluded from the interval.
20    Excluded(T),
21}
22
23/// Methods for `Bound`.
24impl<T> Bound<T> {
25    /// Returns a reference to the value contained within the `Bound`.
26    ///
27    /// # Examples
28    ///
29    /// ```
30    /// use timekeep_rs::Bound;
31    ///
32    /// let inclusive_bound = Bound::Included(15);
33    /// assert_eq!(inclusive_bound.value(), &15);
34    ///
35    /// let exclusive_bound = Bound::Excluded(20);
36    /// assert_eq!(exclusive_bound.value(), &20);
37    /// ```
38    pub fn value(&self) -> &T {
39        match self {
40            Bound::Included(value) => value,
41            Bound::Excluded(value) => value,
42        }
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::Bound;
49
50    #[test]
51    fn test_included() {
52        let bound = Bound::Included(5);
53        assert_eq!(bound, Bound::Included(5));
54        if let Bound::Included(value) = bound {
55            assert_eq!(value, 5);
56        } else {
57            panic!("Expected Bound::Included");
58        }
59    }
60
61    #[test]
62    fn test_excluded() {
63        let bound = Bound::Excluded(10);
64        assert_eq!(bound, Bound::Excluded(10));
65        if let Bound::Excluded(value) = bound {
66            assert_eq!(value, 10);
67        } else {
68            panic!("Expected Bound::Excluded");
69        }
70    }
71
72    #[test]
73    fn test_value_included() {
74        let bound = Bound::Included(15);
75        assert_eq!(bound.value(), &15);
76    }
77
78    #[test]
79    fn test_value_excluded() {
80        let bound = Bound::Excluded(20);
81        assert_eq!(bound.value(), &20);
82    }
83
84    #[test]
85    fn test_partial_eq() {
86        let bound1 = Bound::Included(25);
87        let bound2 = Bound::Included(25);
88        assert_eq!(bound1, bound2);
89
90        let bound3 = Bound::Excluded(30);
91        let bound4 = Bound::Excluded(30);
92        assert_eq!(bound3, bound4);
93
94        let bound5 = Bound::Included(35);
95        let bound6 = Bound::Excluded(35);
96        assert_ne!(bound5, bound6);
97    }
98
99    #[test]
100    fn test_partial_ord() {
101        let bound1 = Bound::Included(5);
102        let bound2 = Bound::Included(10);
103        assert!(bound1 < bound2);
104
105        let bound3 = Bound::Excluded(15);
106        let bound4 = Bound::Excluded(20);
107        assert!(bound3 < bound4);
108
109        let bound5 = Bound::Included(25);
110        let bound6 = Bound::Excluded(25);
111        assert!(bound5 < bound6);
112    }
113}