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
mod cmp;
pub mod debian;
pub mod python;
pub mod semver;
mod utils;
use std::cmp::{Ordering, PartialOrd};
use std::convert::TryFrom;
use std::fmt;
use std::ops::Deref;
use cmp::{CHAR_ORDER, compare_versions};
use utils::NumChecker;
#[derive(Clone, Debug, Hash)]
pub struct Version(String);
impl Deref for Version {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl PartialEq<Version> for Version {
fn eq(&self, other: &Version) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Eq for Version {}
impl PartialOrd<Version> for Version {
fn partial_cmp(&self, other: &Version) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Version {
fn cmp(&self, other: &Version) -> Ordering {
compare_versions(&self.0, &other.0)
}
}
impl Version {
pub fn epoch() -> u32 {
unimplemented!()
}
}
pub enum InvalidVersion {
InvalidCharacter,
LeadingZero,
EmptyField,
#[doc(hidden)]
__Nonexhaustive,
}
impl TryFrom<String> for Version {
type Error = InvalidVersion;
fn try_from(string: String) -> Result<Version, InvalidVersion> {
for c in string.bytes() {
if CHAR_ORDER[usize::from(c)] == 255 {
return Err(InvalidVersion::InvalidCharacter);
}
}
Ok(Version(string))
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SimpleVersion(Version);
impl AsRef<Version> for SimpleVersion {
fn as_ref(&self) -> &Version {
&self.0
}
}
impl TryFrom<String> for SimpleVersion {
type Error = InvalidVersion;
fn try_from(string: String) -> Result<SimpleVersion, InvalidVersion> {
let mut num_check = NumChecker::new();
for c in string.bytes() {
if c == b'.' {
if num_check == NumChecker::Start {
return Err(InvalidVersion::EmptyField);
}
} else if c < b'0' || b'9' < c {
return Err(InvalidVersion::InvalidCharacter);
}
if !num_check.check(c) {
return Err(InvalidVersion::LeadingZero);
}
}
if num_check == NumChecker::Start {
return Err(InvalidVersion::EmptyField);
}
Ok(SimpleVersion(Version(string)))
}
}