use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ApiVersion {
V1,
}
pub const CURRENT_VERSION: ApiVersion = ApiVersion::V1;
pub const SUPPORTED_VERSIONS: &[ApiVersion] = &[
ApiVersion::V1,
];
pub mod versions {
pub const V1: &str = "v1";
}
impl ApiVersion {
pub fn as_str(&self) -> &'static str {
match self {
Self::V1 => "v1",
}
}
pub fn major(&self) -> u32 {
match self {
Self::V1 => 1,
}
}
pub fn is_deprecated(&self) -> bool {
match self {
Self::V1 => false,
}
}
pub fn deprecation_message(&self) -> Option<&'static str> {
match self {
Self::V1 => None,
}
}
pub fn sunset_date(&self) -> Option<&'static str> {
match self {
Self::V1 => None,
}
}
}
impl fmt::Display for ApiVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl FromStr for ApiVersion {
type Err = VersionParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"v1" | "1" => Ok(Self::V1),
_ => Err(VersionParseError(s.to_string())),
}
}
}
#[derive(Debug, Clone)]
pub struct VersionParseError(pub String);
impl std::error::Error for VersionParseError {}
impl fmt::Display for VersionParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Invalid API version: '{}'", self.0)
}
}