rust_releases_core/
channel.rs

1use crate::CoreError;
2use std::convert::TryFrom;
3use std::fmt::{Display, Formatter};
4
5/// Enumerates the Rust release channels
6#[derive(Debug, Copy, Clone, Eq, PartialEq)]
7pub enum Channel {
8    /// An identifier for the `beta` release channel
9    Beta,
10    /// An identifier for the `nightly` release channel
11    Nightly,
12    /// An identifier for the `stable` release channel
13    Stable,
14}
15
16impl TryFrom<&str> for Channel {
17    type Error = CoreError;
18
19    fn try_from(item: &str) -> Result<Self, Self::Error> {
20        Ok(match item {
21            "beta" => Self::Beta,
22            "nightly" => Self::Nightly,
23            "stable" => Self::Stable,
24            unsupported => return Err(CoreError::NoSuchChannel(unsupported.to_string())),
25        })
26    }
27}
28
29impl From<Channel> for &str {
30    fn from(channel: Channel) -> Self {
31        match channel {
32            Channel::Beta => "beta",
33            Channel::Nightly => "nightly",
34            Channel::Stable => "stable",
35        }
36    }
37}
38
39impl Display for Channel {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        let channel: Channel = *self;
42        f.write_str(channel.into())
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use yare::parameterized;
50
51    #[parameterized(
52        beta = { "beta", Channel::Beta },
53        nightly = { "nightly", Channel::Nightly },
54        stable = { "stable", Channel::Stable },
55    )]
56    fn channel_from_str(input: &str, expected: Channel) {
57        assert_eq!(Channel::try_from(input).unwrap(), expected);
58    }
59
60    #[parameterized(
61        beta = { Channel::Beta, "beta" },
62        nightly = { Channel::Nightly, "nightly" },
63        stable = { Channel::Stable, "stable" },
64    )]
65    fn channel_into_str(input: Channel, expected: &str) {
66        assert_eq!(Into::<&str>::into(input), expected);
67    }
68}