1#[repr(transparent)]
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
23pub struct DayFlags(u8);
24
25impl DayFlags {
26 pub const WEEKEND: Self = Self(1 << 0);
28
29 pub const HOLIDAY: Self = Self(1 << 1);
31
32 pub const DAY_OFF: Self = Self(1 << 2);
34
35 pub const WORKING_DAY: Self = Self(1 << 3);
37
38 pub const SHORT_DAY: Self = Self(1 << 4);
40
41 pub const TRANSFERRED: Self = Self(1 << 5);
43
44 pub const EMPTY: Self = Self(0);
46
47 #[inline]
49 #[must_use]
50 pub const fn with(self, other: Self) -> Self {
51 Self(self.0 | other.0)
52 }
53
54 #[inline]
56 #[must_use]
57 pub const fn with_if(self, condition: bool, other: Self) -> Self {
58 Self(self.0 | (other.0 & (condition as u8).wrapping_neg()))
59 }
60
61 #[inline]
63 pub const fn is_weekend(self) -> bool {
64 self.0 & Self::WEEKEND.0 != 0
65 }
66
67 #[inline]
69 pub const fn is_holiday(self) -> bool {
70 self.0 & Self::HOLIDAY.0 != 0
71 }
72
73 #[inline]
75 pub const fn is_day_off(self) -> bool {
76 self.0 & Self::DAY_OFF.0 != 0
77 }
78
79 #[inline]
81 pub const fn is_working_day(self) -> bool {
82 self.0 & Self::WORKING_DAY.0 != 0
83 }
84
85 #[inline]
87 pub const fn is_short_day(self) -> bool {
88 self.0 & Self::SHORT_DAY.0 != 0
89 }
90
91 #[inline]
93 pub const fn is_transferred(self) -> bool {
94 self.0 & Self::TRANSFERRED.0 != 0
95 }
96
97 #[inline]
99 pub const fn bits(self) -> u8 {
100 self.0
101 }
102
103 #[inline]
107 pub const fn from_bits(bits: u8) -> Self {
108 Self(bits)
109 }
110}
111
112#[cfg(feature = "serde")]
113impl serde::Serialize for DayFlags {
114 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
115 serializer.serialize_u8(self.0)
116 }
117}
118
119#[cfg(feature = "serde")]
120impl<'de> serde::Deserialize<'de> for DayFlags {
121 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
122 <u8 as serde::Deserialize>::deserialize(deserializer).map(Self)
123 }
124}
125
126#[cfg(all(test, feature = "serde"))]
127mod serde_tests {
128 use super::*;
129
130 #[test]
131 fn test_serde_json_as_bits() {
132 let flags = DayFlags::HOLIDAY.with(DayFlags::DAY_OFF);
133 let json = serde_json::to_string(&flags).unwrap();
134
135 assert_eq!(json, "6");
136 assert_eq!(serde_json::from_str::<DayFlags>(&json).unwrap(), flags);
137 }
138}