use super::THE_SOLAR_DAYS;
use std::mem::transmute;
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
#[derive(Debug, PartialOrd, Ord, PartialEq, Clone, Eq, Hash, Copy)]
pub enum SolarDay {
First,
Second,
Third,
Fourth,
Fifth,
Sixth,
Seventh,
Eighth,
Ninth,
Tenth,
Eleventh,
Twelfth,
Thirteen,
Fourteen,
Fifteen,
Sixteen,
Seventeen,
Eighteen,
Nineteen,
Twenty,
TwentyFirst,
TwentySecond,
TwentyThird,
TwentyFourth,
TwentyFifth,
TwentySixth,
TwentySeventh,
TwentyEighth,
TwentyNinth,
Thirty,
ThirtyFirst,
}
impl SolarDay {
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn from_ordinal_unsafe(number: i8) -> SolarDay {
transmute(number)
}
#[allow(clippy::should_implement_trait)]
#[inline]
pub fn from_str<S: AsRef<str>>(s: S) -> Option<SolarDay> {
let s = s.as_ref();
for (i, &t) in THE_SOLAR_DAYS.iter().enumerate() {
if t == s {
return Some(unsafe { Self::from_ordinal_unsafe(i as i8) });
}
}
None
}
#[inline]
pub fn to_str(self) -> &'static str {
let i = self as usize;
THE_SOLAR_DAYS[i]
}
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn from_u8_unsafe(day: u8) -> SolarDay {
transmute(day - 1)
}
#[inline]
pub fn from_u8(day: u8) -> Option<SolarDay> {
if day == 0 || day > 31 {
None
} else {
Some(unsafe { Self::from_u8_unsafe(day) })
}
}
#[inline]
pub fn to_u8(self) -> u8 {
self as u8 + 1
}
}
impl Display for SolarDay {
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
f.write_str(self.to_str())
}
}
impl FromStr for SolarDay {
type Err = ();
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
SolarDay::from_str(s).ok_or(())
}
}