use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
#[non_exhaustive]
pub enum VHDLStandard {
VHDL1987,
VHDL1993,
VHDL2000,
VHDL2002,
#[default]
VHDL2008,
VHDL2019,
}
impl VHDLStandard {
pub const ALL: [Self; 6] = [
Self::VHDL1987,
Self::VHDL1993,
Self::VHDL2000,
Self::VHDL2002,
Self::VHDL2008,
Self::VHDL2019,
];
pub const fn as_str(self) -> &'static str {
match self {
Self::VHDL1987 => "1987",
Self::VHDL1993 => "1993",
Self::VHDL2000 => "2000",
Self::VHDL2002 => "2002",
Self::VHDL2008 => "2008",
Self::VHDL2019 => "2019",
}
}
pub const fn year(self) -> u16 {
match self {
Self::VHDL1987 => 1987,
Self::VHDL1993 => 1993,
Self::VHDL2000 => 2000,
Self::VHDL2002 => 2002,
Self::VHDL2008 => 2008,
Self::VHDL2019 => 2019,
}
}
pub fn is_at_least(self, other: Self) -> bool {
self >= other
}
pub fn is_before(self, other: Self) -> bool {
self < other
}
pub fn latest() -> VHDLStandard {
Self::VHDL2019
}
}
#[test]
fn order_of_standards() {
assert!(VHDLStandard::VHDL2008 > VHDLStandard::VHDL1993);
}
#[derive(Debug, Clone)]
pub struct ParseVHDLStandardError;
impl std::fmt::Display for ParseVHDLStandardError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "invalid VHDL standard")
}
}
impl std::error::Error for ParseVHDLStandardError {}
impl TryFrom<&str> for VHDLStandard {
type Error = ParseVHDLStandardError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
use VHDLStandard::*;
Ok(match value {
"1987" | "87" => VHDL1987,
"1993" | "93" => VHDL1993,
"2000" | "00" => VHDL2000,
"2002" | "02" => VHDL2002,
"2008" | "08" => VHDL2008,
"2019" | "19" => VHDL2019,
_ => return Err(ParseVHDLStandardError),
})
}
}
impl AsRef<str> for VHDLStandard {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for VHDLStandard {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}