-
Notifications
You must be signed in to change notification settings - Fork 10
/
lib.rs
230 lines (202 loc) · 6.88 KB
/
lib.rs
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
/*
* To the extent possible under law, the author has dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide. This software is distributed without any warranty.
*
* See <http://creativecommons.org/publicdomain/zero/1.0/>
*/
use std::cmp::Ordering::{self, Equal, Greater, Less};
use std::collections::VecDeque;
#[derive(Debug)]
enum SortingType {
Numerical(i64, String),
Lexical(String),
SemverPrerelease(String),
}
impl SortingType {
fn into_string(self) -> String {
match self {
Self::Numerical(_, a) | Self::Lexical(a) | Self::SemverPrerelease(a) => a,
}
}
}
fn split_once_rest<'a>(s: &'a str, numeric: bool) -> Option<(&str, &str)> {
let loc = s.find(if numeric {
|c: char| c.is_ascii_digit()
} else {
|c: char| !c.is_ascii_digit()
});
if let Some(index) = loc {
Some(s.split_at(index))
} else {
Some((s, ""))
}
}
fn is_semver_prerelease(s: &str) -> bool {
s.len() > 1 && s.starts_with('-')
}
fn decompose(str_in: &str) -> VecDeque<SortingType> {
if str_in.is_empty() {
return VecDeque::new();
}
let mut last_numeric = str_in.starts_with(|c: char| c.is_ascii_digit());
let mut s = str_in.to_owned();
let mut out: VecDeque<SortingType> = VecDeque::new();
if let Some((left, _)) = s.split_once('+') {
s = left.to_owned();
};
while !s.is_empty() {
if last_numeric {
if let Some((left, right)) = split_once_rest(&s, false) {
out.push_back(SortingType::Numerical(
left.parse::<i64>().unwrap(),
left.to_owned(),
));
s = right.to_owned();
last_numeric = false;
}
} else if let Some((left, right)) = split_once_rest(&s, true) {
out.push_back(if is_semver_prerelease(left) {
SortingType::SemverPrerelease(left.to_string())
} else {
SortingType::Lexical(left.to_string())
});
s = right.to_owned();
last_numeric = true;
}
}
out
}
#[derive(Debug)]
struct VersionComparisonIterator {
left: VecDeque<SortingType>,
right: VecDeque<SortingType>,
}
impl Iterator for VersionComparisonIterator {
type Item = (Option<SortingType>, Option<SortingType>);
fn next(&mut self) -> Option<Self::Item> {
let item = (self.left.pop_front(), self.right.pop_front());
if let (None, None) = item {
None
} else {
Some(item)
}
}
}
pub fn compare(left: &str, right: &str) -> Ordering {
let iter = VersionComparisonIterator {
left: decompose(left),
right: decompose(right),
};
for next in iter {
use SortingType::*;
let current = match next {
(Some(l), None) => {
if let SemverPrerelease(_) = l {
Less
} else {
Greater
}
}
(None, Some(r)) => {
if let SemverPrerelease(_) = r {
Greater
} else {
Less
}
}
(Some(l), Some(r)) => match (l, r) {
(Numerical(l, _), Numerical(r, _)) => l.cmp(&r),
(l, r) => l.into_string().cmp(&r.into_string()),
},
(None, None) => unreachable!(),
};
if current != Equal {
return current;
}
}
Equal
}
#[derive(Debug, Copy, Clone)]
pub struct FlexVer<'a>(pub &'a str);
impl PartialEq for FlexVer<'_> {
fn eq(&self, other: &Self) -> bool {
compare(self.0, other.0) == Equal
}
}
impl Eq for FlexVer<'_> {}
impl PartialOrd for FlexVer<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(compare(self.0, other.0))
}
}
impl Ord for FlexVer<'_> {
fn cmp(&self, other: &Self) -> Ordering {
compare(self.0, other.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test(left: &str, right: &str, result: Ordering) {
assert_eq!(compare(&left, &right), result);
assert_eq!(
compare(&right, &left),
match result {
Less => Greater,
Greater => Less,
Equal => Equal,
}
);
}
#[test]
fn test_compare() {
test("b1.7.3", "a1.2.6", Greater);
test("b1.2.6", "a1.7.3", Greater);
test("a1.1.2", "a1.1.2_01", Less);
test("1.16.5-0.00.5", "1.14.2-1.3.7", Greater);
test("1.0.0", "1.0.0-2", Less);
test("1.0.0", "1.0.0_01", Less);
test("1.0.1", "1.0.0_01", Greater);
test("1.0.0_01", "1.0.1", Less);
test("0.17.1-beta.1", "0.17.1", Less);
test("0.17.1-beta.1", "0.17.1-beta.2", Less);
test("1.4.5_01", "1.4.5_01+fabric-1.17", Equal);
test("1.4.5_01", "1.4.5_01+fabric-1.17+ohno", Equal);
test("14w16a", "18w40b", Less);
test("18w40a", "18w40b", Less);
test("1.4.5_01+fabric-1.17", "18w40b", Less);
test("13w02a", "c0.3.0_01", Less);
test("0.6.0-1.18.x", "0.9.beta-1.18.x", Less);
}
#[test]
fn test_ord() {
assert!(FlexVer("b1.7.3") > FlexVer("a1.2.6"));
assert!(FlexVer("b1.2.6") > FlexVer("a1.7.3"));
assert!(FlexVer("a1.1.2") < FlexVer("a1.1.2_01"));
assert!(FlexVer("1.16.5-0.00.5") > FlexVer("1.14.2-1.3.7"));
assert!(FlexVer("1.0.0") < FlexVer("1.0.0-2"));
assert!(FlexVer("1.0.0") < FlexVer("1.0.0_01"));
assert!(FlexVer("1.0.1") > FlexVer("1.0.0_01"));
assert!(FlexVer("1.0.0_01") < FlexVer("1.0.1"));
assert!(FlexVer("0.17.1-beta.1") < FlexVer("0.17.1"));
assert!(FlexVer("0.17.1-beta.1") < FlexVer("0.17.1-beta.2"));
assert!(FlexVer("1.4.5_01") == FlexVer("1.4.5_01+fabric-1.17"));
assert!(FlexVer("1.4.5_01") == FlexVer("1.4.5_01+fabric-1.17+ohno"));
assert!(FlexVer("14w16a") < FlexVer("18w40b"));
assert!(FlexVer("18w40a") < FlexVer("18w40b"));
assert!(FlexVer("1.4.5_01+fabric-1.17") < FlexVer("18w40b"));
assert!(FlexVer("13w02a") < FlexVer("c0.3.0_01"));
assert!(FlexVer("0.6.0-1.18.x") < FlexVer("0.9.beta-1.18.x"));
assert_eq!(FlexVer("b1.7.3"), FlexVer("b1.7.3").max(FlexVer("a1.2.6")));
assert_eq!(FlexVer("b1.2.6"), FlexVer("b1.2.6").max(FlexVer("a1.7.3")));
assert_eq!(FlexVer("a1.2.6"), FlexVer("b1.7.3").min(FlexVer("a1.2.6")));
assert_eq!(FlexVer("a1.7.3"), FlexVer("b1.2.6").min(FlexVer("a1.7.3")));
assert_eq!(FlexVer("1.0.0"), FlexVer("1.0.0").max(FlexVer("1.0.0")));
assert_eq!(FlexVer("1.0.0"), FlexVer("1.0.0").min(FlexVer("1.0.0")));
assert_eq!(
FlexVer("1.1.0"),
FlexVer("1.1.0").clamp(FlexVer("1.0.0"), FlexVer("1.2.0"))
);
}
}