Skip to main content

ed_journals/modules/ship/models/ship_module/
ship_string_lights.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 ShipStringLights {
12    pub name: String,
13}
14
15#[derive(Debug, Error)]
16pub enum ShipStringLightsError {
17    #[error("Failed to parse string lights: '{0}'")]
18    FailedToParse(String),
19}
20
21lazy_static! {
22    static ref STRING_LIGHTS_COLOR_REGEX: Regex = Regex::new(r#"^string_lights_(\w+)$"#).unwrap();
23}
24
25impl FromStr for ShipStringLights {
26    type Err = ShipStringLightsError;
27
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        let Some(captures) = STRING_LIGHTS_COLOR_REGEX.captures(s) else {
30            return Err(ShipStringLightsError::FailedToParse(s.to_string()));
31        };
32
33        Ok(ShipStringLights {
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!(ShipStringLights);