use serde::Serialize;
use strum::{Display as StrumDisplay, EnumString};
#[derive(Debug, Copy, Clone, PartialEq, Eq, EnumString, StrumDisplay, Serialize)]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum SpecCommandEffect {
Read,
Write,
Destructive,
}
impl SpecCommandEffect {
pub fn as_str(&self) -> &'static str {
match self {
Self::Read => "read",
Self::Write => "write",
Self::Destructive => "destructive",
}
}
pub fn label(&self) -> &'static str {
match self {
Self::Read => "read-only",
Self::Write => "modifies state",
Self::Destructive => "destructive",
}
}
}
pub(crate) const EFFECT_VALUES: &str = "read, write, destructive";
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
#[test]
fn test_parse() {
assert_eq!(
SpecCommandEffect::from_str("read").unwrap(),
SpecCommandEffect::Read
);
assert_eq!(
SpecCommandEffect::from_str("destructive").unwrap(),
SpecCommandEffect::Destructive
);
assert!(SpecCommandEffect::from_str("readonly").is_err());
}
#[test]
fn test_display_roundtrips() {
for effect in [
SpecCommandEffect::Read,
SpecCommandEffect::Write,
SpecCommandEffect::Destructive,
] {
assert_eq!(
SpecCommandEffect::from_str(&effect.to_string()).unwrap(),
effect
);
assert_eq!(effect.to_string(), effect.as_str());
}
}
}