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