ed_journals/modules/ship/models/
ship_slot.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use std::fmt::{Display, Formatter};
use std::num::ParseIntError;
use std::str::FromStr;

use lazy_static::lazy_static;
use regex::Regex;
use serde::Serialize;
use thiserror::Error;

use crate::from_str_deserialize_impl;
use crate::modules::ship::models::ship_slot::core_slot::CoreSlot;
use crate::modules::ship::{HardpointSize, HardpointSizeError};

mod core_slot;

#[derive(Debug, Serialize, Clone, PartialEq)]
pub struct ShipSlot {
    pub slot_nr: u8,
    pub kind: ShipSlotKind,
}

// TODO kinda want to refactor this to use untagged variants
#[derive(Debug, Serialize, Clone, PartialEq)]
pub enum ShipSlotKind {
    ShipCockpit,
    CargoHatch,
    UtilityMount,
    Hardpoint(HardpointSize),
    OptionalInternal(u8),
    Military,
    CoreInternal(CoreSlot),
    DataLinkScanner,
    CodexScanner,
    DiscoveryScanner,

    // Cosmetic
    PaintJob,
    Decal,
    VesselVoice,
    Nameplate,
    IDPlate,
    Bobble,
    StringLights,
    EngineColor,
    WeaponColor,
    ShipKitSpoiler,
    ShipKitWings,
    ShipKitTail,
    ShipKitBumper,
}

#[derive(Debug, Error)]
pub enum ShipSlotError {
    #[error("Failed to parse slot number in: '{0}'")]
    FailedToParseSlotNr(String),

