ed_journals/modules/exploration/models/
codex_organic_structure_entry.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::exploration::shared::codex_regex::CODEX_REGEX;
use serde::Serialize;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use thiserror::Error;

/// Codex entries related to organic structures other than the ones already covered by
/// [crate::exobiology::Genus], [crate::exobiology::Species] and [crate::exobiology::Variant].
#[derive(Debug, Serialize, Clone, PartialEq, Eq, Hash)]
pub enum CodexOrganicStructureEntry {
    StolonTree,

    #[cfg(feature = "allow-unknown")]
    #[cfg_attr(docsrs, doc(cfg(feature = "allow-unknown")))]
    Unknown(String),
}

impl CodexOrganicStructureEntry {
    /// Whether the current variant is unknown.
    #[cfg(feature = "allow-unknown")]
    #[cfg_attr(docsrs, doc(cfg(feature = "allow-unknown")))]
    pub fn is_unknown(&self) -> bool {
        matches!(self, CodexOrganicStructureEntry::Unknown(_))
    }
}

#[derive(Debug, Error)]
pub enum CodexOrganicStructureError {
    #[error("Failed to parse planet codex entry: '{0}'")]
    FailedToParse(String),

    #[error("Unknown planet codex entry: '{0}'")]
    UnknownEntry(String),
}

impl FromStr for CodexOrganicStructureEntry {
    type Err = CodexOrganicStructureError;

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

        let string: &str = &captures
            .get(1)
            .expect("Should have been captured already")
            .as_str()
            .to_ascii_lowercase();

        Ok(match string {
            "l_seed_sdrt02_v3" => CodexOrganicStructureEntry::StolonTree,

            #[cfg(feature = "allow-unknown")]
            _ => CodexOrganicStructureEntry::Unknown(string.to_string()),

            #[cfg(not(feature = "allow-unknown"))]
            _ => return Err(CodexOrganicStructureError::UnknownEntry(string.to_string())),
        })
    }
}

impl Display for CodexOrganicStructureEntry {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                CodexOrganicStructureEntry::StolonTree => "Stolon Tree",

                #[cfg(feature = "allow-unknown")]
                CodexOrganicStructureEntry::Unknown(unknown) =>
                    return write!(f, "Unknown organic structure codex entry: {}", unknown),
            }
        )
    }
}