rust_toolchain/
channel.rs

1mod beta;
2mod nightly;
3mod stable;
4
5use crate::{Date, RustVersion};
6
7pub use beta::Beta;
8pub use nightly::Nightly;
9pub use stable::Stable;
10
11/// A Rust release [`channel`].
12///
13/// Does not include the once used `Alpha` release channel, which has not been used post `1.0.0`.
14///
15/// # Variants
16///
17/// See also: [`Stable`], [`Beta`] and [`Nightly`].
18///
19/// # Reading materials
20///
21/// - [`rustup concepts: channels`]
22/// - [`rust book: how rust is made`]
23/// - [`rust forge: rust release channel layout`]
24/// - [`rust forge: current rust release versions`]
25///
26///
27/// [`channel`]: https://forge.rust-lang.org/#current-release-versions
28/// [`rustup concepts: channels`]: https://rust-lang.github.io/rustup/concepts/channels.html
29/// [`rust book: how rust is made`]: https://doc.rust-lang.org/book/appendix-07-nightly-rust.html
30/// [`rust forge: rust release channel layout`]: https://forge.rust-lang.org/infra/channel-layout.html#the-rust-release-channel-layout
31/// [`rust forge: current rust release versions`]: https://forge.rust-lang.org/#current-release-versions
32#[derive(Clone, Debug, Eq, Hash, PartialEq)]
33pub enum Channel {
34    /// The stable release channel
35    Stable(Stable),
36    /// The beta release channel
37    Beta(Beta),
38    /// The nightly release channel
39    Nightly(Nightly),
40}
41
42impl Channel {
43    /// Create a new [`Stable`] channel instance.
44    pub fn stable(version: RustVersion) -> Self {
45        Channel::Stable(Stable { version })
46    }
47
48    /// Create a new [`Beta`] channel instance.
49    pub fn beta(version: RustVersion) -> Self {
50        Channel::Beta(Beta {
51            version,
52            prerelease: None,
53        })
54    }
55
56    /// Create a new [`Nightly`] channel instance.
57    pub fn nightly(date: Date) -> Self {
58        Channel::Nightly(Nightly { date })
59    }
60
61    /// Whether the given [`Channel`] is of the [`Stable`] variant.
62    pub fn is_stable(&self) -> bool {
63        matches!(self, Self::Stable(_))
64    }
65
66    /// Whether the given [`Channel`] is of the [`Beta`] variant.
67    pub fn is_beta(&self) -> bool {
68        matches!(self, Self::Beta(_))
69    }
70
71    /// Whether the given [`Channel`] is of the [`Nightly`] variant.
72    pub fn is_nightly(&self) -> bool {
73        matches!(self, Self::Nightly(_))
74    }
75
76    /// Returns the release version, or None, if it's a nightly release.
77    pub fn version(&self) -> Option<RustVersion> {
78        match self {
79            Channel::Stable(v) => Some(v.version),
80            Channel::Beta(v) => Some(v.version),
81            Channel::Nightly(_) => None,
82        }
83    }
84
85    /// Returns the release date, or
86    pub fn date(&self) -> Option<Date> {
87        match self {
88            Channel::Stable(_) => None,
89            Channel::Beta(_) => None,
90            Channel::Nightly(v) => Some(v.date.clone()),
91        }
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use crate::{Date, RustVersion};
99    use yare::parameterized;
100
101    #[test]
102    fn create_channel_stable() {
103        let stable = Channel::stable(RustVersion::new(1, 2, 3));
104
105        assert!(stable.is_stable());
106        assert!(!stable.is_beta());
107        assert!(!stable.is_nightly());
108    }
109
110    #[test]
111    fn create_channel_beta() {
112        let stable = Channel::beta(RustVersion::new(1, 2, 3));
113
114        assert!(!stable.is_stable());
115        assert!(stable.is_beta());
116        assert!(!stable.is_nightly());
117    }
118
119    #[test]
120    fn create_channel_nightly() {
121        let stable = Channel::nightly(Date::new(1, 1, 1));
122
123        assert!(!stable.is_stable());
124        assert!(!stable.is_beta());
125        assert!(stable.is_nightly());
126    }
127
128    #[parameterized(
129        stable = { Channel::stable("1.2.3".parse().unwrap()), Some(RustVersion::new(1,2,3)) },
130        beta = { Channel::beta("1.2.3".parse().unwrap()), Some(RustVersion::new(1,2,3)) },
131        nightly = { Channel::nightly(Date::new(2024, 1, 1)), None }
132    )]
133    fn version(c: Channel, expected: Option<RustVersion>) {
134        assert_eq!(c.version(), expected)
135    }
136}