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.
38 ///
39 /// # Errors
40 ///
41 /// Returns an error if any component of `s` fails to parse as a [`u64`].
42 pub fn parse(s: &str) -> anyhow::Result<Self> {
43 let mut parts = s.split('.').map(str::parse::<u64>);
44 let major = parts.next().unwrap_or(Ok(0))?;
45 let minor = parts.next().unwrap_or(Ok(0))?;
46 let patch = parts.next().unwrap_or(Ok(0))?;
47 Ok(Self {
48 major,
49 minor,
50 patch,
51 })
52 }
53}
54
55impl Display for SemVer {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use rstest::rstest;
64
65 use super::*;
66
67 #[rstest]
68 #[case("6.2.0", 6, 2, 0)]
69 #[case("7.0.15", 7, 0, 15)]
70 #[case("0.0.1", 0, 0, 1)]
71 #[case("1", 1, 0, 0)]
72 #[case("2.5", 2, 5, 0)]
73 fn test_semver_parse(
74 #[case] input: &str,
75 #[case] major: u64,
76 #[case] minor: u64,
77 #[case] patch: u64,
78 ) {
79 let v = SemVer::parse(input).unwrap();
80 assert_eq!(v.major, major);
81 assert_eq!(v.minor, minor);
82 assert_eq!(v.patch, patch);
83 }
84
85 #[rstest]
86 fn test_semver_display() {
87 let v = SemVer::parse("7.2.4").unwrap();
88 assert_eq!(v.to_string(), "7.2.4");
89 }
90
91 #[rstest]
92 fn test_semver_ordering() {
93 let v620 = SemVer::parse("6.2.0").unwrap();
94 let v700 = SemVer::parse("7.0.0").unwrap();
95 let v621 = SemVer::parse("6.2.1").unwrap();
96 let v630 = SemVer::parse("6.3.0").unwrap();
97
98 assert!(v700 > v620);
99 assert!(v621 > v620);
100 assert!(v630 > v621);
101 assert!(v700 >= v620);
102 assert!(v620 >= v620);
103 }
104
105 #[rstest]
106 fn test_semver_parse_invalid() {
107 assert!(SemVer::parse("abc").is_err());
108 }
109}