use std::{fmt, str::FromStr};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Tier {
Read,
Mutate,
Destructive,
}
impl Tier {
#[must_use]
pub const fn is_mutating(self) -> bool {
matches!(self, Self::Mutate | Self::Destructive)
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Read => "read",
Self::Mutate => "mutate",
Self::Destructive => "destructive",
}
}
}
impl fmt::Display for Tier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Tier {
type Err = ParseTierError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"read" => Ok(Self::Read),
"mutate" => Ok(Self::Mutate),
"destructive" => Ok(Self::Destructive),
other => Err(ParseTierError {
value: other.to_owned(),
}),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
#[error("invalid tier {value:?}: must be one of read, mutate, destructive")]
pub struct ParseTierError {
value: String,
}