brk_types/
semesterindex.rs1use std::{
2 fmt::Debug,
3 ops::{Add, AddAssign, Div},
4};
5
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use vecdb::{CheckedSub, Formattable, Pco, PrintableIndex};
9
10use super::MonthIndex;
11
12#[derive(
13 Debug,
14 Clone,
15 Copy,
16 PartialEq,
17 Eq,
18 PartialOrd,
19 Ord,
20 Default,
21 Serialize,
22 Deserialize,
23 Pco,
24 JsonSchema,
25)]
26pub struct SemesterIndex(u16);
27
28impl From<u16> for SemesterIndex {
29 #[inline]
30 fn from(value: u16) -> Self {
31 Self(value)
32 }
33}
34
35impl From<usize> for SemesterIndex {
36 #[inline]
37 fn from(value: usize) -> Self {
38 Self(value as u16)
39 }
40}
41
42impl From<SemesterIndex> for u16 {
43 #[inline]
44 fn from(value: SemesterIndex) -> Self {
45 value.0
46 }
47}
48
49impl From<SemesterIndex> for usize {
50 #[inline]
51 fn from(value: SemesterIndex) -> Self {
52 value.0 as usize
53 }
54}
55
56impl Add<usize> for SemesterIndex {
57 type Output = Self;
58
59 fn add(self, rhs: usize) -> Self::Output {
60 Self::from(self.0 + rhs as u16)
61 }
62}
63
64impl Add<SemesterIndex> for SemesterIndex {
65 type Output = Self;
66
67 fn add(self, rhs: Self) -> Self::Output {
68 Self::from(self.0 + rhs.0)
69 }
70}
71
72impl AddAssign for SemesterIndex {
73 fn add_assign(&mut self, rhs: Self) {
74 *self = Self(self.0 + rhs.0)
75 }
76}
77
78impl Div<usize> for SemesterIndex {
79 type Output = Self;
80 fn div(self, _: usize) -> Self::Output {
81 unreachable!()
82 }
83}
84
85impl From<MonthIndex> for SemesterIndex {
86 #[inline]
87 fn from(value: MonthIndex) -> Self {
88 Self((usize::from(value) / 6) as u16)
89 }
90}
91
92impl CheckedSub for SemesterIndex {
93 fn checked_sub(self, rhs: Self) -> Option<Self> {
94 self.0.checked_sub(rhs.0).map(Self)
95 }
96}
97
98impl PrintableIndex for SemesterIndex {
99 fn to_string() -> &'static str {
100 "semesterindex"
101 }
102
103 fn to_possible_strings() -> &'static [&'static str] {
104 &["s", "semester", "semesterindex"]
105 }
106}
107
108impl std::fmt::Display for SemesterIndex {
109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110 let mut buf = itoa::Buffer::new();
111 let str = buf.format(self.0);
112 f.write_str(str)
113 }
114}
115
116impl Formattable for SemesterIndex {
117 #[inline(always)]
118 fn may_need_escaping() -> bool {
119 false
120 }
121}