no-more-secrets-rust/src/color.rs

41 lines
855 B
Rust
Raw Normal View History

2023-07-23 19:52:25 -07:00
use std::{convert::TryFrom, ffi::OsString};
pub(crate) enum Color {
Black = 0,
Red = 1,
Green = 2,
Yellow = 3,
Blue = 4,
Magenta = 5,
Cyan = 6,
White = 7,
}
impl Default for Color {
fn default() -> Self {
Color::Blue
}
}
impl TryFrom<OsString> for Color {
type Error = ();
fn try_from(s: OsString) -> Result<Self, ()> {
let s = match s.to_str() {
Some(s) => s,
None => return Err(()),
};
Ok(match s {
"black" => Color::Black,
"red" => Color::Red,
"green" => Color::Green,
"yellow" => Color::Yellow,
"blue" => Color::Blue,
"magenta" => Color::Magenta,
"cyan" => Color::Cyan,
"white" => Color::White,
_ => return Err(()),
})
}
}