const INPUT: &str = "Input";
const OUTPUT: &str = "Output";
const INOUT: &str = "InOut";
const INPUT_TYPE: &str = "input_port";
const OUTPUT_TYPE: &str = "output_port";
const INOUT_TYPE: &str = "inout_port";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum PortDirection {
In,
Out,
InOut,
}
impl core::fmt::Display for PortDirection {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl PortDirection {
#[must_use]
pub const fn as_str(&self) -> &str {
match self {
Self::In => INPUT,
Self::Out => OUTPUT,
Self::InOut => INOUT,
}
}
#[must_use]
pub const fn type_str(self) -> &'static str {
match self {
Self::In => INPUT_TYPE,
Self::Out => OUTPUT_TYPE,
Self::InOut => INOUT_TYPE,
}
}
}
impl TryFrom<&str> for PortDirection {
type Error = crate::port::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let value = value.to_lowercase();
match value.as_ref() {
INPUT_TYPE | INPUT => Ok(Self::In),
OUTPUT_TYPE | OUTPUT => Ok(Self::Out),
INOUT_TYPE | INOUT => Ok(Self::InOut),
_ => Err(crate::port::Error::CouldNotConvert {
value: value.into(),
port: "direction".into(),
}),
}
}
}