ed_journals/modules/ship/models/ship_module/
module_class.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::fmt::{Display, Formatter};
use std::str::FromStr;

use serde::Serialize;
use thiserror::Error;

use crate::modules::ship::{ArmorGrade, ArmorModule};

/// The class of a module. Not all classes are available for every module.
#[derive(Debug, Serialize, Clone, PartialEq)]
pub enum ModuleClass {
    A,
    B,
    C,
    D,
    E,
    F,
    G,
    H,
    I,

    #[cfg(feature = "allow-unknown")]
    #[cfg_attr(docsrs, doc(cfg(feature = "allow-unknown")))]
    Unknown,
}

#[derive(Debug, Error)]
pub enum ModuleClassError {
    #[error("Unknown module class: {0}")]
    UnknownModuleClass(u8),

    #[error("Unknown module class string: '{0}'")]
    UnknownModuleClassString(String),
}

impl TryFrom<u8> for ModuleClass {
    type Error = ModuleClassError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            5 => Ok(ModuleClass::A),
            4 => Ok(ModuleClass::B),
            3 => Ok(ModuleClass::C),
            2 => Ok(ModuleClass::D),
            1 => Ok(ModuleClass::E),

            #[cfg(feature = "allow-unknown")]
            _ => Ok(ModuleClass::Unknown),

            #[cfg(not(feature = "allow-unknown"))]
            _ => Err(ModuleClassError::UnknownModuleClass(value)),
        }
    }
}

impl FromStr for ModuleClass {
    type Err = ModuleClassError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "A" | "a" => Ok(ModuleClass::A),
            "B" | "b" => Ok(ModuleClass::B),
            "C" | "c" => Ok(ModuleClass::C),
            "D" | "d" => Ok(ModuleClass::D),
            "E" | "e" => Ok(ModuleClass::E),
            "F" | "f" => Ok(ModuleClass::F),
            "G" | "g" => Ok(ModuleClass::G),
            "H" | "h" => Ok(ModuleClass::H),
            "I" | "i" => Ok(ModuleClass::I),

            #[cfg(feature = "allow-unknown")]
            _ => Ok(ModuleClass::Unknown),

            #[cfg(not(feature = "allow-unknown"))]
            _ => Err(ModuleClassError::UnknownModuleClassString(s.to_string())),
        }
    }
}

impl Display for ModuleClass {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                ModuleClass::A => "A",
                ModuleClass::B => "B",
                ModuleClass::C => "C",
                ModuleClass::D => "D",
                ModuleClass::E => "E",
                ModuleClass::F => "F",
                ModuleClass::G => "G",
                ModuleClass::H => "H",
                ModuleClass::I => "I",

                #[cfg(feature = "allow-unknown")]
                ModuleClass::Unknown => "U",
            }
        )
    }
}

impl From<&ArmorGrade> for ModuleClass {
    fn from(value: &ArmorGrade) -> Self {
        match value {
            ArmorGrade::LightweightAlloy => ModuleClass::C,
            ArmorGrade::ReinforcedAlloy => ModuleClass::B,
            ArmorGrade::MilitaryGradeComposite => ModuleClass::A,
            ArmorGrade::MirroredSurfaceComposite => ModuleClass::A,
            ArmorGrade::ReactiveSurfaceComposite => ModuleClass::A,

            #[cfg(feature = "allow-unknown")]
            ArmorGrade::Unknown(_) => ModuleClass::Unknown,

            #[cfg(feature = "allow-unknown")]
            ArmorGrade::UnknownString(_) => ModuleClass::Unknown,
        }
    }
}

impl From<ArmorGrade> for ModuleClass {
    fn from(value: ArmorGrade) -> Self {
        (&value).into()
    }
}

impl From<&ArmorModule> for ModuleClass {
    fn from(value: &ArmorModule) -> Self {
        (&value.grade).into()
    }
}

impl From<ArmorModule> for ModuleClass {
    fn from(value: ArmorModule) -> Self {
        value.grade.into()
    }
}