#![allow(deprecated)]
mod version;
mod channel;
mod date;
use std::env;
use std::process::Command;
#[doc(inline)] pub use version::*;
#[doc(inline)] pub use channel::*;
#[doc(inline)] pub use date::*;
fn version_and_date_from_rustc_version(s: &str) -> (Option<String>, Option<String>) {
let last_line = s.lines().last().unwrap_or(s);
let mut components = last_line.trim().split(" ");
let version = components.nth(1);
let date = components.filter(|c| c.ends_with(')')).next()
.map(|s| s.trim_right().trim_right_matches(")").trim_left().trim_left_matches('('));
(version.map(|s| s.to_string()), date.map(|s| s.to_string()))
}
fn version_and_date_from_rustc_verbose_version(s: &str) -> (Option<String>, Option<String>) {
let (mut version, mut date) = (None, None);
for line in s.lines() {
let split = |s: &str| s.splitn(2, ":").nth(1).map(|s| s.trim().to_string());
match line.trim().split(" ").nth(0) {
Some("rustc") => {
let (v, d) = version_and_date_from_rustc_version(line);
version = version.or(v);
date = date.or(d);
},
Some("release:") => version = split(line),
Some("commit-date:") if line.ends_with("unknown") => date = None,
Some("commit-date:") => date = split(line),
_ => continue
}
}
(version, date)
}
fn get_version_and_date() -> Option<(Option<String>, Option<String>)> {
let rustc = env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());
Command::new(rustc).arg("--verbose").arg("--version").output().ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|s| version_and_date_from_rustc_verbose_version(&s))
}
pub fn triple() -> Option<(Version, Channel, Date)> {
let (version_str, date_str) = match get_version_and_date() {
Some((Some(version), Some(date))) => (version, date),
_ => return None
};
match Version::parse(&version_str) {
Some(version) => match Channel::parse(&version_str) {
Some(channel) => match Date::parse(&date_str) {
Some(date) => Some((version, channel, date)),
_ => None,
},
_ => None,
},
_ => None
}
}
pub fn is_min_date(min_date: &str) -> Option<bool> {
match (Date::read(), Date::parse(min_date)) {
(Some(rustc_date), Some(min_date)) => Some(rustc_date >= min_date),
_ => None
}
}
pub fn is_max_date(max_date: &str) -> Option<bool> {
match (Date::read(), Date::parse(max_date)) {
(Some(rustc_date), Some(max_date)) => Some(rustc_date <= max_date),
_ => None
}
}
pub fn is_exact_date(date: &str) -> Option<bool> {
match (Date::read(), Date::parse(date)) {
(Some(rustc_date), Some(date)) => Some(rustc_date == date),
_ => None
}
}
pub fn is_min_version(min_version: &str) -> Option<bool> {
match (Version::read(), Version::parse(min_version)) {
(Some(rustc_ver), Some(min_ver)) => Some(rustc_ver >= min_ver),
_ => None
}
}
pub fn is_max_version(max_version: &str) -> Option<bool> {
match (Version::read(), Version::parse(max_version)) {
(Some(rustc_ver), Some(max_ver)) => Some(rustc_ver <= max_ver),
_ => None
}
}
pub fn is_exact_version(version: &str) -> Option<bool> {
match (Version::read(), Version::parse(version)) {
(Some(rustc_ver), Some(version)) => Some(rustc_ver == version),
_ => None
}
}
pub fn is_feature_flaggable() -> Option<bool> {
Channel::read().map(|c| c.supports_features())
}
#[cfg(test)]
mod tests {
use super::version_and_date_from_rustc_version;
use super::version_and_date_from_rustc_verbose_version;
macro_rules! check_parse {
(@ $f:expr, $s:expr => $v:expr, $d:expr) => ({
if let (Some(v), d) = $f($s) {
let e_d: Option<&str> = $d.into();
assert_eq!((v, d), ($v.into(), e_d.map(|s| s.into())));
} else {
panic!("{:?} didn't parse for version testing.", $s);
}
});
($f:expr, $s:expr => $v:expr, $d:expr) => ({
let warn = "warning: invalid logging spec 'warning', ignoring it";
let warn2 = "warning: sorry, something went wrong :(sad)";
check_parse!(@ $f, $s => $v, $d);
check_parse!(@ $f, &format!("{}\n{}", warn, $s) => $v, $d);
check_parse!(@ $f, &format!("{}\n{}", warn2, $s) => $v, $d);
check_parse!(@ $f, &format!("{}\n{}\n{}", warn, warn2, $s) => $v, $d);
check_parse!(@ $f, &format!("{}\n{}\n{}", warn2, warn, $s) => $v, $d);
})
}
macro_rules! check_terse_parse {
($($s:expr => $v:expr, $d:expr,)+) => {$(
check_parse!(version_and_date_from_rustc_version, $s => $v, $d);
)+}
}
macro_rules! check_verbose_parse {
($($s:expr => $v:expr, $d:expr,)+) => {$(
check_parse!(version_and_date_from_rustc_verbose_version, $s => $v, $d);
)+}
}
#[test]
fn test_version_parse() {
check_terse_parse! {
"rustc 1.18.0" => "1.18.0", None,
"rustc 1.8.0" => "1.8.0", None,
"rustc 1.20.0-nightly" => "1.20.0-nightly", None,
"rustc 1.20" => "1.20", None,
"rustc 1.3" => "1.3", None,
"rustc 1" => "1", None,
"rustc 1.5.1-beta" => "1.5.1-beta", None,
"rustc 1.20.0 (2017-07-09)" => "1.20.0", Some("2017-07-09"),
"rustc 1.20.0-dev (2017-07-09)" => "1.20.0-dev", Some("2017-07-09"),
"rustc 1.20.0-nightly (d84693b93 2017-07-09)" => "1.20.0-nightly", Some("2017-07-09"),
"rustc 1.20.0 (d84693b93 2017-07-09)" => "1.20.0", Some("2017-07-09"),
"rustc 1.30.0-nightly (3bc2ca7e4 2018-09-20)" => "1.30.0-nightly", Some("2018-09-20"),
};
}
#[test]
fn test_verbose_version_parse() {
check_verbose_parse! {
"rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)\n\
binary: rustc\n\
commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e\n\
commit-date: 2015-05-13\n\
build-date: 2015-05-14\n\
host: x86_64-unknown-linux-gnu\n\
release: 1.0.0" => "1.0.0", Some("2015-05-13"),
"rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)\n\
commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e\n\
commit-date: 2015-05-13\n\
build-date: 2015-05-14\n\
host: x86_64-unknown-linux-gnu\n\
release: 1.0.0" => "1.0.0", Some("2015-05-13"),
"rustc 1.50.0 (cb75ad5db 2021-02-10)\n\
binary: rustc\n\
commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\n\
commit-date: 2021-02-10\n\
host: x86_64-unknown-linux-gnu\n\
release: 1.50.0" => "1.50.0", Some("2021-02-10"),
"rustc 1.52.0-nightly (234781afe 2021-03-07)\n\
binary: rustc\n\
commit-hash: 234781afe33d3f339b002f85f948046d8476cfc9\n\
commit-date: 2021-03-07\n\
host: x86_64-unknown-linux-gnu\n\
release: 1.52.0-nightly\n\
LLVM version: 12.0.0" => "1.52.0-nightly", Some("2021-03-07"),
"rustc 1.41.1\n\
binary: rustc\n\
commit-hash: unknown\n\
commit-date: unknown\n\
host: x86_64-unknown-linux-gnu\n\
release: 1.41.1\n\
LLVM version: 7.0" => "1.41.1", None,
"rustc 1.49.0\n\
binary: rustc\n\
commit-hash: unknown\n\
commit-date: unknown\n\
host: x86_64-unknown-linux-gnu\n\
release: 1.49.0" => "1.49.0", None,
"rustc 1.50.0 (Fedora 1.50.0-1.fc33)\n\
binary: rustc\n\
commit-hash: unknown\n\
commit-date: unknown\n\
host: x86_64-unknown-linux-gnu\n\
release: 1.50.0" => "1.50.0", None,
};
}
}