    #[error(transparent)]
    HardpointSizeParseError(#[from] HardpointSizeError),

    #[error("Failed to parse optional internal size: {0}")]
    OptionalInternalSizeParseError(#[source] ParseIntError),

    #[error("Failed to parse ship slot: '{0}'")]
    FailedToParse(String),
}

lazy_static! {
    static ref UTILITY_HARDPOINT_REGEX: Regex = Regex::new(r#"^TinyHardpoint(\d+)$"#).unwrap();
    static ref HARDPOINT_REGEX: Regex =
        Regex::new(r#"^(Small|Medium|Large|Huge)Hardpoint(\d+)$"#).unwrap();
    static ref OPTIONAL_INTERNAL_REGEX: Regex = Regex::new(r#"^Slot(\d+)_Size(\d+)$"#).unwrap();
    static ref MILITARY_REGEX: Regex = Regex::new(r#"^Military(\d+)$"#).unwrap();
    static ref DECAL_REGEX: Regex = Regex::new(r#"^Decal(\d+)$"#).unwrap();
    static ref NAMEPLATE_REGEX: Regex = Regex::new(r#"^ShipName(\d+)$"#).unwrap();
    static ref ID_PLATE_REGEX: Regex = Regex::new(r#"^ShipID(\d+)$"#).unwrap();
    static ref BOBBLE_REGEX: Regex = Regex::new(r#"^Bobble(\d+)$"#).unwrap();
}

impl FromStr for ShipSlot {
    type Err = ShipSlotError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let specific = match s {
            "ShipCockpit" => Some(ShipSlotKind::ShipCockpit),
            "CargoHatch" => Some(ShipSlotKind::CargoHatch),
            "PaintJob" => Some(ShipSlotKind::PaintJob),
            "VesselVoice" => Some(ShipSlotKind::VesselVoice),
            "DataLinkScanner" => Some(ShipSlotKind::DataLinkScanner),
            "CodexScanner" => Some(ShipSlotKind::CodexScanner),
            "DiscoveryScanner" => Some(ShipSlotKind::DiscoveryScanner),
            "EngineColour" => Some(ShipSlotKind::EngineColor),
            "WeaponColour" => Some(ShipSlotKind::WeaponColor),
            "StringLights" => Some(ShipSlotKind::StringLights),
            "ShipKitSpoiler" => Some(ShipSlotKind::ShipKitSpoiler),
            "ShipKitWings" => Some(ShipSlotKind::ShipKitWings),
            "ShipKitTail" => Some(ShipSlotKind::ShipKitTail),
            "ShipKitBumper" => Some(ShipSlotKind::ShipKitBumper),
            _ => None,
        };

        if let Some(kind) = specific {
            return Ok(ShipSlot { slot_nr: 0, kind });
        }

        if let Some(captures) = UTILITY_HARDPOINT_REGEX.captures(s) {
            let slot_nr = captures
                .get(1)
                .expect("Should have been captured already")
                .as_str()
                .parse()
                .map_err(|_| ShipSlotError::FailedToParseSlotNr(s.to_string()))?;

            return Ok(ShipSlot {
                slot_nr,
                kind: ShipSlotKind::UtilityMount,
            });
        }

        if let Some(captures) = HARDPOINT_REGEX.captures(s) {
            let size = captures
                .get(1)
                .expect("Should have been captured already")
                .as_str()
                .parse()?;

            let slot_nr = captures
                .get(2)
                .expect("Should have been captured already")
                .as_str()
                .parse()
                .map_err(|_| ShipSlotError::FailedToParseSlotNr(s.to_string()))?;

            return Ok(ShipSlot {
                slot_nr,
                kind: ShipSlotKind::Hardpoint(size),
            });
        }

        if let Some(captures) = OPTIONAL_INTERNAL_REGEX.captures(s) {
            let slot_nr = captures
                .get(1)
                .expect("Should have been captured already")
                .as_str()
                .parse()
                .map_err(|_| ShipSlotError::FailedToParseSlotNr(s.to_string()))?;

            let size = captures
                .get(2)
                .expect("Should have been captured already")
                .as_str()
                .parse()
                .map_err(ShipSlotError::OptionalInternalSizeParseError)?;

            return Ok(ShipSlot {
                slot_nr,
                kind: ShipSlotKind::OptionalInternal(size),
            });
        }

        if let Some(captures) = MILITARY_REGEX.captures(s) {
            let slot_nr = captures
                .get(1)
                .expect("Should have been captured already")
                .as_str()
                .parse()
                .map_err(|_| ShipSlotError::FailedToParseSlotNr(s.to_string()))?;

            return Ok(ShipSlot {
                slot_nr,
                kind: ShipSlotKind::Military,
            });
        }

        if let Some(captures) = DECAL_REGEX.captures(s) {
            let slot_nr = captures
                .get(1)
                .expect("Should have been captured already")
                .as_str()
                .parse()
                .map_err(|_| ShipSlotError::FailedToParseSlotNr(s.to_string()))?;

            return Ok(ShipSlot {
                slot_nr,
                kind: ShipSlotKind::Decal,
            });
        }

        if let Some(captures) = NAMEPLATE_REGEX.captures(s) {
            let slot_nr = captures
                .get(1)
                .expect("Should have been captured already")
                .as_str()
                .parse()
                .map_err(|_| ShipSlotError::FailedToParseSlotNr(s.to_string()))?;

            return Ok(ShipSlot {
                slot_nr,
                kind: ShipSlotKind::Nameplate,
            });
        }

        if let Some(captures) = ID_PLATE_REGEX.captures(s) {
            let slot_nr = captures
                .get(1)
                .expect("Should have been captured already")
                .as_str()
                .parse()
                .map_err(|_| ShipSlotError::FailedToParseSlotNr(s.to_string()))?;

            return Ok(ShipSlot {
                slot_nr,
                kind: ShipSlotKind::IDPlate,
            });
        }

        if let Some(captures) = BOBBLE_REGEX.captures(s) {
            let slot_nr = captures
                .get(1)
                .expect("Should have been captured already")
                .as_str()
                .parse()
                .map_err(|_| ShipSlotError::FailedToParseSlotNr(s.to_string()))?;

            return Ok(ShipSlot {
                slot_nr,
                kind: ShipSlotKind::Bobble,
            });
        }

        if let Ok(core_slot) = s.parse() {
            return Ok(ShipSlot {
                slot_nr: 1,
                kind: ShipSlotKind::CoreInternal(core_slot),
            });
        }

        Err(ShipSlotError::FailedToParse(s.to_string()))
    }
}

from_str_deserialize_impl!(ShipSlot);

impl Display for ShipSlot {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            ShipSlotKind::ShipCockpit => write!(f, "Ship Cockpit"),
            ShipSlotKind::CargoHatch => write!(f, "Cargo Hatch"),
            ShipSlotKind::UtilityMount => write!(f, "Utility Mount"),
            ShipSlotKind::Hardpoint(size) => write!(f, "{} Hardpoint", size.size_str()),
            ShipSlotKind::OptionalInternal(size) => write!(f, "Size {} Optional Internal", size),
            ShipSlotKind::Military => write!(f, "Military Slot"),
            ShipSlotKind::CoreInternal(core_slot) => write!(f, "{} Core Internal", core_slot),
            ShipSlotKind::DataLinkScanner => write!(f, "Data Link Scanner"),
            ShipSlotKind::CodexScanner => write!(f, "Codex Scanner"),
            ShipSlotKind::DiscoveryScanner => write!(f, "Discovery Scanner"),

            // Cosmetic
            ShipSlotKind::PaintJob => write!(f, "Paint job"),
            ShipSlotKind::Decal => write!(f, "Decal"),
            ShipSlotKind::VesselVoice => write!(f, "COVAS Voice"),
            ShipSlotKind::Nameplate => write!(f, "Nameplate"),
            ShipSlotKind::IDPlate => write!(f, "ID-Plate"),
            ShipSlotKind::Bobble => write!(f, "Bobble"),
            ShipSlotKind::StringLights => write!(f, "String Lights"),
            ShipSlotKind::EngineColor => write!(f, "Engine Colour"),
            ShipSlotKind::WeaponColor => write!(f, "Weapon Colour"),
            ShipSlotKind::ShipKitSpoiler => write!(f, "Ship Kit Spoiler"),
            ShipSlotKind::ShipKitWings => write!(f, "Ship Kit Wing"),
            ShipSlotKind::ShipKitTail => write!(f, "Ship Kit Tail"),
            ShipSlotKind::ShipKitBumper => write!(f, "Ship Kit Bumper"),
        }
    }
}