use crate::services::Error as ServiceError;
use std::str::FromStr;
use strum_macros::EnumIter;
#[derive(
EnumIter,
Serialize,
Deserialize,
Debug,
Copy,
Clone,
Hash,
PartialOrd,
Ord,
PartialEq,
Eq,
)]
#[serde(rename_all = "kebab-case")]
pub enum FetchDirection {
Before,
After,
}
impl FetchDirection {
#[cfg(test)]
pub fn name(self) -> &'static str {
match self {
FetchDirection::Before => "before",
FetchDirection::After => "after",
}
}
}
impl FromStr for FetchDirection {
type Err = ServiceError;
fn from_str(value: &str) -> Result<FetchDirection, ServiceError> {
match value {
"before" => Ok(FetchDirection::Before),
"after" => Ok(FetchDirection::After),
_ => Err(ServiceError::InvalidEnumValue),
}
}
}
#[test]
fn name_serde() {
use strum::IntoEnumIterator;
for variant in FetchDirection::iter() {
let output = serde_json::to_string(&variant).expect("Unable to serialize JSON");
let serde_name: String =
serde_json::from_str(&output).expect("Unable to deserialize JSON");
assert_eq!(
&serde_name,
variant.name(),
"Serde name does not match variant name",
);
let converted: FetchDirection =
serde_name.as_str().parse().expect("Could not convert item");
assert_eq!(converted, variant, "Converted item does not match variant");
}
}