ed_journals/modules/ship/models/ship_module/
ship_voicepack.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
use std::str::FromStr;

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

use crate::from_str_deserialize_impl;

#[derive(Debug, Serialize, Clone, PartialEq)]
pub struct ShipVoicepack {
    pub name: String,
}

#[derive(Debug, Error)]
pub enum ShipVoicepackError {
    #[error("Failed to parse voicepack: '{0}'")]
    FailedToParse(String),
}

lazy_static! {
    static ref VOICEPACK_REGEX: Regex = Regex::new(r#"^voicepack_(\w+)$"#).unwrap();
}

impl FromStr for ShipVoicepack {
    type Err = ShipVoicepackError;

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

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

        Ok(ShipVoicepack { name })
    }
}

from_str_deserialize_impl!(ShipVoicepack);