|
| 1 | +use std::error::Error; |
| 2 | +use std::fmt::Display; |
| 3 | +use std::str::FromStr; |
| 4 | + |
| 5 | +/// A valid day number of advent (i.e. an integer in range 1 to 25). |
| 6 | +/// |
| 7 | +/// # Display |
| 8 | +/// This value displays as a two digit number. |
| 9 | +/// |
| 10 | +/// ``` |
| 11 | +/// # use advent_of_code::Day; |
| 12 | +/// let day = Day::new(8).unwrap(); |
| 13 | +/// assert_eq!(day.to_string(), "08") |
| 14 | +/// ``` |
| 15 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 16 | +pub struct Day(u8); |
| 17 | + |
| 18 | +impl Day { |
| 19 | + /// Creates a [`Day`] from the provided value if it's in the valid range, |
| 20 | + /// returns [`None`] otherwise. |
| 21 | + pub fn new(day: u8) -> Option<Self> { |
| 22 | + if day == 0 || day > 25 { |
| 23 | + return None; |
| 24 | + } |
| 25 | + Some(Self(day)) |
| 26 | + } |
| 27 | + |
| 28 | + // Not part of the public API |
| 29 | + #[doc(hidden)] |
| 30 | + pub const fn __new_unchecked(day: u8) -> Self { |
| 31 | + Self(day) |
| 32 | + } |
| 33 | + |
| 34 | + /// Converts the [`Day`] into an [`u8`]. |
| 35 | + pub fn into_inner(self) -> u8 { |
| 36 | + self.0 |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl Display for Day { |
| 41 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 42 | + write!(f, "{:02}", self.0) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl PartialEq<u8> for Day { |
| 47 | + fn eq(&self, other: &u8) -> bool { |
| 48 | + self.0.eq(other) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl PartialOrd<u8> for Day { |
| 53 | + fn partial_cmp(&self, other: &u8) -> Option<std::cmp::Ordering> { |
| 54 | + self.0.partial_cmp(other) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/* -------------------------------------------------------------------------- */ |
| 59 | + |
| 60 | +impl FromStr for Day { |
| 61 | + type Err = DayFromStrError; |
| 62 | + |
| 63 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 64 | + let day = s.parse().map_err(|_| DayFromStrError)?; |
| 65 | + Self::new(day).ok_or(DayFromStrError) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// An error which can be returned when parsing a [`Day`]. |
| 70 | +#[derive(Debug)] |
| 71 | +pub struct DayFromStrError; |
| 72 | + |
| 73 | +impl Error for DayFromStrError {} |
| 74 | + |
| 75 | +impl Display for DayFromStrError { |
| 76 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 77 | + f.write_str("expecting a day number between 1 and 25") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/* -------------------------------------------------------------------------- */ |
| 82 | + |
| 83 | +/// An iterator that yields every day of advent from the 1st to the 25th. |
| 84 | +pub fn all_days() -> AllDays { |
| 85 | + AllDays::new() |
| 86 | +} |
| 87 | + |
| 88 | +/// An iterator that yields every day of advent from the 1st to the 25th. |
| 89 | +pub struct AllDays { |
| 90 | + current: u8, |
| 91 | +} |
| 92 | + |
| 93 | +impl AllDays { |
| 94 | + #[allow(clippy::new_without_default)] |
| 95 | + pub fn new() -> Self { |
| 96 | + Self { current: 1 } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl Iterator for AllDays { |
| 101 | + type Item = Day; |
| 102 | + |
| 103 | + fn next(&mut self) -> Option<Self::Item> { |
| 104 | + if self.current > 25 { |
| 105 | + return None; |
| 106 | + } |
| 107 | + // NOTE: the iterator starts at 1 and we have verified that the value is not above 25. |
| 108 | + let day = Day(self.current); |
| 109 | + self.current += 1; |
| 110 | + |
| 111 | + Some(day) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/* -------------------------------------------------------------------------- */ |
| 116 | + |
| 117 | +/// Creates a [`Day`] value in a const context. |
| 118 | +#[macro_export] |
| 119 | +macro_rules! day { |
| 120 | + ($day:expr) => {{ |
| 121 | + const _ASSERT: () = assert!( |
| 122 | + $day != 0 && $day <= 25, |
| 123 | + concat!( |
| 124 | + "invalid day number `", |
| 125 | + $day, |
| 126 | + "`, expecting a value between 1 and 25" |
| 127 | + ), |
| 128 | + ); |
| 129 | + $crate::Day::__new_unchecked($day) |
| 130 | + }}; |
| 131 | +} |
| 132 | + |
| 133 | +/* -------------------------------------------------------------------------- */ |
| 134 | + |
| 135 | +#[cfg(feature = "test_lib")] |
| 136 | +mod tests { |
| 137 | + use super::{all_days, Day}; |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn all_days_iterator() { |
| 141 | + let mut iter = all_days(); |
| 142 | + |
| 143 | + assert_eq!(iter.next(), Some(Day(1))); |
| 144 | + assert_eq!(iter.next(), Some(Day(2))); |
| 145 | + assert_eq!(iter.next(), Some(Day(3))); |
| 146 | + assert_eq!(iter.next(), Some(Day(4))); |
| 147 | + assert_eq!(iter.next(), Some(Day(5))); |
| 148 | + assert_eq!(iter.next(), Some(Day(6))); |
| 149 | + assert_eq!(iter.next(), Some(Day(7))); |
| 150 | + assert_eq!(iter.next(), Some(Day(8))); |
| 151 | + assert_eq!(iter.next(), Some(Day(9))); |
| 152 | + assert_eq!(iter.next(), Some(Day(10))); |
| 153 | + assert_eq!(iter.next(), Some(Day(11))); |
| 154 | + assert_eq!(iter.next(), Some(Day(12))); |
| 155 | + assert_eq!(iter.next(), Some(Day(13))); |
| 156 | + assert_eq!(iter.next(), Some(Day(14))); |
| 157 | + assert_eq!(iter.next(), Some(Day(15))); |
| 158 | + assert_eq!(iter.next(), Some(Day(16))); |
| 159 | + assert_eq!(iter.next(), Some(Day(17))); |
| 160 | + assert_eq!(iter.next(), Some(Day(18))); |
| 161 | + assert_eq!(iter.next(), Some(Day(19))); |
| 162 | + assert_eq!(iter.next(), Some(Day(20))); |
| 163 | + assert_eq!(iter.next(), Some(Day(21))); |
| 164 | + assert_eq!(iter.next(), Some(Day(22))); |
| 165 | + assert_eq!(iter.next(), Some(Day(23))); |
| 166 | + assert_eq!(iter.next(), Some(Day(24))); |
| 167 | + assert_eq!(iter.next(), Some(Day(25))); |
| 168 | + assert_eq!(iter.next(), None); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +/* -------------------------------------------------------------------------- */ |
0 commit comments