1 //! This tiny crate checks that the running or installed `rustc` meets some
2 //! version requirements. The version is queried by calling the Rust compiler
3 //! with `--version`. The path to the compiler is determined first via the
4 //! `RUSTC` environment variable. If it is not set, then `rustc` is used. If
5 //! that fails, no determination is made, and calls return `None`.
9 //! Set a `cfg` flag in `build.rs` if the running compiler was determined to be
10 //! at least version `1.13.0`:
13 //! extern crate version_check as rustc;
15 //! if rustc::is_min_version("1.13.0").unwrap_or(false) {
16 //! println!("cargo:rustc-cfg=question_mark_operator");
20 //! See [`is_max_version`] or [`is_exact_version`] to check if the compiler
21 //! is _at most_ or _exactly_ a certain version.
23 //! Check that the running compiler was released on or after `2018-12-18`:
26 //! extern crate version_check as rustc;
28 //! match rustc::is_min_date("2018-12-18") {
29 //! Some(true) => "Yep! It's recent!",
30 //! Some(false) => "No, it's older.",
31 //! None => "Couldn't determine the rustc version."
35 //! See [`is_max_date`] or [`is_exact_date`] to check if the compiler was
36 //! released _prior to_ or _exactly on_ a certain date.
38 //! Check that the running compiler supports feature flags:
41 //! extern crate version_check as rustc;
43 //! match rustc::is_feature_flaggable() {
44 //! Some(true) => "Yes! It's a dev or nightly release!",
45 //! Some(false) => "No, it's stable or beta.",
46 //! None => "Couldn't determine the rustc version."
50 //! Check that the running compiler is on the stable channel:
53 //! extern crate version_check as rustc;
55 //! match rustc::Channel::read() {
56 //! Some(c) if c.is_stable() => format!("Yes! It's stable."),
57 //! Some(c) => format!("No, the channel {} is not stable.", c),
58 //! None => format!("Couldn't determine the rustc version.")
62 //! To interact with the version, release date, and release channel as structs,
63 //! use [`Version`], [`Date`], and [`Channel`], respectively. The [`triple()`]
64 //! function returns all three values efficiently.
68 //! This crate is dead simple with no dependencies. If you need something more
69 //! and don't care about panicking if the version cannot be obtained, or if you
70 //! don't mind adding dependencies, see
71 //! [rustc_version](https://crates.io/crates/rustc_version).
80 use std
::process
::Command
;
82 #[doc(inline)] pub use version::*;
83 #[doc(inline)] pub use channel::*;
84 #[doc(inline)] pub use date::*;
86 /// Parses (version, date) as available from rustc version string.
87 fn version_and_date_from_rustc_version(s
: &str) -> (Option
<String
>, Option
<String
>) {
88 let last_line
= s
.lines().last().unwrap_or(s
);
89 let mut components
= last_line
.trim().split(" ");
90 let version
= components
.nth(1);
91 let date
= components
.filter(|c
| c
.ends_with('
)'
)).next()
92 .map(|s
| s
.trim_right().trim_right_matches(")").trim_left().trim_left_matches('
('
));
93 (version
.map(|s
| s
.to_string()), date
.map(|s
| s
.to_string()))
96 /// Returns (version, date) as available from `rustc --version`.
97 fn get_version_and_date() -> Option
<(Option
<String
>, Option
<String
>)> {
98 env
::var("RUSTC").ok()
99 .and_then(|rustc
| Command
::new(rustc
).arg("--version").output().ok())
100 .or_else(|| Command
::new("rustc").arg("--version").output().ok())
101 .and_then(|output
| String
::from_utf8(output
.stdout
).ok())
102 .map(|s
| version_and_date_from_rustc_version(&s
))
105 /// Reads the triple of [`Version`], [`Channel`], and [`Date`] of the installed
106 /// or running `rustc`.
108 /// If any attribute cannot be determined (see the [top-level
109 /// documentation](crate)), returns `None`.
111 /// To obtain only one of three attributes, use [`Version::read()`],
112 /// [`Channel::read()`], or [`Date::read()`].
113 pub fn triple() -> Option
<(Version
, Channel
, Date
)> {
114 let (version_str
, date_str
) = match get_version_and_date() {
115 Some((Some(version
), Some(date
))) => (version
, date
),
119 // Can't use `?` or `try!` for `Option` in 1.0.0.
120 match Version
::parse(&version_str
) {
121 Some(version
) => match Channel
::parse(&version_str
) {
122 Some(channel
) => match Date
::parse(&date_str
) {
123 Some(date
) => Some((version
, channel
, date
)),
132 /// Checks that the running or installed `rustc` was released **on or after**
135 /// The format of `min_date` must be YYYY-MM-DD. For instance: `2016-12-20` or
138 /// If the date cannot be retrieved or parsed, or if `min_date` could not be
139 /// parsed, returns `None`. Otherwise returns `true` if the installed `rustc`
140 /// was release on or after `min_date` and `false` otherwise.
141 pub fn is_min_date(min_date
: &str) -> Option
<bool
> {
142 match (Date
::read(), Date
::parse(min_date
)) {
143 (Some(rustc_date
), Some(min_date
)) => Some(rustc_date
>= min_date
),
148 /// Checks that the running or installed `rustc` was released **on or before**
151 /// The format of `max_date` must be YYYY-MM-DD. For instance: `2016-12-20` or
154 /// If the date cannot be retrieved or parsed, or if `max_date` could not be
155 /// parsed, returns `None`. Otherwise returns `true` if the installed `rustc`
156 /// was release on or before `max_date` and `false` otherwise.
157 pub fn is_max_date(max_date
: &str) -> Option
<bool
> {
158 match (Date
::read(), Date
::parse(max_date
)) {
159 (Some(rustc_date
), Some(max_date
)) => Some(rustc_date
<= max_date
),
164 /// Checks that the running or installed `rustc` was released **exactly** on
167 /// The format of `date` must be YYYY-MM-DD. For instance: `2016-12-20` or
170 /// If the date cannot be retrieved or parsed, or if `date` could not be parsed,
171 /// returns `None`. Otherwise returns `true` if the installed `rustc` was
172 /// release on `date` and `false` otherwise.
173 pub fn is_exact_date(date
: &str) -> Option
<bool
> {
174 match (Date
::read(), Date
::parse(date
)) {
175 (Some(rustc_date
), Some(date
)) => Some(rustc_date
== date
),
180 /// Checks that the running or installed `rustc` is **at least** some minimum
183 /// The format of `min_version` is a semantic version: `1.3.0`, `1.15.0-beta`,
184 /// `1.14.0`, `1.16.0-nightly`, etc.
186 /// If the version cannot be retrieved or parsed, or if `min_version` could not
187 /// be parsed, returns `None`. Otherwise returns `true` if the installed `rustc`
188 /// is at least `min_version` and `false` otherwise.
189 pub fn is_min_version(min_version
: &str) -> Option
<bool
> {
190 match (Version
::read(), Version
::parse(min_version
)) {
191 (Some(rustc_ver
), Some(min_ver
)) => Some(rustc_ver
>= min_ver
),
196 /// Checks that the running or installed `rustc` is **at most** some maximum
199 /// The format of `max_version` is a semantic version: `1.3.0`, `1.15.0-beta`,
200 /// `1.14.0`, `1.16.0-nightly`, etc.
202 /// If the version cannot be retrieved or parsed, or if `max_version` could not
203 /// be parsed, returns `None`. Otherwise returns `true` if the installed `rustc`
204 /// is at most `max_version` and `false` otherwise.
205 pub fn is_max_version(max_version
: &str) -> Option
<bool
> {
206 match (Version
::read(), Version
::parse(max_version
)) {
207 (Some(rustc_ver
), Some(max_ver
)) => Some(rustc_ver
<= max_ver
),
212 /// Checks that the running or installed `rustc` is **exactly** some version.
214 /// The format of `version` is a semantic version: `1.3.0`, `1.15.0-beta`,
215 /// `1.14.0`, `1.16.0-nightly`, etc.
217 /// If the version cannot be retrieved or parsed, or if `version` could not be
218 /// parsed, returns `None`. Otherwise returns `true` if the installed `rustc` is
219 /// exactly `version` and `false` otherwise.
220 pub fn is_exact_version(version
: &str) -> Option
<bool
> {
221 match (Version
::read(), Version
::parse(version
)) {
222 (Some(rustc_ver
), Some(version
)) => Some(rustc_ver
== version
),
227 /// Checks whether the running or installed `rustc` supports feature flags.
229 /// In other words, if the channel is either "nightly" or "dev".
231 /// If the version could not be determined, returns `None`. Otherwise returns
232 /// `true` if the running version supports feature flags and `false` otherwise.
233 pub fn is_feature_flaggable() -> Option
<bool
> {
234 Channel
::read().map(|c
| c
.supports_features())
239 use super::version_and_date_from_rustc_version
;
241 macro_rules
! check_parse
{
242 ($s
:expr
=> $v
:expr
, $d
:expr
) => (
243 if let (Some(v
), d
) = version_and_date_from_rustc_version($s
) {
244 let e_d
: Option
<&str> = $d
.into();
245 assert_eq
!((v
, d
), ($v
.into(), e_d
.map(|s
| s
.into())));
247 panic
!("{:?} didn't parse for version testing.", $s
);
253 fn test_version_parse() {
254 check_parse
!("rustc 1.18.0" => "1.18.0", None
);
255 check_parse
!("rustc 1.8.0" => "1.8.0", None
);
256 check_parse
!("rustc 1.20.0-nightly" => "1.20.0-nightly", None
);
257 check_parse
!("rustc 1.20" => "1.20", None
);
258 check_parse
!("rustc 1.3" => "1.3", None
);
259 check_parse
!("rustc 1" => "1", None
);
260 check_parse
!("rustc 1.5.1-beta" => "1.5.1-beta", None
);
262 // Because of 1.0.0, we can't use Option<T>: From<T>.
263 check_parse
!("rustc 1.20.0 (2017-07-09)"
264 => "1.20.0", Some("2017-07-09"));
266 check_parse
!("rustc 1.20.0-dev (2017-07-09)"
267 => "1.20.0-dev", Some("2017-07-09"));
269 check_parse
!("rustc 1.20.0-nightly (d84693b93 2017-07-09)"
270 => "1.20.0-nightly", Some("2017-07-09"));
272 check_parse
!("rustc 1.20.0 (d84693b93 2017-07-09)"
273 => "1.20.0", Some("2017-07-09"));
275 check_parse
!("warning: invalid logging spec 'warning', ignoring it
276 rustc 1.30.0-nightly (3bc2ca7e4 2018-09-20)"
277 => "1.30.0-nightly", Some("2018-09-20"));
279 check_parse
!("warning: invalid logging spec 'warning', ignoring it\n
280 rustc 1.30.0-nightly (3bc2ca7e4 2018-09-20)"
281 => "1.30.0-nightly", Some("2018-09-20"));
283 check_parse
!("warning: invalid logging spec 'warning', ignoring it
284 warning: something else went wrong
285 rustc 1.30.0-nightly (3bc2ca7e4 2018-09-20)"
286 => "1.30.0-nightly", Some("2018-09-20"));