Debian Codegraph @

src/version.rs

raw symbols SCIP
1 //! R Version strings
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#;
3
4 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
5 /// Represents an R version string like "1.2.3" or "2.5-1".
6 ///
7 /// R version strings consist of non-negative integers separated by `.` or `-`.
8 /// Both separators are equivalent: `2.5-1` and `2.5.1` represent the same version.
9 /// There is no concept of pre-release versions in R's versioning scheme.
10 pub struct Versionrust-analyzer cargo r-description 0.4.1 version/Version# {
11 /// Version components like [1, 2, 3]
12 pub componentsrust-analyzer cargo r-description 0.4.1 version/Version#components.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32>,
13 }
14
15 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Versionrust-analyzer cargo r-description 0.4.1 version/Version# {
16 fn fmtrust-analyzer cargo r-description 0.4.1 version/impl#[Version][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
17 f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(
18 &self
19 .componentsrust-analyzer cargo r-description 0.4.1 version/Version#components.
20 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
21 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|c| c.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())
22 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>()
23 .joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().("."),
24 )
25 }
26 }
27
28 impl Versionrust-analyzer cargo r-description 0.4.1 version/Version# {
29 /// Create a new version
30 pub fn newrust-analyzer cargo r-description 0.4.1 version/impl#[Version]new().(major: u32, minor: u32, patch: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u32>) -> Selfrust-analyzer cargo r-description 0.4.1 version/impl#[Version] {
31 Selfrust-analyzer cargo r-description 0.4.1 version/impl#[Version] {
32 componentsrust-analyzer cargo r-description 0.4.1 version/Version#components.: if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(patch) = patch {
33 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![major, minor, patch]
34 } else {
35 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![major, minor]
36 },
37 }
38 }
39 }
40
41 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for Versionrust-analyzer cargo r-description 0.4.1 version/Version# {
42 type Errrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]Err# = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#;
43
44 fn from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr], Selfrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#> {
45 // Both '.' and '-' are valid separators in R version strings and are equivalent.
46 // e.g. "2.5-1" == "2.5.1"
47 let components = s
48 .splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(|c| c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'.' || c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'-')
49 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|part| {
50 part.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()
51 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Invalid version component: {s}"))
52 })
53 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>, _>>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
54
55 if components.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().2 {
56 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Invalid version string: {s}"));
57 }
58
59 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr] { componentsrust-analyzer cargo r-description 0.4.1 version/Version#components. })
60 }
61 }
62
63 impl Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# for Versionrust-analyzer cargo r-description 0.4.1 version/Version# {
64 fn cmprust-analyzer cargo r-description 0.4.1 version/impl#[Version][Ord]cmp().(&self, other: &Selfrust-analyzer cargo r-description 0.4.1 version/impl#[Version][Ord]) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering# {
65 for (a, b) in self.componentsrust-analyzer cargo r-description 0.4.1 version/Version#components..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(other.componentsrust-analyzer cargo r-description 0.4.1 version/Version#components..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()) {
66 match a.cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][Ord]cmp().(b) {
67 Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Equalrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Equal# => continue,
68 ordering => return ordering,
69 }
70 }
71 self.componentsrust-analyzer cargo r-description 0.4.1 version/Version#components..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().().cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][Ord]cmp().(&other.componentsrust-analyzer cargo r-description 0.4.1 version/Version#components..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().())
72 }
73 }
74
75 impl PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd# for Versionrust-analyzer cargo r-description 0.4.1 version/Version# {
76 fn partial_cmprust-analyzer cargo r-description 0.4.1 version/impl#[Version][`PartialOrd<Self>`]partial_cmp().(&self, other: &Selfrust-analyzer cargo r-description 0.4.1 version/impl#[Version][`PartialOrd<Self>`]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#> {
77 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(self.cmprust-analyzer cargo r-description 0.4.1 version/impl#[Version][Ord]cmp().(other))
78 }
79 }
80
81 #[cfg(test)]
82 mod testsrust-analyzer cargo r-description 0.4.1 version/tests/ {
83 use superrust-analyzer cargo r-description 0.4.1 version/::Versionrust-analyzer cargo r-description 0.4.1 version/Version#;
84 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
85
86 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
87 fn test_version_from_strrust-analyzer cargo r-description 0.4.1 version/tests/test_version_from_str().() {
88 let version = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.3").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
89 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(version, Versionrust-analyzer cargo r-description 0.4.1 version/Version#::newrust-analyzer cargo r-description 0.4.1 version/impl#[Version]new().(1, 2, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(3)));
90
91 // '-' and '.' are equivalent separators in R
92 let version = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("2.5-1").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
93 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(version.componentsrust-analyzer cargo r-description 0.4.1 version/Version#components., vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2, 5, 1]);
94
95 // Development versions use a 4th numeric component
96 let version = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.3.9000").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
97 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(version.componentsrust-analyzer cargo r-description 0.4.1 version/Version#components., vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1, 2, 3, 9000]);
98 }
99
100 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
101 fn test_version_cmprust-analyzer cargo r-description 0.4.1 version/tests/test_version_cmp().() {
102 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#;
103
104 let v1 = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.3").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
105 let v2 = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.3").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
106 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(v1.cmprust-analyzer cargo r-description 0.4.1 version/impl#[Version][Ord]cmp().(&v2), Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Equalrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Equal#);
107
108 let v1 = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.3").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
109 let v2 = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.4").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
110 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(v1.cmprust-analyzer cargo r-description 0.4.1 version/impl#[Version][Ord]cmp().(&v2), Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less#);
111
112 // '-' and '.' are equivalent: "2.5-1" == "2.5.1"
113 let v1 = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("2.5-1").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
114 let v2 = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("2.5.1").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
115 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(v1.cmprust-analyzer cargo r-description 0.4.1 version/impl#[Version][Ord]cmp().(&v2), Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Equalrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Equal#);
116
117 // Versions can have more than three components
118 let v1 = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.3.9000").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
119 let v2 = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.3").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
120 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(v1.cmprust-analyzer cargo r-description 0.4.1 version/impl#[Version][Ord]cmp().(&v2), Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Greaterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Greater#);
121
122 let v1 = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.3.9000").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
123 let v2 = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.4").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
124 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(v1.cmprust-analyzer cargo r-description 0.4.1 version/impl#[Version][Ord]cmp().(&v2), Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less#);
125 }
126
127 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
128 fn test_version_displayrust-analyzer cargo r-description 0.4.1 version/tests/test_version_display().() {
129 // Display normalizes to '.' separator
130 let version = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.2.3").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
131 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(version.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "1.2.3");
132
133 let version = Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("2.5-1").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
134 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(version.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "2.5.1");
135 }
136
137 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
138 fn test_version_invalidrust-analyzer cargo r-description 0.4.1 version/tests/test_version_invalid().() {
139 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("a").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
140 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1.a.3").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
141 // Single component is not a valid R package version
142 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Versionrust-analyzer cargo r-description 0.4.1 version/Version#::from_strrust-analyzer cargo r-description 0.4.1 version/impl#[Version][FromStr]from_str().("1").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
143 }
144 }