Skip to main content

bt_rs/common/
tool_version.rs

1// tool_version.rs : src/common
2
3use std::cmp::Ordering;
4
5
6/// Semantic version components parsed from a tool's `--version` output.
7#[derive(Clone)]
8#[derive(Debug)]
9pub struct ToolVersion {
10    pub major : u32,
11    pub minor : u32,
12    pub patch : u32,
13    pub build : String,
14}
15
16
17// API functions
18
19impl ToolVersion {
20}
21
22
23// Mutating methods
24
25impl ToolVersion {
26}
27
28
29// Non-mutating methods
30
31impl ToolVersion {
32    fn cmp_components(
33        &self,
34        major : u32,
35        minor : u32,
36        patch : u32,
37        build : &str,
38    ) -> Ordering {
39        self.major
40            .cmp(&major)
41            .then_with(|| self.minor.cmp(&minor))
42            .then_with(|| self.patch.cmp(&patch))
43            .then_with(|| self.build.as_str().cmp(build))
44    }
45}
46
47
48// Implementation
49
50impl ToolVersion {
51}
52
53
54// Trait implementations
55
56impl Eq for ToolVersion {
57}
58
59impl PartialEq for ToolVersion {
60    fn eq(
61        &self,
62        other : &Self,
63    ) -> bool {
64        self.cmp(other) == Ordering::Equal
65    }
66}
67
68impl PartialEq<(u32,)> for ToolVersion {
69    fn eq(
70        &self,
71        other : &(u32,),
72    ) -> bool {
73        self.cmp_components(other.0, 0, 0, "") == Ordering::Equal
74    }
75}
76
77impl PartialEq<(u32, u32)> for ToolVersion {
78    fn eq(
79        &self,
80        other : &(u32, u32),
81    ) -> bool {
82        self.cmp_components(other.0, other.1, 0, "") == Ordering::Equal
83    }
84}
85
86impl PartialEq<(u32, u32, u32)> for ToolVersion {
87    fn eq(
88        &self,
89        other : &(u32, u32, u32),
90    ) -> bool {
91        self.cmp_components(other.0, other.1, other.2, "") == Ordering::Equal
92    }
93}
94
95impl PartialEq<(u32, u32, u32, &str)> for ToolVersion {
96    fn eq(
97        &self,
98        other : &(u32, u32, u32, &str),
99    ) -> bool {
100        self.cmp_components(other.0, other.1, other.2, other.3) == Ordering::Equal
101    }
102}
103
104impl Ord for ToolVersion {
105    fn cmp(
106        &self,
107        other : &Self,
108    ) -> Ordering {
109        self.cmp_components(other.major, other.minor, other.patch, other.build.as_str())
110    }
111}
112
113impl PartialOrd for ToolVersion {
114    fn partial_cmp(
115        &self,
116        other : &Self,
117    ) -> Option<Ordering> {
118        Some(self.cmp(other))
119    }
120}
121
122impl PartialOrd<(u32,)> for ToolVersion {
123    fn partial_cmp(
124        &self,
125        other : &(u32,),
126    ) -> Option<Ordering> {
127        Some(self.cmp_components(other.0, 0, 0, ""))
128    }
129}
130
131impl PartialOrd<(u32, u32)> for ToolVersion {
132    fn partial_cmp(
133        &self,
134        other : &(u32, u32),
135    ) -> Option<Ordering> {
136        Some(self.cmp_components(other.0, other.1, 0, ""))
137    }
138}
139
140impl PartialOrd<(u32, u32, u32)> for ToolVersion {
141    fn partial_cmp(
142        &self,
143        other : &(u32, u32, u32),
144    ) -> Option<Ordering> {
145        Some(self.cmp_components(other.0, other.1, other.2, ""))
146    }
147}
148
149impl PartialOrd<(u32, u32, u32, &str)> for ToolVersion {
150    fn partial_cmp(
151        &self,
152        other : &(u32, u32, u32, &str),
153    ) -> Option<Ordering> {
154        Some(self.cmp_components(other.0, other.1, other.2, other.3))
155    }
156}
157
158
159#[cfg(test)]
160mod tests {
161    #![allow(non_snake_case)]
162
163    use super::ToolVersion;
164
165    use std::cmp::Ordering;
166
167
168    #[test]
169    fn TEST_ToolVersion_Ord_AGAINST_ToolVersion() {
170        let a = ToolVersion {
171            major : 1,
172            minor : 94,
173            patch : 0,
174            build : String::new(),
175        };
176        let b = ToolVersion {
177            major : 1,
178            minor : 94,
179            patch : 1,
180            build : String::new(),
181        };
182
183        assert_eq!(a.cmp(&b), Ordering::Less);
184        assert!(a < b);
185    }
186
187    #[test]
188    fn TEST_ToolVersion_PartialOrd_AGAINST_major() {
189        let version = ToolVersion {
190            major : 2,
191            minor : 0,
192            patch : 0,
193            build : String::new(),
194        };
195
196        assert!(version > (1,));
197        assert!(version >= (2,));
198        assert_eq!(version.partial_cmp(&(2,)), Some(Ordering::Equal));
199    }
200
201    #[test]
202    fn TEST_ToolVersion_PartialOrd_AGAINST_major_minor() {
203        let version = ToolVersion {
204            major : 1,
205            minor : 94,
206            patch : 0,
207            build : String::new(),
208        };
209
210        assert!(version >= (1, 94));
211        assert!(version >= (1, 93));
212        assert!(version < (1, 95));
213        assert!(version < (2, 0));
214    }
215
216    #[test]
217    fn TEST_ToolVersion_PartialOrd_AGAINST_major_minor_patch() {
218        let version = ToolVersion {
219            major : 1,
220            minor : 96,
221            patch : 0,
222            build : String::new(),
223        };
224
225        assert!(version >= (1, 96, 0));
226        assert!(version < (1, 96, 1));
227    }
228
229    #[test]
230    fn TEST_ToolVersion_PartialOrd_AGAINST_major_minor_patch_build() {
231        let version = ToolVersion {
232            major : 1,
233            minor : 96,
234            patch : 0,
235            build : "abc".to_string(),
236        };
237
238        assert!(version >= (1, 96, 0, "abc"));
239        assert!(version < (1, 96, 0, "abd"));
240    }
241}
242
243
244// ///////////////////////////// end of file //////////////////////////// //