68 lines
1.6 KiB
Rust
68 lines
1.6 KiB
Rust
use std::fmt;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
|
#[repr(u8)]
|
|
pub enum Weekday {
|
|
Thursday,
|
|
Friday,
|
|
Saturday,
|
|
Sunday,
|
|
Monday,
|
|
Tuesday,
|
|
Wednesday,
|
|
}
|
|
|
|
impl TryFrom<i64> for Weekday {
|
|
type Error = ParseWeekdayError;
|
|
|
|
fn try_from(value: i64) -> Result<Self, Self::Error> {
|
|
match value {
|
|
0 => Ok(Self::Thursday),
|
|
1 => Ok(Self::Friday),
|
|
2 => Ok(Self::Saturday),
|
|
3 => Ok(Self::Sunday),
|
|
4 => Ok(Self::Monday),
|
|
5 => Ok(Self::Tuesday),
|
|
6 => Ok(Self::Wednesday),
|
|
_ => Err(ParseWeekdayError),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Weekday {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(
|
|
f,
|
|
"{}",
|
|
match self {
|
|
Self::Thursday => "Thursday",
|
|
Self::Friday => "Friday",
|
|
Self::Saturday => "Saturday",
|
|
Self::Sunday => "Sunday",
|
|
Self::Monday => "Monday",
|
|
Self::Tuesday => "Tuesday",
|
|
Self::Wednesday => "Wednesday",
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
impl Weekday {
|
|
pub fn abbreviate(&self) -> &'static str {
|
|
match self {
|
|
Self::Thursday => "Thu",
|
|
Self::Friday => "Fri",
|
|
Self::Saturday => "Sat",
|
|
Self::Sunday => "Sun",
|
|
Self::Monday => "Mon",
|
|
Self::Tuesday => "Tue",
|
|
Self::Wednesday => "Wed",
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct ParseWeekdayError;
|