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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use crate::backport::*;
use crate::error::{ErrorKind, Position};
use crate::identifier::Identifier;
use crate::{BuildMetadata, Comparator, Op, Prerelease, Version, VersionReq};
use core::str::FromStr;
pub struct Error {
pub(crate) kind: ErrorKind,
}
impl FromStr for Version {
type Err = Error;
fn from_str(text: &str) -> Result<Self, Self::Err> {
let mut pos = Position::Major;
let (major, text) = numeric_identifier(text, pos)?;
let text = dot(text, pos)?;
pos = Position::Minor;
let (minor, text) = numeric_identifier(text, pos)?;
let text = dot(text, pos)?;
pos = Position::Patch;
let (patch, text) = numeric_identifier(text, pos)?;
if text.is_empty() {
return Ok(Version::new(major, minor, patch));
}
let (pre, text) = if let Some(text) = text.strip_prefix('-') {
pos = Position::Pre;
let (pre, text) = prerelease_identifier(text)?;
if pre.is_empty() {
return Err(Error::new(ErrorKind::EmptySegment(pos)));
}
(pre, text)
} else {
(Prerelease::EMPTY, text)
};
let (build, text) = if let Some(text) = text.strip_prefix('+') {
pos = Position::Build;
let (build, text) = build_identifier(text)?;
if build.is_empty() {
return Err(Error::new(ErrorKind::EmptySegment(pos)));
}
(build, text)
} else {
(BuildMetadata::EMPTY, text)
};
if let Some(unexpected) = text.chars().next() {
return Err(Error::new(ErrorKind::UnexpectedCharAfter(pos, unexpected)));
}
Ok(Version {
major,
minor,
patch,
pre,
build,
})
}
}
impl FromStr for VersionReq {
type Err = Error;
fn from_str(text: &str) -> Result<Self, Self::Err> {
let text = text.trim_start_matches(' ');
if let Some(text) = wildcard(text) {
if text.trim_start_matches(' ').is_empty() {
#[cfg(not(no_const_vec_new))]
return Ok(VersionReq::STAR);
#[cfg(no_const_vec_new)]
return Ok(VersionReq {
comparators: Vec::new(),
});
} else {
return Err(Error::new(ErrorKind::UnexpectedAfterWildcard));
}
}
let depth = 0;
let mut comparators = Vec::new();
let len = version_req(text, &mut comparators, depth)?;
unsafe { comparators.set_len(len) }
Ok(VersionReq { comparators })
}
}
impl FromStr for Comparator {
type Err = Error;
fn from_str(text: &str) -> Result<Self, Self::Err> {
let text = text.trim_start_matches(' ');
let (comparator, pos, rest) = comparator(text)?;
if !rest.is_empty() {
let unexpected = rest.chars().next().unwrap();
return Err(Error::new(ErrorKind::UnexpectedCharAfter(pos, unexpected)));
}
Ok(comparator)
}
}
impl FromStr for Prerelease {
type Err = Error;
fn from_str(text: &str) -> Result<Self, Self::Err> {
let (pre, rest) = prerelease_identifier(text)?;
if !rest.is_empty() {
return Err(Error::new(ErrorKind::IllegalCharacter(Position::Pre)));
}
Ok(pre)
}
}
impl FromStr for BuildMetadata {
type Err = Error;
fn from_str(text: &str) -> Result<Self, Self::Err> {
let (build, rest) = build_identifier(text)?;
if !rest.is_empty() {
return Err(Error::new(ErrorKind::IllegalCharacter(Position::Build)));
}
Ok(build)
}
}
impl Error {
fn new(kind: ErrorKind) -> Self {
Error { kind }
}
}
impl Op {
const DEFAULT: Self = Op::Caret;
}
fn numeric_identifier(input: &str, pos: Position) -> Result<(u64, &str), Error> {
let mut len = 0;
let mut value = 0u64;
while let Some(&digit) = input.as_bytes().get(len) {
if digit < b'0' || digit > b'9' {
break;
}
if value == 0 && len > 0 {
return Err(Error::new(ErrorKind::LeadingZero(pos)));
}
match value
.checked_mul(10)
.and_then(|value| value.checked_add((digit - b'0') as u64))
{
Some(sum) => value = sum,
None => return Err(Error::new(ErrorKind::Overflow(pos))),
}
len += 1;
}
if len > 0 {
Ok((value, &input[len..]))
} else if let Some(unexpected) = input[len..].chars().next() {
Err(Error::new(ErrorKind::UnexpectedChar(pos, unexpected)))
} else {
Err(Error::new(ErrorKind::UnexpectedEnd(pos)))
}
}
fn wildcard(input: &str) -> Option<&str> {
if let Some(rest) = input.strip_prefix('*') {
Some(rest)
} else if let Some(rest) = input.strip_prefix('x') {
Some(rest)
} else if let Some(rest) = input.strip_prefix('X') {
Some(rest)
} else {
None
}
}
fn dot(input: &str, pos: Position) -> Result<&str, Error> {
if let Some(rest) = input.strip_prefix('.') {
Ok(rest)
} else if let Some(unexpected) = input.chars().next() {
Err(Error::new(ErrorKind::UnexpectedCharAfter(pos, unexpected)))
} else {
Err(Error::new(ErrorKind::UnexpectedEnd(pos)))
}
}
fn prerelease_identifier(input: &str) -> Result<(Prerelease, &str), Error> {
let (string, rest) = identifier(input, Position::Pre)?;
let identifier = unsafe { Identifier::new_unchecked(string) };
Ok((Prerelease { identifier }, rest))
}
fn build_identifier(input: &str) -> Result<(BuildMetadata, &str), Error> {
let (string, rest) = identifier(input, Position::Build)?;
let identifier = unsafe { Identifier::new_unchecked(string) };
Ok((BuildMetadata { identifier }, rest))
}
fn identifier(input: &str, pos: Position) -> Result<(&str, &str), Error> {
let mut accumulated_len = 0;
let mut segment_len = 0;
let mut segment_has_nondigit = false;
loop {
match input.as_bytes().get(accumulated_len + segment_len) {
Some(b'A'..=b'Z') | Some(b'a'..=b'z') | Some(b'-') => {
segment_len += 1;
segment_has_nondigit = true;
}
Some(b'0'..=b'9') => {
segment_len += 1;
}
boundary => {
if segment_len == 0 {
if accumulated_len == 0 && boundary != Some(&b'.') {
return Ok(("", input));
} else {
return Err(Error::new(ErrorKind::EmptySegment(pos)));
}
}
if pos == Position::Pre
&& segment_len > 1
&& !segment_has_nondigit
&& input[accumulated_len..].starts_with('0')
{
return Err(Error::new(ErrorKind::LeadingZero(pos)));
}
accumulated_len += segment_len;
if boundary == Some(&b'.') {
accumulated_len += 1;
segment_len = 0;
segment_has_nondigit = false;
} else {
return Ok(input.split_at(accumulated_len));
}
}
}
}
}
fn op(input: &str) -> (Op, &str) {
let bytes = input.as_bytes();
if bytes.get(0) == Some(&b'=') {
(Op::Exact, &input[1..])
} else if bytes.get(0) == Some(&b'>') {
if bytes.get(1) == Some(&b'=') {
(Op::GreaterEq, &input[2..])
} else {
(Op::Greater, &input[1..])
}
} else if bytes.get(0) == Some(&b'<') {
if bytes.get(1) == Some(&b'=') {
(Op::LessEq, &input[2..])
} else {
(Op::Less, &input[1..])
}
} else if bytes.get(0) == Some(&b'~') {
(Op::Tilde, &input[1..])
} else if bytes.get(0) == Some(&b'^') {
(Op::Caret, &input[1..])
} else {
(Op::DEFAULT, input)
}
}
fn comparator(input: &str) -> Result<(Comparator, Position, &str), Error> {
let (mut op, text) = op(input);
let default_op = input.len() == text.len();
let text = text.trim_start_matches(' ');
let mut pos = Position::Major;
let (major, text) = numeric_identifier(text, pos)?;
let mut has_wildcard = false;
let (minor, text) = if let Some(text) = text.strip_prefix('.') {
pos = Position::Minor;
if let Some(text) = wildcard(text) {
has_wildcard = true;
if default_op {
op = Op::Wildcard;
}
(None, text)
} else {
let (minor, text) = numeric_identifier(text, pos)?;
(Some(minor), text)
}
} else {
(None, text)
};
let (patch, text) = if let Some(text) = text.strip_prefix('.') {
pos = Position::Patch;
if let Some(text) = wildcard(text) {
if default_op {
op = Op::Wildcard;
}
(None, text)
} else if has_wildcard {
return Err(Error::new(ErrorKind::UnexpectedAfterWildcard));
} else {
let (patch, text) = numeric_identifier(text, pos)?;
(Some(patch), text)
}
} else {
(None, text)
};
let (pre, text) = if patch.is_some() && text.starts_with('-') {
pos = Position::Pre;
let text = &text[1..];
let (pre, text) = prerelease_identifier(text)?;
if pre.is_empty() {
return Err(Error::new(ErrorKind::EmptySegment(pos)));
}
(pre, text)
} else {
(Prerelease::EMPTY, text)
};
let text = if patch.is_some() && text.starts_with('+') {
pos = Position::Build;
let text = &text[1..];
let (build, text) = build_identifier(text)?;
if build.is_empty() {
return Err(Error::new(ErrorKind::EmptySegment(pos)));
}
text
} else {
text
};
let text = text.trim_start_matches(' ');
let comparator = Comparator {
op,
major,
minor,
patch,
pre,
};
Ok((comparator, pos, text))
}
fn version_req(input: &str, out: &mut Vec<Comparator>, depth: usize) -> Result<usize, Error> {
let (comparator, pos, text) = comparator(input)?;
if text.is_empty() {
out.reserve_exact(depth + 1);
unsafe { out.as_mut_ptr().add(depth).write(comparator) }
return Ok(depth + 1);
}
let text = if let Some(text) = text.strip_prefix(',') {
text.trim_start_matches(' ')
} else {
let unexpected = text.chars().next().unwrap();
return Err(Error::new(ErrorKind::ExpectedCommaFound(pos, unexpected)));
};
const MAX_COMPARATORS: usize = 32;
if depth + 1 == MAX_COMPARATORS {
return Err(Error::new(ErrorKind::ExcessiveComparators));
}
let len = version_req(text, out, depth + 1)?;
unsafe { out.as_mut_ptr().add(depth).write(comparator) }
Ok(len)
}