Skip to main content

ed_journals/modules/ship/models/ship_module/
ship_voicepack.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 ShipVoicepack {
12    pub name: String,
13}
14
15#[derive(Debug, Error)]
16pub enum ShipVoicepackError {
17    #[error("Failed to parse voicepack: '{0}'")]
18    FailedToParse(String),
19}
20
21lazy_static! {
22    static ref VOICEPACK_REGEX: Regex = Regex::new(r#"^voicepack_(\w+)$"#).unwrap();
23}
24
25impl FromStr for ShipVoicepack {
26    type Err = ShipVoicepackError;
27
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        let Some(captures) = VOICEPACK_REGEX.captures(s) else {
30            return Err(ShipVoicepackError::FailedToParse(s.to_string()));
31        };
32
33        let name = captures
34            .get(1)
35            .expect("Should have been captured already")
36            .as_str()
37            .to_string();
38
39        Ok(ShipVoicepack { name })
40    }
41}
42
43from_str_deserialize_impl!(ShipVoicepack);