Skip to main content

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

1use std::str::FromStr;
2
3use lazy_static::lazy_static;
4use regex::Regex;
5use serde::Serialize;
6use thiserror::Error;
7
8use crate::from_str_deserialize_impl;
9
10#[derive(Debug, Serialize, Clone, PartialEq)]
11pub struct ShipPaintJob {
12    pub name: String,
13}
14
15#[derive(Debug, Error)]
16pub enum ShipPaintJobError {
17    #[error("Failed to parse paint job: '{0}'")]
18    FailedToParse(String),
19}
20
21lazy_static! {
22    static ref PAINTJOB_REGEX: Regex = Regex::new(r#"^paintjob_(\w+)$"#).unwrap();
23}
24
25impl FromStr for ShipPaintJob {
26    type Err = ShipPaintJobError;
27
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        let Some(captures) = PAINTJOB_REGEX.captures(s) else {
30            return Err(ShipPaintJobError::FailedToParse(s.to_string()));
31        };
32
33        Ok(ShipPaintJob {
34            name: captures
35                .get(1)
36                .expect("Should have been captured already")
37                .as_str()
38                .to_string(),
39        })
40    }
41}
42
43from_str_deserialize_impl!(ShipPaintJob);