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