Skip to main content

ferroui_base/utilities/
day_of_week.rs

1//! The day of the week (.NET `System.DayOfWeek`).
2
3use std::fmt;
4
5/// A day of the week; the numeric values are the ones of .NET (`Sunday` is 0).
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[repr(u8)]
8pub enum DayOfWeek {
9    #[default]
10    Sunday = 0,
11    Monday = 1,
12    Tuesday = 2,
13    Wednesday = 3,
14    Thursday = 4,
15    Friday = 5,
16    Saturday = 6,
17}
18
19impl DayOfWeek {
20    /// The seven days, starting with Sunday.
21    pub const ALL: [DayOfWeek; 7] = [
22        DayOfWeek::Sunday,
23        DayOfWeek::Monday,
24        DayOfWeek::Tuesday,
25        DayOfWeek::Wednesday,
26        DayOfWeek::Thursday,
27        DayOfWeek::Friday,
28        DayOfWeek::Saturday,
29    ];
30
31    /// The day for a numeric value 0..=6 (the C# cast `(DayOfWeek)value`);
32    /// `None` for values that name no day.
33    pub const fn from_i32(value: i32) -> Option<DayOfWeek> {
34        if value >= 0 && value < 7 {
35            Some(Self::ALL[value as usize])
36        } else {
37            None
38        }
39    }
40
41    /// The numeric value (the C# cast `(int)day`).
42    #[inline]
43    pub const fn to_i32(self) -> i32 {
44        self as i32
45    }
46
47    /// The English name of the day, as `Enum.ToString()` gives it.
48    pub const fn name(self) -> &'static str {
49        match self {
50            DayOfWeek::Sunday => "Sunday",
51            DayOfWeek::Monday => "Monday",
52            DayOfWeek::Tuesday => "Tuesday",
53            DayOfWeek::Wednesday => "Wednesday",
54            DayOfWeek::Thursday => "Thursday",
55            DayOfWeek::Friday => "Friday",
56            DayOfWeek::Saturday => "Saturday",
57        }
58    }
59
60}
61
62impl fmt::Display for DayOfWeek {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        f.write_str(self.name())
65    }
66}
67
68impl From<DayOfWeek> for i32 {
69    #[inline]
70    fn from(value: DayOfWeek) -> i32 {
71        value as i32
72    }
73}