1 //! Module for the version manifest.
2 //!
3 //! A version manifest can be used to configure and specify how versions are parsed and compared.
4 //! For example, you can configure the maximum depth of a version number, and set whether text
5 //! parts are ignored in a version string.
6 
7 /// Version manifest (configuration).
8 ///
9 /// A manifest (configuration) that is used respectively when parsing and comparing version strings.
10 #[derive(Debug, PartialEq)]
11 pub struct VersionManifest {
12     /// The maximum depth of a version number. This specifies the maximum number of parts.
13     max_depth: Option<usize>,
14 
15     /// True to ignore text parts in version strings.
16     ignore_text: bool,
17 }
18 
19 /// Version manifest implementation.
20 impl VersionManifest {
21     /// Constructor.
22     ///
23     /// # Examples
24     ///
25     /// ```
26     /// use version_compare::VersionManifest;
27     ///
28     /// let mut manifest = VersionManifest::new();
29     ///
30     /// // Ignore text parts
31     /// manifest.set_ignore_text(true);
32     /// ```
new() -> Self33     pub fn new() -> Self {
34         VersionManifest {
35             max_depth: None,
36             ignore_text: false,
37         }
38     }
39 
40     /// The maximum depth of a version number.
41     /// None if no depth is configured.
42     ///
43     /// # Examples
44     ///
45     /// ```
46     /// use version_compare::VersionManifest;
47     ///
48     /// let manifest = VersionManifest::new();
49     ///
50     /// match manifest.max_depth() {
51     ///     &Some(depth) => println!("Maximum depth of {}", depth),
52     ///     &None => println!("No maximum depth")
53     /// }
54     /// ```
max_depth(&self) -> &Option<usize>55     pub fn max_depth(&self) -> &Option<usize> {
56         &self.max_depth
57     }
58 
59     /// The maximum depth of a version number as numerical value.
60     /// Zero is returned if no depth is configured.
61     ///
62     /// # Examples
63     ///
64     /// ```
65     /// use version_compare::VersionManifest;
66     ///
67     /// let manifest = VersionManifest::new();
68     ///
69     /// println!("Maximum depth of {}", manifest.max_depth_number());
70     /// ```
max_depth_number(&self) -> usize71     pub fn max_depth_number(&self) -> usize {
72         if self.max_depth.is_some() {
73             self.max_depth.unwrap()
74         } else {
75             0
76         }
77     }
78 
79     /// Set the maximum depth of a version number.
80     ///
81     /// # Examples
82     ///
83     /// ```
84     /// use version_compare::VersionManifest;
85     ///
86     /// let mut manifest = VersionManifest::new();
87     ///
88     /// // Set the maximum depth to 3
89     /// manifest.set_max_depth(Some(3));
90     ///
91     /// // Don't use a maximum depth
92     /// manifest.set_max_depth(None);
93     /// ```
set_max_depth(&mut self, max_depth: Option<usize>)94     pub fn set_max_depth(&mut self, max_depth: Option<usize>) {
95         if max_depth.is_some() && max_depth.unwrap() > 0 {
96             self.max_depth = max_depth;
97         } else {
98             self.max_depth = None;
99         }
100     }
101 
102     /// Set the maximum depth of a version number.
103     /// Use zero to disable the maximum depth.
104     ///
105     /// # Examples
106     ///
107     /// ```
108     /// use version_compare::VersionManifest;
109     ///
110     /// let mut manifest = VersionManifest::new();
111     ///
112     /// // Set the maximum depth to 3
113     /// manifest.set_max_depth_number(3);
114     ///
115     /// // Don't use a maximum depth
116     /// manifest.set_max_depth_number(0);
117     /// ```
set_max_depth_number(&mut self, max_depth: usize)118     pub fn set_max_depth_number(&mut self, max_depth: usize) {
119         if max_depth > 0 {
120             self.max_depth = Some(max_depth);
121         } else {
122             self.max_depth = None;
123         }
124     }
125 
126     /// Check whether there's a maximum configured depth.
127     ///
128     /// # Examples
129     ///
130     /// ```
131     /// use version_compare::VersionManifest;
132     ///
133     /// let mut manifest = VersionManifest::new();
134     ///
135     /// assert!(!manifest.has_max_depth());
136     ///
137     /// manifest.set_max_depth(Some(3));
138     /// assert!(manifest.has_max_depth());
139     /// ```
has_max_depth(&self) -> bool140     pub fn has_max_depth(&self) -> bool {
141         self.max_depth.is_some() && self.max_depth.unwrap() > 0
142     }
143 
144     /// Check whether to ignore text parts in version numbers.
145     ///
146     /// # Examples
147     ///
148     /// ```
149     /// use version_compare::VersionManifest;
150     ///
151     /// let manifest = VersionManifest::new();
152     ///
153     /// if manifest.ignore_text() {
154     ///     println!("Text parts are ignored");
155     /// } else {
156     ///     println!("Text parts are not ignored");
157     /// }
158     /// ```
ignore_text(&self) -> bool159     pub fn ignore_text(&self) -> bool {
160         self.ignore_text
161     }
162 
163     /// Set whether to ignore text parts.
164     ///
165     /// # Examples
166     ///
167     /// ```
168     /// use version_compare::VersionManifest;
169     ///
170     /// let mut manifest = VersionManifest::new();
171     ///
172     /// // Ignore text parts
173     /// manifest.set_ignore_text(true);
174     ///
175     /// // Don't ignore text parts
176     /// manifest.set_ignore_text(false);
177     /// ```
set_ignore_text(&mut self, ignore_text: bool)178     pub fn set_ignore_text(&mut self, ignore_text: bool) {
179         self.ignore_text = ignore_text;
180     }
181 }
182 
183 #[cfg_attr(tarpaulin, skip)]
184 #[cfg(test)]
185 mod tests {
186     use crate::version_manifest::VersionManifest;
187 
188     #[test]
max_depth()189     fn max_depth() {
190         let mut manifest = VersionManifest::new();
191 
192         manifest.max_depth = Some(1);
193         assert_eq!(manifest.max_depth(), &Some(1));
194 
195         manifest.max_depth = Some(3);
196         assert_eq!(manifest.max_depth(), &Some(3));
197 
198         manifest.max_depth = None;
199         assert_eq!(manifest.max_depth(), &None);
200     }
201 
202     #[test]
max_depth_number()203     fn max_depth_number() {
204         let mut manifest = VersionManifest::new();
205 
206         manifest.max_depth = Some(1);
207         assert_eq!(manifest.max_depth_number(), 1);
208 
209         manifest.max_depth = Some(3);
210         assert_eq!(manifest.max_depth_number(), 3);
211 
212         manifest.max_depth = None;
213         assert_eq!(manifest.max_depth_number(), 0);
214     }
215 
216     #[test]
set_max_depth()217     fn set_max_depth() {
218         let mut manifest = VersionManifest::new();
219 
220         manifest.set_max_depth(Some(1));
221         assert_eq!(manifest.max_depth, Some(1));
222 
223         manifest.set_max_depth(Some(3));
224         assert_eq!(manifest.max_depth, Some(3));
225 
226         manifest.set_max_depth(Some(0));
227         assert_eq!(manifest.max_depth, None);
228 
229         manifest.set_max_depth(None);
230         assert_eq!(manifest.max_depth, None);
231     }
232 
233     #[test]
set_max_depth_number()234     fn set_max_depth_number() {
235         let mut manifest = VersionManifest::new();
236 
237         manifest.set_max_depth_number(1);
238         assert_eq!(manifest.max_depth, Some(1));
239 
240         manifest.set_max_depth_number(3);
241         assert_eq!(manifest.max_depth, Some(3));
242 
243         manifest.set_max_depth_number(0);
244         assert_eq!(manifest.max_depth, None);
245     }
246 
247     #[test]
has_max_depth()248     fn has_max_depth() {
249         let mut manifest = VersionManifest::new();
250 
251         manifest.max_depth = Some(1);
252         assert!(manifest.has_max_depth());
253 
254         manifest.max_depth = Some(3);
255         assert!(manifest.has_max_depth());
256 
257         manifest.max_depth = None;
258         assert!(!manifest.has_max_depth());
259     }
260 
261     #[test]
ignore_text()262     fn ignore_text() {
263         let mut manifest = VersionManifest::new();
264 
265         manifest.ignore_text = true;
266         assert!(manifest.ignore_text());
267 
268         manifest.ignore_text = false;
269         assert!(!manifest.ignore_text());
270     }
271 
272     #[test]
set_ignore_text()273     fn set_ignore_text() {
274         let mut manifest = VersionManifest::new();
275 
276         manifest.set_ignore_text(true);
277         assert!(manifest.ignore_text);
278 
279         manifest.set_ignore_text(false);
280         assert!(!manifest.ignore_text);
281     }
282 }
283