use wasefire_error::{Code, Error};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Side {
A,
B,
}
impl Side {
pub const LIST: [Side; 2] = [Side::A, Side::B];
pub fn opposite(self) -> Self {
match self {
Side::A => Side::B,
Side::B => Side::A,
}
}
pub fn name(self) -> &'static str {
match self {
Side::A => "A",
Side::B => "B",
}
}
}
impl From<bool> for Side {
fn from(is_b: bool) -> Self {
if is_b { Side::B } else { Side::A }
}
}
impl core::fmt::Display for Side {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.name().fmt(f)
}
}
impl core::str::FromStr for Side {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Error> {
match input {
"A" => Ok(Side::A),
"B" => Ok(Side::B),
_ => Err(Error::user(Code::InvalidArgument)),
}
}
}