ed_journals/modules/ship/models/ship_module/
ship_cockpit_module.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
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::ShipType;

/// Represents the cockpit module, which is different per ship type.
#[derive(Debug, Serialize, Clone, PartialEq)]
pub struct ShipCockpitModule(pub ShipType);

#[derive(Debug, Error)]
pub enum ShipCockpitModuleError {
    #[error("Failed to parse cockpit module")]
    FailedRegex,

    #[error("Unknown ship type")]
    UnknownShipType,
}

lazy_static! {
    static ref COCKPIT_MODULE_REGEX: Regex = Regex::new(r#"^(\$)?(.+)_cockpit(_name;)?$"#).unwrap();
}

impl FromStr for ShipCockpitModule {
    type Err = ShipCockpitModuleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let Some(captures) = COCKPIT_MODULE_REGEX.captures(s) else {
            return Err(ShipCockpitModuleError::FailedRegex);
        };

        let ship_type = captures
            .get(2)
            .expect("Should have already been matched")
            .as_str()
            .parse()
            .map_err(|_| ShipCockpitModuleError::UnknownShipType)?;

        Ok(ShipCockpitModule(ship_type))
    }
}

from_str_deserialize_impl!(ShipCockpitModule);