Skip to main content

refix_message/value/
month_year.rs

1use super::{Date, number};
2
3/// A FIX MonthYear, such as a contract's maturity, with an optional day or week of the month.
4#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
5pub struct MonthYear {
6    year: u16,
7    month: u8,
8    day_or_week: Option<DayOrWeek>,
9}
10
11#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
12enum DayOrWeek {
13    Day(u8),
14    Week(u8),
15}
16
17impl MonthYear {
18    /// The month `month` of `year`, if it exists and the year fits FIX's four digits.
19    pub const fn new(year: u16, month: u8) -> Option<Self> {
20        Self::checked(year, month, None)
21    }
22
23    /// The day `day` of `month` in `year`, if that date exists.
24    pub const fn with_day(year: u16, month: u8, day: u8) -> Option<Self> {
25        Self::checked(year, month, Some(DayOrWeek::Day(day)))
26    }
27
28    /// The week `week` of `month` in `year`, from `1` to `5`.
29    pub const fn with_week(year: u16, month: u8, week: u8) -> Option<Self> {
30        Self::checked(year, month, Some(DayOrWeek::Week(week)))
31    }
32
33    /// The year, such as `2026`.
34    pub const fn year(&self) -> u16 {
35        self.year
36    }
37
38    /// The month, from `1` for January to `12` for December.
39    pub const fn month(&self) -> u8 {
40        self.month
41    }
42
43    /// The day of the month, if one was given.
44    pub const fn day(&self) -> Option<u8> {
45        match self.day_or_week {
46            Some(DayOrWeek::Day(day)) => Some(day),
47            _ => None,
48        }
49    }
50
51    /// The week of the month, from `1` to `5`, if one was given.
52    pub const fn week(&self) -> Option<u8> {
53        match self.day_or_week {
54            Some(DayOrWeek::Week(week)) => Some(week),
55            _ => None,
56        }
57    }
58
59    /// Reads a FIX MonthYear such as `202612`, `20261218` or `202612w3`.
60    pub(crate) fn parse(text: &[u8]) -> Option<Self> {
61        let (year_month, rest) = text.split_at_checked(6)?;
62        let year = u16::try_from(number(&year_month[0..4])?).ok()?;
63        let month = u8::try_from(number(&year_month[4..6])?).ok()?;
64        match rest {
65            [] => Self::new(year, month),
66            [b'w', week] if week.is_ascii_digit() => Self::with_week(year, month, week - b'0'),
67            [_, _] => Self::with_day(year, month, u8::try_from(number(rest)?).ok()?),
68            _ => None,
69        }
70    }
71
72    const fn checked(year: u16, month: u8, day_or_week: Option<DayOrWeek>) -> Option<Self> {
73        // The month exists if its first day does.
74        let valid = match day_or_week {
75            None => Date::new(year, month, 1).is_some(),
76            Some(DayOrWeek::Day(day)) => Date::new(year, month, day).is_some(),
77            Some(DayOrWeek::Week(week)) => {
78                Date::new(year, month, 1).is_some() && matches!(week, 1..=5)
79            }
80        };
81        if valid {
82            Some(Self {
83                year,
84                month,
85                day_or_week,
86            })
87        } else {
88            None
89        }
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    fn parse(text: &str) -> Option<MonthYear> {
98        MonthYear::parse(text.as_bytes())
99    }
100
101    mod construction {
102        use super::*;
103
104        #[test]
105        fn keeps_its_parts() {
106            let month = MonthYear::new(2026, 12).unwrap();
107            assert_eq!((month.year(), month.month()), (2026, 12));
108            assert_eq!((month.day(), month.week()), (None, None));
109
110            let day = MonthYear::with_day(2026, 12, 18).unwrap();
111            assert_eq!((day.day(), day.week()), (Some(18), None));
112
113            let week = MonthYear::with_week(2026, 12, 3).unwrap();
114            assert_eq!((week.day(), week.week()), (None, Some(3)));
115        }
116
117        #[test]
118        fn rejects_months_that_do_not_exist() {
119            assert_eq!(MonthYear::new(2026, 0), None);
120            assert_eq!(MonthYear::new(2026, 13), None);
121            assert_eq!(MonthYear::new(10000, 1), None);
122        }
123
124        #[test]
125        fn checks_the_day_against_its_month() {
126            assert!(MonthYear::with_day(2024, 2, 29).is_some());
127            assert_eq!(MonthYear::with_day(2026, 2, 29), None);
128            assert_eq!(MonthYear::with_day(2026, 4, 31), None);
129            assert_eq!(MonthYear::with_day(2026, 12, 0), None);
130        }
131
132        #[test]
133        fn takes_weeks_one_to_five() {
134            assert!(MonthYear::with_week(2026, 12, 1).is_some());
135            assert!(MonthYear::with_week(2026, 12, 5).is_some());
136            assert_eq!(MonthYear::with_week(2026, 12, 0), None);
137            assert_eq!(MonthYear::with_week(2026, 12, 6), None);
138            assert_eq!(MonthYear::with_week(2026, 13, 1), None);
139        }
140    }
141
142    mod parsing {
143        use super::*;
144
145        #[test]
146        fn reads_a_month() {
147            assert_eq!(parse("202612"), MonthYear::new(2026, 12));
148        }
149
150        #[test]
151        fn reads_a_day() {
152            assert_eq!(parse("20261218"), MonthYear::with_day(2026, 12, 18));
153        }
154
155        #[test]
156        fn reads_a_week() {
157            assert_eq!(parse("202612w3"), MonthYear::with_week(2026, 12, 3));
158        }
159
160        #[test]
161        fn rejects_values_that_do_not_exist() {
162            for text in [
163                "202600", "202613", "20260231", "20261200", "202612w0", "202612w6",
164            ] {
165                assert_eq!(parse(text), None, "{text:?}");
166            }
167        }
168
169        #[test]
170        fn rejects_other_shapes() {
171            for text in [
172                "",
173                "2026",
174                "20261",
175                "2026121",
176                "202612181",
177                "2026-12",
178                "202612W3",
179                "202612w",
180                "202612w33",
181                "2026121a",
182                "+02612",
183                "202612 ",
184            ] {
185                assert_eq!(parse(text), None, "{text:?}");
186            }
187        }
188    }
189
190    mod equality {
191        use super::*;
192
193        #[test]
194        fn tells_the_forms_apart() {
195            assert_ne!(parse("202612"), parse("20261201"));
196            assert_ne!(parse("20261201"), parse("202612w1"));
197        }
198    }
199}