intermodal/src/use_color.rs
Casey Rodarmor fa6d4e6ad0
Revise command line value names
Make command line value names make sense in context. For example,
`--announce URL` instead of `--announce ANNOUNCE`.

type: documentation
2020-04-07 19:01:14 -07:00

53 lines
1.2 KiB
Rust

use crate::common::*;
#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) enum UseColor {
Auto,
Always,
Never,
}
impl UseColor {
pub(crate) const ALWAYS: &'static str = "always";
pub(crate) const AUTO: &'static str = "auto";
pub(crate) const NEVER: &'static str = "never";
pub(crate) const VALUES: &'static [&'static str] = &[Self::AUTO, Self::ALWAYS, Self::NEVER];
}
impl FromStr for UseColor {
type Err = Infallible;
fn from_str(text: &str) -> Result<Self, Self::Err> {
match text.to_lowercase().as_str() {
Self::AUTO => Ok(Self::Auto),
Self::ALWAYS => Ok(Self::Always),
Self::NEVER => Ok(Self::Never),
_ => unreachable!(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_str() {
assert_eq!(UseColor::Auto, UseColor::AUTO.parse().unwrap());
assert_eq!(UseColor::Always, UseColor::ALWAYS.parse().unwrap());
assert_eq!(UseColor::Never, UseColor::NEVER.parse().unwrap());
assert_eq!(
UseColor::Auto,
UseColor::AUTO.to_uppercase().parse().unwrap()
);
assert_eq!(
UseColor::Always,
UseColor::ALWAYS.to_uppercase().parse().unwrap()
);
assert_eq!(
UseColor::Never,
UseColor::NEVER.to_uppercase().parse().unwrap()
);
}
}