ed_journals/modules/ship/models/ship_module/
ship_kit_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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::{ShipType, ShipTypeError};

#[derive(Debug, Serialize, Clone, PartialEq)]
pub struct ShipKitModule {
    pub ship: ShipType,
    pub name: String,
    pub piece: String,
}

#[derive(Debug, Error)]
pub enum ShipKitModuleError {
    #[error("Failed to parse ship kit number: {0}")]
    FailedToParseShipKitNr(#[from] ParseIntError),

    #[error(transparent)]
    ShipTypeError(#[from] ShipTypeError),

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

lazy_static! {
    static ref SHIP_KIT_MODULE_REGEX: Regex =
        Regex::new(r#"^([a-z0-9_]+?)_shipkit([a-z0-9]+)_([a-z0-9]+)$"#).unwrap();
}

impl FromStr for ShipKitModule {
    type Err = ShipKitModuleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let Some(captures) = SHIP_KIT_MODULE_REGEX.captures(s) else {
            return Err(ShipKitModuleError::FailedToParse(s.to_string()));
        };

        let ship = captures
            .get(1)
            .expect("Should have been captured already")
            .as_str()
            .parse()?;

        let name = captures
            .get(2)
            .expect("Should have been captured already")
            .as_str()
            .to_string();

        let piece = captures
            .get(3)
            .expect("Should have been captured already")
            .as_str()
            .to_string();

        Ok(ShipKitModule { ship, name, piece })
    }
}

from_str_deserialize_impl!(ShipKitModule);