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
#![warn(missing_docs)]
extern crate semver;
use semver::Identifier;
use std::process::Command;
use std::{env, str};
use std::ffi::OsString;
pub use semver::Version;
mod errors;
pub use errors::{Error, Result};
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Channel {
Dev,
Nightly,
Beta,
Stable,
}
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct VersionMeta {
pub semver: Version,
pub commit_hash: Option<String>,
pub commit_date: Option<String>,
pub build_date: Option<String>,
pub channel: Channel,
pub host: String,
pub short_version_string: String,
}
impl VersionMeta {
pub fn for_command(cmd: Command) -> Result<VersionMeta> {
let mut cmd = cmd;
let out = match cmd.arg("-vV").output() {
Err(e) => return Err(Error::CouldNotExecuteCommand(e)),
Ok(out) => out,
};
let out = try!(str::from_utf8(&out.stdout));
version_meta_for(out)
}
}
pub fn version() -> Result<Version> {
Ok(try!(version_meta()).semver)
}
pub fn version_meta() -> Result<VersionMeta> {
let cmd = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));
VersionMeta::for_command(Command::new(cmd))
}
pub fn version_meta_for(verbose_version_string: &str) -> Result<VersionMeta> {
let out: Vec<_> = verbose_version_string.lines().collect();
if !(out.len() >= 6 && out.len() <= 8) {
return Err(Error::UnexpectedVersionFormat);
}
let short_version_string = out[0];
fn expect_prefix<'a>(line: &'a str, prefix: &str) -> Result<&'a str> {
match line.starts_with(prefix) {
true => Ok(&line[prefix.len()..]),
false => Err(Error::UnexpectedVersionFormat),
}
}
let commit_hash = match try!(expect_prefix(out[2], "commit-hash: ")) {
"unknown" => None,
hash => Some(hash.to_owned()),
};
let commit_date = match try!(expect_prefix(out[3], "commit-date: ")) {
"unknown" => None,
hash => Some(hash.to_owned()),
};
let mut idx = 4;
let mut build_date = None;
if out[idx].starts_with("build-date") {
build_date = match try!(expect_prefix(out[idx], "build-date: ")) {
"unknown" => None,
s => Some(s.to_owned()),
};
idx = idx + 1;
}
let host = try!(expect_prefix(out[idx], "host: "));
idx = idx + 1;
let release = try!(expect_prefix(out[idx], "release: "));
let semver: Version = try!(release.parse());
let channel = if semver.pre.is_empty() {
Channel::Stable
} else {
match semver.pre[0] {
Identifier::AlphaNumeric(ref s)
if s == "dev" => Channel::Dev,
Identifier::AlphaNumeric(ref s)
if s == "beta" => Channel::Beta,
Identifier::AlphaNumeric(ref s)
if s == "nightly" => Channel::Nightly,
ref x => return Err(Error::UnknownPreReleaseTag(x.clone())),
}
};
Ok(VersionMeta {
semver: semver,
commit_hash: commit_hash,
commit_date: commit_date,
build_date: build_date,
channel: channel,
host: host.into(),
short_version_string: short_version_string.into(),
})
}
#[test]
fn smoketest() {
let v = version().unwrap();
assert!(v.major >= 1);
let v = version_meta().unwrap();
assert!(v.semver.major >= 1);
assert!(version().unwrap() >= Version::parse("1.0.0").unwrap());
}
#[test]
fn parse_unexpected() {
let res = version_meta_for(
"rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
binary: rustc
commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e
commit-date: 2015-05-13
rust-birthday: 2015-05-14
host: x86_64-unknown-linux-gnu
release: 1.0.0");
assert!(match res {
Err(Error::UnexpectedVersionFormat) => true,
_ => false,
});
}
#[test]
fn parse_1_0_0() {
let version = version_meta_for(
"rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
binary: rustc
commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e
commit-date: 2015-05-13
build-date: 2015-05-14
host: x86_64-unknown-linux-gnu
release: 1.0.0").unwrap();
assert_eq!(version.semver, Version::parse("1.0.0").unwrap());
assert_eq!(version.commit_hash, Some("a59de37e99060162a2674e3ff45409ac73595c0e".into()));
assert_eq!(version.commit_date, Some("2015-05-13".into()));
assert_eq!(version.build_date, Some("2015-05-14".into()));
assert_eq!(version.channel, Channel::Stable);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)");
}
#[test]
fn parse_unknown() {
let version = version_meta_for(
"rustc 1.3.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.3.0").unwrap();
assert_eq!(version.semver, Version::parse("1.3.0").unwrap());
assert_eq!(version.commit_hash, None);
assert_eq!(version.commit_date, None);
assert_eq!(version.channel, Channel::Stable);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.3.0");
}
#[test]
fn parse_nightly() {
let version = version_meta_for(
"rustc 1.5.0-nightly (65d5c0833 2015-09-29)
binary: rustc
commit-hash: 65d5c083377645a115c4ac23a620d3581b9562b6
commit-date: 2015-09-29
host: x86_64-unknown-linux-gnu
release: 1.5.0-nightly").unwrap();
assert_eq!(version.semver, Version::parse("1.5.0-nightly").unwrap());
assert_eq!(version.commit_hash, Some("65d5c083377645a115c4ac23a620d3581b9562b6".into()));
assert_eq!(version.commit_date, Some("2015-09-29".into()));
assert_eq!(version.channel, Channel::Nightly);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.5.0-nightly (65d5c0833 2015-09-29)");
}
#[test]
fn parse_stable() {
let version = version_meta_for(
"rustc 1.3.0 (9a92aaf19 2015-09-15)
binary: rustc
commit-hash: 9a92aaf19a64603b02b4130fe52958cc12488900
commit-date: 2015-09-15
host: x86_64-unknown-linux-gnu
release: 1.3.0").unwrap();
assert_eq!(version.semver, Version::parse("1.3.0").unwrap());
assert_eq!(version.commit_hash, Some("9a92aaf19a64603b02b4130fe52958cc12488900".into()));
assert_eq!(version.commit_date, Some("2015-09-15".into()));
assert_eq!(version.channel, Channel::Stable);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.3.0 (9a92aaf19 2015-09-15)");
}
#[test]
fn parse_1_16_0_nightly() {
let version = version_meta_for(
"rustc 1.16.0-nightly (5d994d8b7 2017-01-05)
binary: rustc
commit-hash: 5d994d8b7e482e87467d4a521911477bd8284ce3
commit-date: 2017-01-05
host: x86_64-unknown-linux-gnu
release: 1.16.0-nightly
LLVM version: 3.9").unwrap();
assert_eq!(version.semver, Version::parse("1.16.0-nightly").unwrap());
assert_eq!(version.commit_hash, Some("5d994d8b7e482e87467d4a521911477bd8284ce3".into()));
assert_eq!(version.commit_date, Some("2017-01-05".into()));
assert_eq!(version.channel, Channel::Nightly);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.16.0-nightly (5d994d8b7 2017-01-05)");
}