use std::{fmt::Display, str::FromStr};
use serde::{Deserialize, Serialize};
#[derive(Debug, Eq, PartialEq, Copy, Clone, Deserialize, Serialize)]
pub enum Status {
Open,
Closed,
}
impl FromStr for Status {
type Err = decode::Error;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let value = match value {
"open" => Self::Open,
"closed" => Self::Closed,
other => return Err(decode::Error::UnknownStatusString(other.to_owned())),
};
Ok(value)
}
}
impl TryFrom<u64> for Status {
type Error = decode::Error;
fn try_from(value: u64) -> Result<Self, Self::Error> {
let value = match value {
1 => Self::Open,
2 => Self::Closed,
other => return Err(decode::Error::UnknownStatus(other)),
};
Ok(value)
}
}
impl From<Status> for u64 {
fn from(value: Status) -> Self {
match value {
Status::Open => 1,
Status::Closed => 2,
}
}
}
impl Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Status::Open => f.write_str("open"),
Status::Closed => f.write_str("closed"),
}
}
}
#[allow(missing_docs)]
pub mod decode {
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("The status with id ({0}) is not known.")]
UnknownStatus(u64),
#[error("The status named ({0}) is not known.")]
UnknownStatusString(String),
}
}