use std::str::FromStr;
use strum::IntoEnumIterator;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, strum::EnumIter)]
#[non_exhaustive]
pub enum V1 {
Zero,
One,
Two,
Three,
}
impl std::fmt::Display for V1 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
V1::Zero => write!(f, "1.0"),
V1::One => write!(f, "1.1"),
V1::Two => write!(f, "1.2"),
V1::Three => write!(f, "1.3"),
}
}
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
serde_with::DeserializeFromStr,
serde_with::SerializeDisplay,
)]
#[non_exhaustive]
pub enum SupportedVersion {
V1(V1),
}
impl SupportedVersion {
pub fn has_same_major_version(self, other: SupportedVersion) -> bool {
match (self, other) {
(SupportedVersion::V1(_), SupportedVersion::V1(_)) => true,
}
}
pub fn all() -> impl Iterator<Item = Self> {
V1::iter().map(Self::V1)
}
}
impl Default for SupportedVersion {
fn default() -> Self {
Self::V1(V1::Three)
}
}
impl std::fmt::Display for SupportedVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SupportedVersion::V1(version) => write!(f, "{version}"),
}
}
}
impl FromStr for SupportedVersion {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"1.0" => Ok(Self::V1(V1::Zero)),
"1.1" => Ok(Self::V1(V1::One)),
"1.2" => Ok(Self::V1(V1::Two)),
"1.3" => Ok(Self::V1(V1::Three)),
_ => Err(s.to_string()),
}
}
}