use core::{fmt, str::FromStr};
use Weekday::*;
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Weekday {
Monday = 0,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
impl Weekday {
pub const COUNT: usize = 7;
#[inline(always)]
pub const fn previous(self) -> Weekday {
self.previous_nth(1)
}
#[inline]
pub const fn previous_nth(self, nth: usize) -> Weekday {
Self::from_monday_index_unchecked(self.index_from_monday().wrapping_sub(nth) % Self::COUNT)
}
#[inline(always)]
pub const fn next(self) -> Weekday {
self.next_nth(1)
}
#[inline]
pub const fn next_nth(self, nth: usize) -> Weekday {
Self::from_monday_index_unchecked(self.index_from_monday().wrapping_add(nth) % Self::COUNT)
}
}
impl Weekday {
#[inline(always)]
pub const fn number_from_monday(self) -> u8 {
self.index_from_monday() as u8 + 1
}
#[inline(always)]
pub const fn index_from_monday(self) -> usize {
self as _
}
#[inline]
pub const fn from_monday_number(n: u8) -> Result<Weekday, &'static str> {
match n {
1 => Ok(Monday),
2 => Ok(Tuesday),
3 => Ok(Wednesday),
4 => Ok(Thursday),
5 => Ok(Friday),
6 => Ok(Saturday),
7 => Ok(Sunday),
_ => Err("The weekday number must be between 1 and 7."),
}
}
#[inline]
pub const fn from_monday_index(index: usize) -> Result<Weekday, &'static str> {
match index {
0 => Ok(Monday),
1 => Ok(Tuesday),
2 => Ok(Wednesday),
3 => Ok(Thursday),
4 => Ok(Friday),
5 => Ok(Saturday),
6 => Ok(Sunday),
_ => Err("The weekday number must be between 0 and 6."),
}
}
#[inline]
pub const fn from_monday_index_unchecked(index: usize) -> Self {
match index {
0 => Monday,
1 => Tuesday,
2 => Wednesday,
3 => Thursday,
4 => Friday,
5 => Saturday,
6 => Sunday,
_ => panic!("The weekday number must be between 0 and 6."),
}
}
}
impl Weekday {
#[inline(always)]
pub const fn number_from_sunday(self) -> u8 {
self.index_from_sunday() as u8 + 1
}
#[inline]
pub const fn index_from_sunday(self) -> usize {
match self {
Monday => 1,
Tuesday => 2,
Wednesday => 3,
Thursday => 4,
Friday => 5,
Saturday => 6,
Sunday => 0,
}
}
#[inline]
pub const fn from_sunday_number(n: u8) -> Result<Weekday, &'static str> {
match n {
1 => Ok(Sunday),
2 => Ok(Monday),
3 => Ok(Tuesday),
4 => Ok(Wednesday),
5 => Ok(Thursday),
6 => Ok(Friday),
7 => Ok(Saturday),
_ => Err("The weekday number must be between 1 and 7."),
}
}
#[inline]
pub const fn from_sunday_index(index: usize) -> Result<Weekday, &'static str> {
match index {
0 => Ok(Sunday),
1 => Ok(Monday),
2 => Ok(Tuesday),
3 => Ok(Wednesday),
4 => Ok(Thursday),
5 => Ok(Friday),
6 => Ok(Saturday),
_ => Err("The weekday number must be between 0 and 6."),
}
}
#[inline]
pub const fn from_sunday_index_unchecked(index: usize) -> Self {
match index {
0 => Sunday,
1 => Monday,
2 => Tuesday,
3 => Wednesday,
4 => Thursday,
5 => Friday,
6 => Saturday,
_ => panic!("The weekday number must be between 0 and 6."),
}
}
}
impl Weekday {
pub fn abbr3(self) -> &'static str {
match self {
Monday => "Mon",
Tuesday => "Tue",
Wednesday => "Wed",
Thursday => "Thu",
Friday => "Fri",
Saturday => "Sat",
Sunday => "Sun",
}
}
pub const Mon: Weekday = Weekday::Monday;
pub const Tue: Weekday = Weekday::Tuesday;
pub const Wed: Weekday = Weekday::Wednesday;
pub const Thu: Weekday = Weekday::Thursday;
pub const Fri: Weekday = Weekday::Friday;
pub const Sat: Weekday = Weekday::Saturday;
pub const Sun: Weekday = Weekday::Sunday;
pub fn abbr2(self) -> &'static str {
match self {
Monday => "MO",
Tuesday => "TU",
Wednesday => "WE",
Thursday => "TH",
Friday => "FR",
Saturday => "SA",
Sunday => "SU",
}
}
pub const MO: Weekday = Weekday::Monday;
pub const TU: Weekday = Weekday::Tuesday;
pub const WE: Weekday = Weekday::Wednesday;
pub const TH: Weekday = Weekday::Thursday;
pub const FR: Weekday = Weekday::Friday;
pub const SA: Weekday = Weekday::Saturday;
pub const SU: Weekday = Weekday::Sunday;
pub fn abbr1(self) -> &'static str {
match self {
Monday => "M",
Tuesday => "T",
Wednesday => "W",
Thursday => "H",
Friday => "F",
Saturday => "A",
Sunday => "U",
}
}
pub const M: Weekday = Weekday::Monday;
pub const T: Weekday = Weekday::Tuesday;
pub const W: Weekday = Weekday::Wednesday;
pub const H: Weekday = Weekday::Thursday;
pub const F: Weekday = Weekday::Friday;
pub const A: Weekday = Weekday::Saturday;
pub const U: Weekday = Weekday::Sunday;
pub const fn emoji(self) -> char {
match self {
Monday => '🌕',
Tuesday => '🏹',
Wednesday => '🧙',
Thursday => '⚡',
Friday => '💕',
Saturday => '💰',
Sunday => '🌞',
}
}
pub const fn planet(self) -> char {
match self {
Monday => '☽',
Tuesday => '♂',
Wednesday => '☿',
Thursday => '♃',
Friday => '♀',
Saturday => '♄',
Sunday => '☀',
}
}
pub const fn planet_name(self) -> &'static str {
match self {
Monday => "Moon",
Tuesday => "Mars",
Wednesday => "Mercury",
Thursday => "Jupiter",
Friday => "Venus",
Saturday => "Saturn",
Sunday => "Sun",
}
}
}
impl fmt::Display for Weekday {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Monday => "Monday",
Tuesday => "Tuesday",
Wednesday => "Wednesday",
Thursday => "Thursday",
Friday => "Friday",
Saturday => "Saturday",
Sunday => "Sunday",
})
}
}
impl FromStr for Weekday {
type Err = &'static str;
fn from_str(s: &str) -> Result<Weekday, Self::Err> {
if s.eq_ignore_ascii_case("Monday") {
Ok(Monday)
} else if s.eq_ignore_ascii_case("Tuesday") {
Ok(Tuesday)
} else if s.eq_ignore_ascii_case("Wednesday") {
Ok(Wednesday)
} else if s.eq_ignore_ascii_case("Thursday") {
Ok(Thursday)
} else if s.eq_ignore_ascii_case("Friday") {
Ok(Friday)
} else if s.eq_ignore_ascii_case("Saturday") {
Ok(Saturday)
} else if s.eq_ignore_ascii_case("Sunday") {
Ok(Sunday)
} else if s.eq_ignore_ascii_case("Mon") {
Ok(Monday)
} else if s.eq_ignore_ascii_case("Tue") {
Ok(Tuesday)
} else if s.eq_ignore_ascii_case("Wed") {
Ok(Wednesday)
} else if s.eq_ignore_ascii_case("Thu") {
Ok(Thursday)
} else if s.eq_ignore_ascii_case("Fri") {
Ok(Friday)
} else if s.eq_ignore_ascii_case("Sat") {
Ok(Saturday)
} else if s.eq_ignore_ascii_case("Sun") {
Ok(Sunday)
} else if s.eq_ignore_ascii_case("MO") {
Ok(Monday)
} else if s.eq_ignore_ascii_case("TU") {
Ok(Tuesday)
} else if s.eq_ignore_ascii_case("WE") {
Ok(Wednesday)
} else if s.eq_ignore_ascii_case("TH") {
Ok(Thursday)
} else if s.eq_ignore_ascii_case("FR") {
Ok(Friday)
} else if s.eq_ignore_ascii_case("SA") {
Ok(Saturday)
} else if s.eq_ignore_ascii_case("SU") {
Ok(Sunday)
} else if s.eq_ignore_ascii_case("M") {
Ok(Monday)
} else if s.eq_ignore_ascii_case("T") {
Ok(Tuesday)
} else if s.eq_ignore_ascii_case("W") {
Ok(Wednesday)
} else if s.eq_ignore_ascii_case("H") {
Ok(Thursday)
} else if s.eq_ignore_ascii_case("F") {
Ok(Friday)
} else if s.eq_ignore_ascii_case("S") {
Ok(Saturday)
} else if s.eq_ignore_ascii_case("U") {
Ok(Sunday)
} else {
Err("Invalid weekday name.")
}
}
}