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 | /// |
11 | /// # Examples |
12 | /// |
13 | /// ```rust |
14 | /// use version_compare::{Manifest, Version}; |
15 | /// |
16 | /// // Create manifest with max depth of 2 |
17 | /// let mut manifest = Manifest::default(); |
18 | /// manifest.max_depth = Some(2); |
19 | /// |
20 | /// // Version strings equal with manifest because we compare up-to 2 parts deep |
21 | /// let a = Version::from_manifest("1.0.1" , &manifest).unwrap(); |
22 | /// let b = Version::from_manifest("1.0.2" , &manifest).unwrap(); |
23 | /// assert!(a == b); |
24 | /// ``` |
25 | |
26 | #[derive (Debug, Clone, Copy, PartialEq, Eq, Default)] |
27 | #[non_exhaustive ] |
28 | pub struct Manifest { |
29 | /// The maximum depth of a version number. |
30 | /// |
31 | /// This specifies the maximum number of parts. There is no limit if `None` is set. |
32 | pub max_depth: Option<usize>, |
33 | |
34 | /// Whether to ignore text parts in version strings. |
35 | pub ignore_text: bool, |
36 | |
37 | /// Use GNU sort based ordering. |
38 | /// |
39 | /// Enabling this modifies the ordering of numbers with a leading zero to mimick GNUs sort. |
40 | /// |
41 | /// Issue: https://github.com/timvisee/version-compare/issues/27 |
42 | pub gnu_ordering: bool, |
43 | } |
44 | |
45 | /// Version manifest implementation. |
46 | impl Manifest { |
47 | /// Check whether there's a maximum configured depth. |
48 | /// |
49 | /// # Examples |
50 | /// |
51 | /// ``` |
52 | /// use version_compare::Manifest; |
53 | /// |
54 | /// let mut manifest = Manifest::default(); |
55 | /// |
56 | /// assert!(!manifest.has_max_depth()); |
57 | /// |
58 | /// manifest.max_depth = Some(3); |
59 | /// assert!(manifest.has_max_depth()); |
60 | /// ``` |
61 | pub fn has_max_depth(&self) -> bool { |
62 | self.max_depth.is_some() && self.max_depth.unwrap() > 0 |
63 | } |
64 | } |
65 | |
66 | #[cfg_attr (tarpaulin, skip)] |
67 | #[cfg (test)] |
68 | mod tests { |
69 | use super::Manifest; |
70 | |
71 | #[test ] |
72 | #[allow (clippy::field_reassign_with_default)] |
73 | fn has_max_depth() { |
74 | let mut manifest = Manifest::default(); |
75 | |
76 | manifest.max_depth = Some(1); |
77 | assert!(manifest.has_max_depth()); |
78 | |
79 | manifest.max_depth = Some(3); |
80 | assert!(manifest.has_max_depth()); |
81 | |
82 | manifest.max_depth = None; |
83 | assert!(!manifest.has_max_depth()); |
84 | } |
85 | } |
86 | |