range_ranger/
relation.rs

1#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
2pub enum RangesRelation {
3    /// The first range is strictly before the second one with no overlap
4    ///
5    /// ```text
6    /// [ A ]
7    ///       [ B ]
8    /// ```
9    StrictlyBefore,
10
11    /// The first range is strictly after the second one with no overlap
12    ///
13    /// ```text
14    ///       [ A ]
15    /// [ B ]
16    /// ```
17    StrictlyAfter,
18
19    ///*
20    /// ```text
21    /// [ A ]
22    ///     [ B ]
23    /// ```
24    ////
25    Meets,
26
27    ///*
28    /// ```text
29    ///     [ A ]
30    /// [ B ]
31    /// ```
32    ////
33    IsMet,
34
35    ///*
36    /// ```text
37    /// [ A ]
38    ///   [ B ]
39    /// ```
40    ////
41    Overlaps,
42
43    ///*
44    /// ```text
45    ///   [ A ]
46    /// [ B ]
47    /// ```
48    ////
49    IsOverlapped,
50
51    ///*
52    /// ```text
53    /// [ A ]
54    /// [   B   ]
55    /// ```
56    ////
57    Starts,
58
59    ///*
60    /// ```text
61    /// [   A   ]
62    /// [ B ]
63    /// ```
64    ////
65    IsStarted,
66
67    ///*
68    /// ```text
69    /// [   A   ]
70    ///   [ B ]
71    /// ```
72    ////
73    StrictlyContains,
74
75    ///*
76    /// ```text
77    ///   [ A ]
78    /// [   B   ]
79    /// ```
80    ////
81    IsStrictlyContained,
82
83    ///*
84    /// ```text
85    ///     [ A ]
86    /// [   B   ]
87    /// ```
88    ////
89    Finishes,
90
91    ///*
92    /// ```text
93    /// [   A   ]
94    ///     [ B ]
95    /// ```
96    ////
97    IsFinished,
98
99    ///*
100    /// ```text
101    /// [ A ]
102    /// [ B ]
103    /// ```
104    ////
105    Equal,
106}
107
108impl RangesRelation {
109    /// Returns true if there is any type of overlap between the two ranges
110    ///
111    /// This is equivalend to all the relations except:
112    /// - [`RangesRelation::StrictlyBefore`]
113    /// - [`RangesRelation::StrictlyAfter`]
114    #[must_use]
115    pub fn intersects(&self) -> bool {
116        match self {
117            RangesRelation::StrictlyBefore | RangesRelation::StrictlyAfter => false,
118
119            RangesRelation::Overlaps
120            | RangesRelation::IsOverlapped
121            | RangesRelation::Meets
122            | RangesRelation::IsMet
123            | RangesRelation::Starts
124            | RangesRelation::IsStarted
125            | RangesRelation::StrictlyContains
126            | RangesRelation::IsStrictlyContained
127            | RangesRelation::Finishes
128            | RangesRelation::IsFinished
129            | RangesRelation::Equal => true,
130        }
131    }
132
133    /// Returns true if the ranges are completely disjoint
134    ///
135    /// This is equivalend to the relations:
136    /// - [`RangesRelation::StrictlyBefore`]
137    /// - [`RangesRelation::StrictlyAfter`]
138    #[must_use]
139    pub fn disjoint(&self) -> bool {
140        !self.intersects()
141    }
142
143    /// Returns true if the first range contains the second one.
144    ///
145    /// The equivalent relations are:
146    /// - [`RangesRelation::Equal`]
147    /// - [`RangesRelation::StrictlyContains`]
148    /// - [`RangesRelation::Started`] / [`RangesRelation::Finished`]
149    #[must_use]
150    pub fn contains(&self) -> bool {
151        match self {
152            RangesRelation::Equal
153            | RangesRelation::StrictlyContains
154            | RangesRelation::IsFinished
155            | RangesRelation::IsStarted => true,
156
157            RangesRelation::StrictlyBefore
158            | RangesRelation::StrictlyAfter
159            | RangesRelation::Overlaps
160            | RangesRelation::IsOverlapped
161            | RangesRelation::Meets
162            | RangesRelation::IsMet
163            | RangesRelation::Starts
164            | RangesRelation::IsStrictlyContained
165            | RangesRelation::Finishes => false,
166        }
167    }
168}