]>
git.proxmox.com Git - rustc.git/blob - vendor/version_check/src/channel.rs
3 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
11 /// Release channel: "dev", "nightly", "beta", or "stable".
12 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
13 pub struct Channel(Kind
);
16 /// Reads the release channel of the running compiler. If it cannot be
17 /// determined (see the [top-level documentation](crate)), returns `None`.
22 /// use version_check::Channel;
24 /// match Channel::read() {
25 /// Some(c) => format!("The channel is: {}", c),
26 /// None => format!("Failed to read the release channel.")
29 pub fn read() -> Option
<Channel
> {
30 ::get_version_and_date()
31 .and_then(|(version
, _
)| version
)
32 .and_then(|version
| Channel
::parse(&version
))
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.
42 /// use version_check::Channel;
44 /// let dev = Channel::parse("1.3.0-dev").unwrap();
45 /// assert!(dev.is_dev());
47 /// let nightly = Channel::parse("1.42.2-nightly").unwrap();
48 /// assert!(nightly.is_nightly());
50 /// let beta = Channel::parse("1.32.0-beta").unwrap();
51 /// assert!(beta.is_beta());
53 /// let stable = Channel::parse("1.4.0").unwrap();
54 /// assert!(stable.is_stable());
56 pub fn parse(version
: &str) -> Option
<Channel
> {
57 let version
= version
.trim();
58 if version
.contains("-dev") || version
== "dev" {
59 Some(Channel(Kind
::Dev
))
60 } else if version
.contains("-nightly") || version
== "nightly" {
61 Some(Channel(Kind
::Nightly
))
62 } else if version
.contains("-beta") || version
== "beta" {
63 Some(Channel(Kind
::Beta
))
64 } else if !version
.contains("-") {
65 Some(Channel(Kind
::Stable
))
71 /// Returns the name of the release channel.
72 fn as_str(&self) -> &'
static str {
76 Kind
::Nightly
=> "nightly",
77 Kind
::Stable
=> "stable",
81 /// Returns `true` if this channel supports feature flags. In other words,
82 /// returns `true` if the channel is either `dev` or `nightly`.
87 /// use version_check::Channel;
89 /// let dev = Channel::parse("1.3.0-dev").unwrap();
90 /// assert!(dev.supports_features());
92 /// let nightly = Channel::parse("1.42.2-nightly").unwrap();
93 /// assert!(nightly.supports_features());
95 /// let beta = Channel::parse("1.32.0-beta").unwrap();
96 /// assert!(!beta.supports_features());
98 /// let stable = Channel::parse("1.4.0").unwrap();
99 /// assert!(!stable.supports_features());
101 pub fn supports_features(&self) -> bool
{
103 Kind
::Dev
| Kind
::Nightly
=> true,
104 Kind
::Beta
| Kind
::Stable
=> false
108 /// Returns `true` if this channel is `dev` and `false` otherwise.
113 /// use version_check::Channel;
115 /// let dev = Channel::parse("1.3.0-dev").unwrap();
116 /// assert!(dev.is_dev());
118 /// let stable = Channel::parse("1.0.0").unwrap();
119 /// assert!(!stable.is_dev());
121 pub fn is_dev(&self) -> bool
{
128 /// Returns `true` if this channel is `nightly` and `false` otherwise.
133 /// use version_check::Channel;
135 /// let nightly = Channel::parse("1.3.0-nightly").unwrap();
136 /// assert!(nightly.is_nightly());
138 /// let stable = Channel::parse("1.0.0").unwrap();
139 /// assert!(!stable.is_nightly());
141 pub fn is_nightly(&self) -> bool
{
143 Kind
::Nightly
=> true,
148 /// Returns `true` if this channel is `beta` and `false` otherwise.
153 /// use version_check::Channel;
155 /// let beta = Channel::parse("1.3.0-beta").unwrap();
156 /// assert!(beta.is_beta());
158 /// let stable = Channel::parse("1.0.0").unwrap();
159 /// assert!(!stable.is_beta());
161 pub fn is_beta(&self) -> bool
{
168 /// Returns `true` if this channel is `stable` and `false` otherwise.
173 /// use version_check::Channel;
175 /// let stable = Channel::parse("1.0.0").unwrap();
176 /// assert!(stable.is_stable());
178 /// let beta = Channel::parse("1.3.0-beta").unwrap();
179 /// assert!(!beta.is_stable());
181 pub fn is_stable(&self) -> bool
{
183 Kind
::Stable
=> true,
189 impl fmt
::Display
for Channel
{
190 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
191 write
!(f
, "{}", self.as_str())