brk_structs/structs/
monthindex.rs

1use std::{
2    fmt::Debug,
3    ops::{Add, AddAssign, Div},
4};
5
6use vecdb::{CheckedSub, Printable, StoredCompressed};
7use serde::{Deserialize, Serialize};
8use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
9
10use super::{Date, DateIndex, YearIndex};
11
12#[derive(
13    Debug,
14    Clone,
15    Copy,
16    PartialEq,
17    Eq,
18    PartialOrd,
19    Ord,
20    Default,
21    Serialize,
22    Deserialize,
23    FromBytes,
24    Immutable,
25    IntoBytes,
26    KnownLayout,
27    StoredCompressed,
28)]
29pub struct MonthIndex(u16);
30
31impl From<u16> for MonthIndex {
32    fn from(value: u16) -> Self {
33        Self(value)
34    }
35}
36
37impl From<MonthIndex> for u16 {
38    fn from(value: MonthIndex) -> Self {
39        value.0
40    }
41}
42
43impl From<usize> for MonthIndex {
44    fn from(value: usize) -> Self {
45        Self(value as u16)
46    }
47}
48
49impl From<MonthIndex> for u64 {
50    fn from(value: MonthIndex) -> Self {
51        value.0 as u64
52    }
53}
54
55impl From<MonthIndex> for usize {
56    fn from(value: MonthIndex) -> Self {
57        value.0 as usize
58    }
59}
60
61impl Add<usize> for MonthIndex {
62    type Output = Self;
63
64    fn add(self, rhs: usize) -> Self::Output {
65        Self::from(self.0 + rhs as u16)
66    }
67}
68
69impl Add<MonthIndex> for MonthIndex {
70    type Output = Self;
71
72    fn add(self, rhs: Self) -> Self::Output {
73        Self::from(self.0 + rhs.0)
74    }
75}
76
77impl AddAssign for MonthIndex {
78    fn add_assign(&mut self, rhs: Self) {
79        *self = Self(self.0 + rhs.0)
80    }
81}
82
83impl Div<usize> for MonthIndex {
84    type Output = Self;
85    fn div(self, _: usize) -> Self::Output {
86        unreachable!()
87    }
88}
89
90impl From<DateIndex> for MonthIndex {
91    fn from(value: DateIndex) -> Self {
92        Self::from(Date::from(value))
93    }
94}
95
96impl From<Date> for MonthIndex {
97    fn from(value: Date) -> Self {
98        Self(u16::from(YearIndex::from(value)) * 12 + value.month() as u16 - 1)
99    }
100}
101
102impl CheckedSub for MonthIndex {
103    fn checked_sub(self, rhs: Self) -> Option<Self> {
104        self.0.checked_sub(rhs.0).map(Self)
105    }
106}
107
108impl Printable for MonthIndex {
109    fn to_string() -> &'static str {
110        "monthindex"
111    }
112
113    fn to_possible_strings() -> &'static [&'static str] {
114        &["m", "month", "monthindex"]
115    }
116}