use crate::error::Error;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
#[allow(missing_docs)]
pub enum Weekday {
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7,
}
impl Weekday {
#[inline]
pub fn from_monday_zero_offset(offset: i8) -> Result<Weekday, Error> {
jcore::civil::Weekday::from_monday_zero_offset(offset)
.map_err(Error::jcore_range)
.map(Weekday::from_jcore)
}
#[inline]
pub fn from_monday_one_offset(offset: i8) -> Result<Weekday, Error> {
jcore::civil::Weekday::from_monday_one_offset(offset)
.map_err(Error::jcore_range)
.map(Weekday::from_jcore)
}
#[inline]
pub fn from_sunday_zero_offset(offset: i8) -> Result<Weekday, Error> {
jcore::civil::Weekday::from_sunday_zero_offset(offset)
.map_err(Error::jcore_range)
.map(Weekday::from_jcore)
}
#[inline]
pub fn from_sunday_one_offset(offset: i8) -> Result<Weekday, Error> {
jcore::civil::Weekday::from_sunday_one_offset(offset)
.map_err(Error::jcore_range)
.map(Weekday::from_jcore)
}
#[inline]
pub fn to_monday_zero_offset(self) -> i8 {
self.to_jcore().to_monday_zero_offset()
}
#[inline]
pub fn to_monday_one_offset(self) -> i8 {
self.to_jcore().to_monday_one_offset()
}
#[inline]
pub fn to_sunday_zero_offset(self) -> i8 {
self.to_jcore().to_sunday_zero_offset()
}
#[inline]
pub fn to_sunday_one_offset(self) -> i8 {
self.to_jcore().to_sunday_one_offset()
}
#[inline]
pub fn next(self) -> Weekday {
self.wrapping_add(1)
}
#[inline]
pub fn previous(self) -> Weekday {
self.wrapping_sub(1)
}
#[inline]
pub fn since(self, other: Weekday) -> i8 {
self.to_jcore().since(other.to_jcore())
}
#[inline]
pub fn until(self, other: Weekday) -> i8 {
self.to_jcore().until(other.to_jcore())
}
#[inline]
pub fn wrapping_add<D: Into<i64>>(self, days: D) -> Weekday {
Weekday::from_jcore(self.to_jcore().wrapping_add(days.into()))
}
#[inline]
pub fn wrapping_sub<D: Into<i64>>(self, days: D) -> Weekday {
Weekday::from_jcore(self.to_jcore().wrapping_sub(days.into()))
}
#[inline]
pub fn cycle_forward(self) -> WeekdaysForward {
WeekdaysForward { it: self.to_jcore().cycle_forward() }
}
#[inline]
pub fn cycle_reverse(self) -> WeekdaysReverse {
WeekdaysReverse { it: self.to_jcore().cycle_reverse() }
}
}
impl Weekday {
#[inline]
pub(crate) const fn from_jcore(weekday: jcore::civil::Weekday) -> Weekday {
match weekday {
jcore::civil::Weekday::Monday => Weekday::Monday,
jcore::civil::Weekday::Tuesday => Weekday::Tuesday,
jcore::civil::Weekday::Wednesday => Weekday::Wednesday,
jcore::civil::Weekday::Thursday => Weekday::Thursday,
jcore::civil::Weekday::Friday => Weekday::Friday,
jcore::civil::Weekday::Saturday => Weekday::Saturday,
jcore::civil::Weekday::Sunday => Weekday::Sunday,
}
}
#[inline]
pub(crate) const fn to_jcore(self) -> jcore::civil::Weekday {
match self {
Weekday::Monday => jcore::civil::Weekday::Monday,
Weekday::Tuesday => jcore::civil::Weekday::Tuesday,
Weekday::Wednesday => jcore::civil::Weekday::Wednesday,
Weekday::Thursday => jcore::civil::Weekday::Thursday,
Weekday::Friday => jcore::civil::Weekday::Friday,
Weekday::Saturday => jcore::civil::Weekday::Saturday,
Weekday::Sunday => jcore::civil::Weekday::Sunday,
}
}
}
impl core::ops::Add<i8> for Weekday {
type Output = Weekday;
#[inline]
fn add(self, rhs: i8) -> Weekday {
self.wrapping_add(rhs)
}
}
impl core::ops::Add<i16> for Weekday {
type Output = Weekday;
#[inline]
fn add(self, rhs: i16) -> Weekday {
self.wrapping_add(rhs)
}
}
impl core::ops::Add<i32> for Weekday {
type Output = Weekday;
#[inline]
fn add(self, rhs: i32) -> Weekday {
self.wrapping_add(rhs)
}
}
impl core::ops::Add<i64> for Weekday {
type Output = Weekday;
#[inline]
fn add(self, rhs: i64) -> Weekday {
self.wrapping_add(rhs)
}
}
impl core::ops::Add<Weekday> for i8 {
type Output = Weekday;
#[inline]
fn add(self, rhs: Weekday) -> Weekday {
rhs.wrapping_add(self)
}
}
impl core::ops::Add<Weekday> for i16 {
type Output = Weekday;
#[inline]
fn add(self, rhs: Weekday) -> Weekday {
rhs.wrapping_add(self)
}
}
impl core::ops::Add<Weekday> for i32 {
type Output = Weekday;
#[inline]
fn add(self, rhs: Weekday) -> Weekday {
rhs.wrapping_add(self)
}
}
impl core::ops::Add<Weekday> for i64 {
type Output = Weekday;
#[inline]
fn add(self, rhs: Weekday) -> Weekday {
rhs.wrapping_add(self)
}
}
impl core::ops::AddAssign<i8> for Weekday {
#[inline]
fn add_assign(&mut self, rhs: i8) {
*self = *self + rhs;
}
}
impl core::ops::AddAssign<i16> for Weekday {
#[inline]
fn add_assign(&mut self, rhs: i16) {
*self = *self + rhs;
}
}
impl core::ops::AddAssign<i32> for Weekday {
#[inline]
fn add_assign(&mut self, rhs: i32) {
*self = *self + rhs;
}
}
impl core::ops::AddAssign<i64> for Weekday {
#[inline]
fn add_assign(&mut self, rhs: i64) {
*self = *self + rhs;
}
}
impl core::ops::Sub<i8> for Weekday {
type Output = Weekday;
#[inline]
fn sub(self, rhs: i8) -> Weekday {
self.wrapping_sub(rhs)
}
}
impl core::ops::Sub<i16> for Weekday {
type Output = Weekday;
#[inline]
fn sub(self, rhs: i16) -> Weekday {
self.wrapping_sub(rhs)
}
}
impl core::ops::Sub<i32> for Weekday {
type Output = Weekday;
#[inline]
fn sub(self, rhs: i32) -> Weekday {
self.wrapping_sub(rhs)
}
}
impl core::ops::Sub<i64> for Weekday {
type Output = Weekday;
#[inline]
fn sub(self, rhs: i64) -> Weekday {
self.wrapping_sub(rhs)
}
}
impl core::ops::SubAssign<i8> for Weekday {
#[inline]
fn sub_assign(&mut self, rhs: i8) {
*self = *self - rhs;
}
}
impl core::ops::SubAssign<i16> for Weekday {
#[inline]
fn sub_assign(&mut self, rhs: i16) {
*self = *self - rhs;
}
}
impl core::ops::SubAssign<i32> for Weekday {
#[inline]
fn sub_assign(&mut self, rhs: i32) {
*self = *self - rhs;
}
}
impl core::ops::SubAssign<i64> for Weekday {
#[inline]
fn sub_assign(&mut self, rhs: i64) {
*self = *self - rhs;
}
}
#[cfg(test)]
impl quickcheck::Arbitrary for Weekday {
fn arbitrary(g: &mut quickcheck::Gen) -> Weekday {
let offset = crate::util::b::WeekdayMondayZero::arbitrary(g);
Weekday::from_monday_zero_offset(offset).unwrap()
}
fn shrink(&self) -> alloc::boxed::Box<dyn Iterator<Item = Weekday>> {
alloc::boxed::Box::new(
self.to_monday_zero_offset()
.shrink()
.filter_map(|n| Weekday::from_monday_zero_offset(n).ok()),
)
}
}
#[derive(Clone, Debug)]
pub struct WeekdaysForward {
it: jcore::civil::WeekdaysForward,
}
impl Iterator for WeekdaysForward {
type Item = Weekday;
#[inline]
fn next(&mut self) -> Option<Weekday> {
self.it.next().map(Weekday::from_jcore)
}
}
impl core::iter::FusedIterator for WeekdaysForward {}
#[derive(Clone, Debug)]
pub struct WeekdaysReverse {
it: jcore::civil::WeekdaysReverse,
}
impl Iterator for WeekdaysReverse {
type Item = Weekday;
#[inline]
fn next(&mut self) -> Option<Weekday> {
self.it.next().map(Weekday::from_jcore)
}
}
impl core::iter::FusedIterator for WeekdaysReverse {}