nautilus_core/string/semver.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Semantic version parsing and comparison.
17
18use std::fmt::Display;
19
20/// Parsed semantic version with major, minor, and patch components.
21///
22/// Supports parsing `"X.Y.Z"` strings and lexicographic comparison
23/// (major, then minor, then patch).
24#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
25pub struct SemVer {
26 /// Major version number.
27 pub major: u64,
28 /// Minor version number.
29 pub minor: u64,
30 /// Patch version number.
31 pub patch: u64,
32}
33
34impl SemVer {
35 /// Parses a `"major.minor.patch"` string into a [`SemVer`].
36 ///
37 /// Missing components default to zero; components beyond patch are ignored.
38 ///
39 /// # Errors
40 ///
41 /// Returns an error if any of the first three components of `s` fails to parse
42 /// as a [`u64`].
43 pub fn parse(s: &str) -> anyhow::Result<Self> {
44 let mut parts = s.split('.').map(str::parse::<u64>);
45 let major = parts.next().unwrap_or(Ok(0))?;
46 let minor = parts.next().unwrap_or(Ok(0))?;
47 let patch = parts.next().unwrap_or(Ok(0))?;
48 Ok(Self {
49 major,
50 minor,
51 patch,
52 })
53 }
54}
55
56impl Display for SemVer {
57 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use rstest::rstest;
65
66 use super::*;
67
68 #[rstest]
69 #[case("6.2.0", 6, 2, 0)]
70 #[case("7.0.15", 7, 0, 15)]
71 #[case("0.0.1", 0, 0, 1)]
72 #[case("1", 1, 0, 0)]
73 #[case("2.5", 2, 5, 0)]
74 fn test_semver_parse(
75 #[case] input: &str,
76 #[case] major: u64,
77 #[case] minor: u64,
78 #[case] patch: u64,
79 ) {
80 let v = SemVer::parse(input).unwrap();
81 assert_eq!(v.major, major);
82 assert_eq!(v.minor, minor);
83 assert_eq!(v.patch, patch);
84 }
85
86 #[rstest]
87 fn test_semver_display() {
88 let v = SemVer::parse("7.2.4").unwrap();
89 assert_eq!(v.to_string(), "7.2.4");
90 }
91
92 #[rstest]
93 fn test_semver_ordering() {
94 let v620 = SemVer::parse("6.2.0").unwrap();
95 let v700 = SemVer::parse("7.0.0").unwrap();
96 let v621 = SemVer::parse("6.2.1").unwrap();
97 let v630 = SemVer::parse("6.3.0").unwrap();
98
99 assert!(v700 > v620);
100 assert!(v621 > v620);
101 assert!(v630 > v621);
102 assert!(v700 >= v620);
103 assert!(v620 >= v620);
104 }
105
106 #[rstest]
107 fn test_semver_parse_invalid() {
108 assert!(SemVer::parse("abc").is_err());
109 }
110}