Skip to content

Commit 98871bd

Browse files
committed
New version: 0.9.
1 parent 96a6d6c commit 98871bd

File tree

7 files changed

+792
-187
lines changed

7 files changed

+792
-187
lines changed

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: rust
2+
rust:
3+
- 1.0.0
4+
- stable
5+
- beta
6+
- nightly

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "version_check"
3-
version = "0.1.5"
3+
version = "0.9.0"
44
authors = ["Sergio Benitez <[email protected]>"]
55
description = "Tiny crate to check the version of the installed/running rustc."
66
documentation = "https://docs.rs/version_check/"

README.md

+30-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# version\_check
22

3+
[![Build Status](https://travis-ci.com/SergioBenitez/version_check.svg?branch=master)](https://travis-ci.com/SergioBenitez/version_check)
4+
[![Current Crates.io Version](https://img.shields.io/crates/v/version_check.svg)](https://crates.io/crates/version_check)
5+
[![rustdocs on docs.rs](https://docs.rs/version_check/badge.svg)](https://docs.rs/version_check)
6+
37
This tiny crate checks that the running or installed `rustc` meets some version
48
requirements. The version is queried by calling the Rust compiler with
59
`--version`. The path to the compiler is determined first via the `RUSTC`
@@ -12,54 +16,57 @@ Add to your `Cargo.toml` file, typically as a build dependency:
1216

1317
```toml
1418
[build-dependencies]
15-
version_check = "0.1"
19+
version_check = "0.9"
1620
```
1721

1822
`version_check` is compatible and compiles with Rust 1.0.0 and beyond.
1923

2024
## Examples
2125

22-
Check that the running compiler is a nightly release:
26+
Set a `cfg` flag in `build.rs` if the running compiler was determined to be
27+
at least version `1.13.0`:
2328

2429
```rust
25-
extern crate version_check;
30+
extern crate version_check as rustc;
2631

27-
match version_check::is_nightly() {
28-
Some(true) => "running a nightly",
29-
Some(false) => "not nightly",
30-
None => "couldn't figure it out"
31-
};
32+
if rustc::is_min_version("1.13.0").unwrap_or(false) {
33+
println!("cargo:rustc-cfg=question_mark_operator");
34+
}
3235
```
3336

34-
Check that the running compiler is at least version `1.13.0`:
37+
Check that the running compiler was released on or after `2018-12-18`:
3538

3639
```rust
37-
extern crate version_check;
40+
extern crate version_check as rustc;
3841

39-
match version_check::is_min_version("1.13.0") {
40-
Some((true, version)) => format!("Yes! It's: {}", version),
41-
Some((false, version)) => format!("No! {} is too old!", version),
42-
None => "couldn't figure it out".into()
42+
match rustc::is_min_date("2018-12-18") {
43+
Some(true) => "Yep! It's recent!",
44+
Some(false) => "No, it's older.",
45+
None => "Couldn't determine the rustc version."
4346
};
4447
```
4548

46-
Check that the running compiler was released on or after `2016-12-18`:
49+
Check that the running compiler supports feature flags:
4750

4851
```rust
49-
extern crate version_check;
52+
extern crate version_check as rustc;
5053

51-
match version_check::is_min_date("2016-12-18") {
52-
Some((true, date)) => format!("Yes! It's: {}", date),
53-
Some((false, date)) => format!("No! {} is too long ago!", date),
54-
None => "couldn't figure it out".into()
54+
match rustc::is_feature_flaggable() {
55+
Some(true) => "Yes! It's a dev or nightly release!",
56+
Some(false) => "No, it's stable or beta.",
57+
None => "Couldn't determine the rustc version."
5558
};
5659
```
5760

61+
See the [rustdocs](https://docs.rs/version_check) for more examples and complete
62+
documentation.
63+
5864
## Alternatives
5965

60-
This crate is dead simple with no dependencies. If you need something more and
61-
don't care about panicking if the version cannot be obtained or adding
62-
dependencies, see [rustc_version](https://crates.io/crates/rustc_version).
66+
This crate is dead simple with no dependencies. If you need something more
67+
and don't care about panicking if the version cannot be obtained, or if you
68+
don't mind adding dependencies, see
69+
[rustc_version](https://crates.io/crates/rustc_version).
6370

6471
## License
6572

src/channel.rs

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
use std::fmt;
2+
3+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
4+
enum Kind {
5+
Dev,
6+
Nightly,
7+
Beta,
8+
Stable,
9+
}
10+
11+
/// Release channel: "dev", "nightly", "beta", or "stable".
12+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
13+
pub struct Channel(Kind);
14+
15+
impl Channel {
16+
/// Reads the release channel of the running compiler. If it cannot be
17+
/// determined (see the [top-level documentation](crate)), returns `None`.
18+
///
19+
/// # Example
20+
///
21+
/// ```rust
22+
/// use version_check::Channel;
23+
///
24+
/// match Channel::read() {
25+
/// Some(c) => format!("The channel is: {}", c),
26+
/// None => format!("Failed to read the release channel.")
27+
/// };
28+
/// ```
29+
pub fn read() -> Option<Channel> {
30+
::get_version_and_date()
31+
.and_then(|(version, _)| version)
32+
.and_then(|version| Channel::parse(&version))
33+
}
34+
35+
/// Parse a Rust release channel from a Rust release version string (of the
36+
/// form `major[.minor[.patch[-channel]]]`). Returns `None` if `version` is
37+
/// not a valid Rust version string.
38+
///
39+
/// # Example
40+
///
41+
/// ```rust
42+
/// use version_check::Channel;
43+
///
44+
/// let dev = Channel::parse("1.3.0-dev").unwrap();
45+
/// assert!(dev.is_dev());
46+
///
47+
/// let nightly = Channel::parse("1.42.2-nightly").unwrap();
48+
/// assert!(nightly.is_nightly());
49+
///
50+
/// let beta = Channel::parse("1.32.0-beta").unwrap();
51+
/// assert!(beta.is_beta());
52+
///
53+
/// let stable = Channel::parse("1.4.0").unwrap();
54+
/// assert!(stable.is_stable());
55+
/// ```
56+
pub fn parse(version: &str) -> Option<Channel> {
57+
if version.contains("-dev") {
58+
Some(Channel(Kind::Dev))
59+
} else if version.contains("-nightly") {
60+
Some(Channel(Kind::Nightly))
61+
} else if version.contains("-beta") {
62+
Some(Channel(Kind::Beta))
63+
} else if !version.contains("-") {
64+
Some(Channel(Kind::Stable))
65+
} else {
66+
None
67+
}
68+
}
69+
70+
/// Returns the name of the release channel.
71+
fn as_str(&self) -> &'static str {
72+
match self.0 {
73+
Kind::Dev => "dev",
74+
Kind::Beta => "beta",
75+
Kind::Nightly => "nightly",
76+
Kind::Stable => "stable",
77+
}
78+
}
79+
80+
/// Returns `true` if this channel supports feature flags. In other words,
81+
/// returns `true` if the channel is either `dev` or `nightly`.
82+
///
83+
/// # Example
84+
///
85+
/// ```rust
86+
/// use version_check::Channel;
87+
///
88+
/// let dev = Channel::parse("1.3.0-dev").unwrap();
89+
/// assert!(dev.supports_features());
90+
///
91+
/// let nightly = Channel::parse("1.42.2-nightly").unwrap();
92+
/// assert!(nightly.supports_features());
93+
///
94+
/// let beta = Channel::parse("1.32.0-beta").unwrap();
95+
/// assert!(!beta.supports_features());
96+
///
97+
/// let stable = Channel::parse("1.4.0").unwrap();
98+
/// assert!(!stable.supports_features());
99+
/// ```
100+
pub fn supports_features(&self) -> bool {
101+
match self.0 {
102+
Kind::Dev | Kind::Nightly => true,
103+
Kind::Beta | Kind::Stable => false
104+
}
105+
}
106+
107+
/// Returns `true` if this channel is `dev` and `false` otherwise.
108+
///
109+
/// # Example
110+
///
111+
/// ```rust
112+
/// use version_check::Channel;
113+
///
114+
/// let dev = Channel::parse("1.3.0-dev").unwrap();
115+
/// assert!(dev.is_dev());
116+
///
117+
/// let stable = Channel::parse("1.0.0").unwrap();
118+
/// assert!(!stable.is_dev());
119+
/// ```
120+
pub fn is_dev(&self) -> bool {
121+
match self.0 {
122+
Kind::Dev => true,
123+
_ => false
124+
}
125+
}
126+
127+
/// Returns `true` if this channel is `nightly` and `false` otherwise.
128+
///
129+
/// # Example
130+
///
131+
/// ```rust
132+
/// use version_check::Channel;
133+
///
134+
/// let nightly = Channel::parse("1.3.0-nightly").unwrap();
135+
/// assert!(nightly.is_nightly());
136+
///
137+
/// let stable = Channel::parse("1.0.0").unwrap();
138+
/// assert!(!stable.is_nightly());
139+
/// ```
140+
pub fn is_nightly(&self) -> bool {
141+
match self.0 {
142+
Kind::Nightly => true,
143+
_ => false
144+
}
145+
}
146+
147+
/// Returns `true` if this channel is `beta` and `false` otherwise.
148+
///
149+
/// # Example
150+
///
151+
/// ```rust
152+
/// use version_check::Channel;
153+
///
154+
/// let beta = Channel::parse("1.3.0-beta").unwrap();
155+
/// assert!(beta.is_beta());
156+
///
157+
/// let stable = Channel::parse("1.0.0").unwrap();
158+
/// assert!(!stable.is_beta());
159+
/// ```
160+
pub fn is_beta(&self) -> bool {
161+
match self.0 {
162+
Kind::Beta => true,
163+
_ => false
164+
}
165+
}
166+
167+
/// Returns `true` if this channel is `stable` and `false` otherwise.
168+
///
169+
/// # Example
170+
///
171+
/// ```rust
172+
/// use version_check::Channel;
173+
///
174+
/// let stable = Channel::parse("1.0.0").unwrap();
175+
/// assert!(stable.is_stable());
176+
///
177+
/// let beta = Channel::parse("1.3.0-beta").unwrap();
178+
/// assert!(!beta.is_stable());
179+
/// ```
180+
pub fn is_stable(&self) -> bool {
181+
match self.0 {
182+
Kind::Stable => true,
183+
_ => false
184+
}
185+
}
186+
}
187+
188+
impl fmt::Display for Channel {
189+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
190+
write!(f, "{}", self.as_str())
191+
}
192+
}

0 commit comments

Comments
 (0)