1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! Compare versions according to the [UAPI Version Format
//! Specification](https://uapi-group.org/specifications/specs/version_format_specification/).
//!
//! This implementation is written purely in Rust and does not rely on any third party
//! dependencies. Most notably, it doesn't link to `libsystemd`. It is `#![no_std]` and thus can,
//! for example, also be used for UEFI development.
//!
//! # Examples
//!
//! You can compare two versions:
//!
//! ```
//! use std::cmp::Ordering;
//!
//! use uapi_version::Version;
//!
//! let a = Version::from("225.1");
//! let b = Version::from("2");
//!
//! assert_eq!(a.cmp(&b), Ordering::Greater)
//! ```
//!
//! [`Version`] implements [`std::cmp::Ord`] and thus can be used to order a list of versions.
//!
//! ```
//! use uapi_version::Version;
//!
//! let mut versions = [
//!     "5.2",
//!     "abc-5",
//!     "1.0.0~rc1",
//! ].map(Version::from);
//!
//! versions.sort();
//!
//! assert_eq!(versions, [ "abc-5", "1.0.0~rc1", "5.2" ].map(Version::from))
//! ```
//!
//! You can also use [`strverscmp`] to compare two strings directly:
//!
//! ```
//! use std::cmp::Ordering;
//!
//! use uapi_version::strverscmp;
//!
//! assert_eq!(strverscmp("124", "123"), Ordering::Greater)
//! ```
#![no_std]

extern crate alloc;

use alloc::fmt;
use alloc::string::String;
use core::cmp::Ordering;

/// The `Version` type.
///
/// Can be built from any string that is a sequence of zero or more characters.
///
/// # Examples
///
/// ```
/// use std::cmp::Ordering;
///
/// use uapi_version::Version;
///
/// let a = Version::from("1.0.0");
/// let b = Version::from("2.0.0");
///
/// // `a` is smaller (i.e. older) than `b`.
/// assert_eq!(a.cmp(&b), Ordering::Less)
/// ```
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Version(String);

impl Version {
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    #[must_use]
    pub fn into_string(self) -> String {
        self.0
    }
}

impl From<&str> for Version {
    fn from(s: &str) -> Self {
        Self(s.into())
    }
}

impl From<String> for Version {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&String> for Version {
    fn from(s: &String) -> Self {
        Self(s.into())
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl PartialOrd for Version {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Version {
    fn cmp(&self, other: &Self) -> Ordering {
        strverscmp(&self.0, &other.0)
    }
}

/// Compare two version strings.
///
/// # Examples
///
/// ```
/// use std::cmp::Ordering;
///
/// use uapi_version::strverscmp;
///
/// assert_eq!(strverscmp("1.0.0", "2.0.0"), Ordering::Less)
/// ```
#[must_use]
pub fn strverscmp(a: &str, b: &str) -> Ordering {
    let mut left_iter = a.chars().peekable();
    let mut right_iter = b.chars().peekable();

    loop {
        let mut left = left_iter.next();
        let mut right = right_iter.next();

        // Step 1: Skip invalid chars
        while left.is_some() && !left.is_some_and(is_valid_version_char) {
            left = left_iter.next();
        }
        while right.is_some() && !right.is_some_and(is_valid_version_char) {
            right = right_iter.next();
        }

        // Step 2: Handle '~'
        if left.is_some_and(|c| c == '~') || right.is_some_and(|c| c == '~') {
            let ordering = compare_special_char('~', left, right);
            if ordering != Ordering::Equal {
                return ordering;
            }
        }

        // Step 3: Handle empty
        if left.is_none() || right.is_none() {
            return left.cmp(&right);
        }

        // Step 4: Handle '-'
        if left.is_some_and(|c| c == '-') || right.is_some_and(|c| c == '-') {
            let ordering = compare_special_char('-', left, right);
            if ordering != Ordering::Equal {
                return ordering;
            }
        }

        // Step 5: Handle '^'
        if left.is_some_and(|c| c == '^') || right.is_some_and(|c| c == '^') {
            let ordering = compare_special_char('^', left, right);
            if ordering != Ordering::Equal {
                return ordering;
            }
        }

        // Step 6: Handle '.'
        if left.is_some_and(|c| c == '.') || right.is_some_and(|c| c == '.') {
            let ordering = compare_special_char('.', left, right);
            if ordering != Ordering::Equal {
                return ordering;
            }
        }

        // Step 7: Handle numerical prefix
        if left.is_some_and(|c| c.is_ascii_digit()) || right.is_some_and(|c| c.is_ascii_digit()) {
            let mut left_digit_prefix = String::new();
            while left.is_some_and(|c| c.is_ascii_digit()) {
                if let Some(char) = left {
                    if char != '0' {
                        left_digit_prefix.push(char);
                    }
                }
                if !left_iter.peek().is_some_and(char::is_ascii_digit) {
                    break;
                }
                left = left_iter.next();
            }

            let mut right_digit_prefix = String::new();
            while right.is_some_and(|c| c.is_ascii_digit()) {
                if let Some(char) = right {
                    if char != '0' {
                        right_digit_prefix.push(char);
                    }
                }
                if !right_iter.peek().is_some_and(char::is_ascii_digit) {
                    break;
                }
                right = right_iter.next();
            }

            if left_digit_prefix.len() != right_digit_prefix.len() {
                return left_digit_prefix.len().cmp(&right_digit_prefix.len());
            }

            let ordering = left_digit_prefix.cmp(&right_digit_prefix);
            if ordering != Ordering::Equal {
                return ordering;
            }
        // Step 8: Handle alphabetical prefix
        } else {
            let mut left_alpha_prefix = String::new();
            while left.is_some_and(|c| c.is_ascii_alphabetic()) {
                if let Some(char) = left {
                    left_alpha_prefix.push(char);
                }
                if !left_iter.peek().is_some_and(char::is_ascii_alphabetic) {
                    break;
                }
                left = left_iter.next();
            }

            let mut right_alpha_prefix = String::new();
            while right.is_some_and(|c| c.is_ascii_alphabetic()) {
                if let Some(char) = right {
                    right_alpha_prefix.push(char);
                }
                if !right_iter.peek().is_some_and(char::is_ascii_alphabetic) {
                    break;
                }
                right = right_iter.next();
            }

            let ordering = left_alpha_prefix.cmp(&right_alpha_prefix);
            if ordering != Ordering::Equal {
                return ordering;
            }
        }
    }
}

fn compare_special_char(char: char, left: Option<char>, right: Option<char>) -> Ordering {
    let left_bool = !left.is_some_and(|c| c == char);
    let right_bool = !right.is_some_and(|c| c == char);
    left_bool.cmp(&right_bool)
}

fn is_valid_version_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || matches!(c, '~' | '-' | '^' | '.')
}