use std::fmt;
use std::str::FromStr;
use crate::types::hooks::InvalidMatchValue;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MatchValue {
Startup,
Resume,
Clear,
Compact,
Read,
Edit,
Write,
}
impl MatchValue {
pub fn new(text: &str) -> Result<Self, InvalidMatchValue> {
match text {
"startup" => Ok(Self::Startup),
"resume" => Ok(Self::Resume),
"clear" => Ok(Self::Clear),
"compact" => Ok(Self::Compact),
"read" => Ok(Self::Read),
"edit" => Ok(Self::Edit),
"write" => Ok(Self::Write),
_ => Err(InvalidMatchValue::new(text)),
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Startup => "startup",
Self::Resume => "resume",
Self::Clear => "clear",
Self::Compact => "compact",
Self::Read => "read",
Self::Edit => "edit",
Self::Write => "write",
}
}
}
impl fmt::Display for MatchValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for MatchValue {
type Err = InvalidMatchValue;
fn from_str(text: &str) -> Result<Self, Self::Err> {
Self::new(text)
}
}
impl TryFrom<&str> for MatchValue {
type Error = InvalidMatchValue;
fn try_from(text: &str) -> Result<Self, Self::Error> {
Self::new(text)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn match_value_parses_the_seven_values() {
for name in [
"startup", "resume", "clear", "compact", "read", "edit", "write",
] {
assert_eq!(
MatchValue::new(name).map(|value| value.to_string()),
Ok(name.to_owned())
);
}
assert_eq!(MatchValue::new("bash"), Err(InvalidMatchValue::new("bash")));
}
#[test]
fn match_value_builds_alike_through_new_from_str_and_try_from() {
fn shown(built: Result<MatchValue, InvalidMatchValue>) -> Result<String, String> {
built
.map(|value| value.to_string())
.map_err(|error| error.to_string())
}
for text in ["read", "bash"] {
let expected = shown(MatchValue::new(text));
assert_eq!(shown(text.parse::<MatchValue>()), expected);
assert_eq!(shown(MatchValue::try_from(text)), expected);
}
assert!(MatchValue::new("read").is_ok());
assert!(MatchValue::new("bash").is_err());
}
}