use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("unrecognized status format: {0:?}")]
pub struct ParseStatusFormatError(pub String);
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, strum::Display)]
pub enum StatusFormat {
#[default]
#[strum(to_string = "1")]
V1,
#[strum(to_string = "2")]
V2,
#[strum(to_string = "3")]
V3,
}
impl FromStr for StatusFormat {
type Err = ParseStatusFormatError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
match input {
"1" => Ok(Self::V1),
"2" => Ok(Self::V2),
"3" => Ok(Self::V3),
other => Err(ParseStatusFormatError(other.to_string())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use test_case::test_case;
#[test_case(StatusFormat::V1)]
#[test_case(StatusFormat::V2)]
#[test_case(StatusFormat::V3)]
fn display_roundtrip(fmt: StatusFormat) {
let string = fmt.to_string();
assert_eq!(string.parse::<StatusFormat>().unwrap(), fmt);
}
#[test]
fn display_values() {
assert_eq!(StatusFormat::V1.to_string(), "1");
assert_eq!(StatusFormat::V2.to_string(), "2");
assert_eq!(StatusFormat::V3.to_string(), "3");
}
#[test]
fn parse_invalid() {
assert!("4".parse::<StatusFormat>().is_err());
assert!("".parse::<StatusFormat>().is_err());
}
}