@@ -8,7 +8,8 @@ use chrono::{Datelike, FixedOffset, Utc};
88#[ cfg( feature = "today" ) ]
99const SERVER_UTC_OFFSET : i32 = -5 ;
1010
11- /// A valid day number of advent (i.e. an integer in range 1 to 25).
11+ /// A valid day number of advent (i.e. an integer in range 1 to 12).
12+ /// Before the year 2025, allow integers up to 25.
1213///
1314/// # Display
1415/// This value displays as a two digit number.
@@ -21,11 +22,20 @@ const SERVER_UTC_OFFSET: i32 = -5;
2122#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
2223pub struct Day ( u8 ) ;
2324
25+ macro_rules! day_count {
26+ ( ) => {
27+ match const_str:: parse!( env!( "AOC_YEAR" ) , u16 ) < 2025 {
28+ true => 25u8 ,
29+ false => 12u8 ,
30+ }
31+ }
32+ }
33+
2434impl Day {
2535 /// Creates a [`Day`] from the provided value if it's in the valid range,
2636 /// returns [`None`] otherwise.
2737 pub const fn new ( day : u8 ) -> Option < Self > {
28- if day == 0 || day > 25 {
38+ if day == 0 || day > day_count ! ( ) {
2939 return None ;
3040 }
3141 Some ( Self ( day) )
@@ -39,11 +49,12 @@ impl Day {
3949
4050#[ cfg( feature = "today" ) ]
4151impl Day {
42- /// Returns the current day if it's between the 1st and the 25th of december, `None` otherwise.
52+ /// Returns the current day if it's between the 1st and the 12th of december, `None` otherwise.
53+ /// Accepts days up to the 25th if the Year is before 2025.
4354 pub fn today ( ) -> Option < Self > {
4455 let offset = FixedOffset :: east_opt ( SERVER_UTC_OFFSET * 3600 ) ?;
4556 let today = Utc :: now ( ) . with_timezone ( & offset) ;
46- if today. month ( ) == 12 && today. day ( ) <= 25 {
57+ if today. month ( ) == day_count ! ( ) as u32 && today. day ( ) <= day_count ! ( ) as u32 {
4758 Self :: new ( u8:: try_from ( today. day ( ) ) . ok ( ) ?)
4859 } else {
4960 None
@@ -88,18 +99,18 @@ impl Error for DayFromStrError {}
8899
89100impl Display for DayFromStrError {
90101 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
91- f. write_str ( "expecting a day number between 1 and 25 " )
102+ f. write_str ( "expecting a day number between 1 and {{day_count!()}} " )
92103 }
93104}
94105
95106/* -------------------------------------------------------------------------- */
96107
97- /// An iterator that yields every day of advent from the 1st to the 25th .
108+ /// An iterator that yields every day of advent from the 1st to the 12th .
98109pub fn all_days ( ) -> AllDays {
99110 AllDays :: new ( )
100111}
101112
102- /// An iterator that yields every day of advent from the 1st to the 25th .
113+ /// An iterator that yields every day of advent from the 1st to the 12th .
103114pub struct AllDays {
104115 current : u8 ,
105116}
@@ -115,10 +126,10 @@ impl Iterator for AllDays {
115126 type Item = Day ;
116127
117128 fn next ( & mut self ) -> Option < Self :: Item > {
118- if self . current > 25 {
129+ if self . current > day_count ! ( ) {
119130 return None ;
120131 }
121- // NOTE: the iterator starts at 1 and we have verified that the value is not above 25 .
132+ // NOTE: the iterator starts at 1 and we have verified that the value is not above 12 .
122133 let day = Day ( self . current ) ;
123134 self . current += 1 ;
124135
@@ -134,7 +145,7 @@ macro_rules! day {
134145 ( $day: expr) => {
135146 const {
136147 $crate:: template:: Day :: new( $day)
137- . expect( "invalid day number, expecting a value between 1 and 25 " )
148+ . expect( "invalid day number, expecting a value between 1 and 12 " )
138149 }
139150 } ;
140151}
@@ -161,19 +172,21 @@ mod tests {
161172 assert_eq ! ( iter. next( ) , Some ( Day ( 10 ) ) ) ;
162173 assert_eq ! ( iter. next( ) , Some ( Day ( 11 ) ) ) ;
163174 assert_eq ! ( iter. next( ) , Some ( Day ( 12 ) ) ) ;
164- assert_eq ! ( iter. next( ) , Some ( Day ( 13 ) ) ) ;
165- assert_eq ! ( iter. next( ) , Some ( Day ( 14 ) ) ) ;
166- assert_eq ! ( iter. next( ) , Some ( Day ( 15 ) ) ) ;
167- assert_eq ! ( iter. next( ) , Some ( Day ( 16 ) ) ) ;
168- assert_eq ! ( iter. next( ) , Some ( Day ( 17 ) ) ) ;
169- assert_eq ! ( iter. next( ) , Some ( Day ( 18 ) ) ) ;
170- assert_eq ! ( iter. next( ) , Some ( Day ( 19 ) ) ) ;
171- assert_eq ! ( iter. next( ) , Some ( Day ( 20 ) ) ) ;
172- assert_eq ! ( iter. next( ) , Some ( Day ( 21 ) ) ) ;
173- assert_eq ! ( iter. next( ) , Some ( Day ( 22 ) ) ) ;
174- assert_eq ! ( iter. next( ) , Some ( Day ( 23 ) ) ) ;
175- assert_eq ! ( iter. next( ) , Some ( Day ( 24 ) ) ) ;
176- assert_eq ! ( iter. next( ) , Some ( Day ( 25 ) ) ) ;
175+ if day_count ! ( ) > 12 {
176+ assert_eq ! ( iter. next( ) , Some ( Day ( 13 ) ) ) ;
177+ assert_eq ! ( iter. next( ) , Some ( Day ( 14 ) ) ) ;
178+ assert_eq ! ( iter. next( ) , Some ( Day ( 15 ) ) ) ;
179+ assert_eq ! ( iter. next( ) , Some ( Day ( 16 ) ) ) ;
180+ assert_eq ! ( iter. next( ) , Some ( Day ( 17 ) ) ) ;
181+ assert_eq ! ( iter. next( ) , Some ( Day ( 18 ) ) ) ;
182+ assert_eq ! ( iter. next( ) , Some ( Day ( 19 ) ) ) ;
183+ assert_eq ! ( iter. next( ) , Some ( Day ( 20 ) ) ) ;
184+ assert_eq ! ( iter. next( ) , Some ( Day ( 21 ) ) ) ;
185+ assert_eq ! ( iter. next( ) , Some ( Day ( 22 ) ) ) ;
186+ assert_eq ! ( iter. next( ) , Some ( Day ( 23 ) ) ) ;
187+ assert_eq ! ( iter. next( ) , Some ( Day ( 24 ) ) ) ;
188+ assert_eq ! ( iter. next( ) , Some ( Day ( 25 ) ) ) ;
189+ }
177190 assert_eq ! ( iter. next( ) , None ) ;
178191 }
179192}
0 commit comments