rust_releases_core/
channel.rs1use crate::CoreError;
2use std::convert::TryFrom;
3use std::fmt::{Display, Formatter};
4
5#[derive(Debug, Copy, Clone, Eq, PartialEq)]
7pub enum Channel {
8 Beta,
10 Nightly,
12 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}