apple_clis/shared/identifiers/
generation.rs1use crate::prelude::*;
2
3use m_gen::MGen;
5use num_generation::NumGeneration;
7
8pub mod m_gen;
9pub mod num_generation;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
13pub enum Generation {
14 Num(NumGeneration),
15 M(MGen),
16}
17
18impl Generation {
19 #[cfg(test)]
20 pub(super) fn testing_num(num: NonZeroU8) -> Self {
21 Generation::Num(NumGeneration::testing_new(num))
22 }
23}
24
25impl From<NumGeneration> for Generation {
26 fn from(value: NumGeneration) -> Self {
27 Self::Num(value)
28 }
29}
30
31impl From<MGen> for Generation {
32 fn from(value: MGen) -> Self {
33 Self::M(value)
34 }
35}
36
37impl NomFromStr for Generation {
38 fn nom_from_str(input: &str) -> IResult<&str, Self> {
39 alt((
40 map(NumGeneration::nom_from_str, Generation::Num),
41 map(MGen::nom_from_str, Generation::M),
42 ))(input)
43 }
44}
45
46impl Display for Generation {
47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 match self {
49 Generation::Num(num) => write!(f, "{}", num),
50 Generation::M(m) => write!(f, "{}", m),
51 }
52 }
53}
54
55#[cfg(test)]
56mod test {
57 use self::shared::assert_nom_parses;
58
59 use super::*;
60
61 #[test]
62 fn generation_ordering() {
63 let lower = Generation::Num(NumGeneration::testing_new(1.try_into().unwrap()));
64 let middle = Generation::M(MGen::new_testing(NonZeroU8::new(1).unwrap()));
65 let higher = Generation::M(MGen::new_testing(NonZeroU8::new(3).unwrap()));
66
67 assert!(lower < middle);
68 assert!(middle < higher);
69 assert!(lower < higher);
70 }
71
72 #[test]
73 fn test_generation_hard_coded() {
74 let examples = ["(6th generation)", "(M2)"];
75
76 assert_nom_parses::<Generation>(examples, |_| true)
77 }
78}