use super::{v03, v10};
use std::convert::TryFrom;
use std::fmt;
use std::fmt::Formatter;
pub(crate) const SPEC_VERSIONS: [&str; 2] = ["0.3", "1.0"];
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub enum SpecVersion {
V03,
V10,
}
impl SpecVersion {
#[inline]
pub fn as_str(&self) -> &str {
match self {
SpecVersion::V03 => "0.3",
SpecVersion::V10 => "1.0",
}
}
#[inline]
pub fn attribute_names(&self) -> &'static [&'static str] {
match self {
SpecVersion::V03 => &v03::ATTRIBUTE_NAMES,
SpecVersion::V10 => &v10::ATTRIBUTE_NAMES,
}
}
}
impl fmt::Display for SpecVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[derive(Debug)]
pub struct UnknownSpecVersion {
spec_version_value: String,
}
impl fmt::Display for UnknownSpecVersion {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Invalid specversion {}", self.spec_version_value)
}
}
impl std::error::Error for UnknownSpecVersion {}
impl TryFrom<&str> for SpecVersion {
type Error = UnknownSpecVersion;
fn try_from(value: &str) -> Result<Self, UnknownSpecVersion> {
match value {
"0.3" => Ok(SpecVersion::V03),
"1.0" => Ok(SpecVersion::V10),
_ => Err(UnknownSpecVersion {
spec_version_value: value.to_string(),
}),
}
}
}