Skip to main content

ed_journals/modules/exploration/models/
codex_organic_structure_entry.rs

1use crate::exploration::shared::codex_regex::CODEX_REGEX;
2use serde::Serialize;
3use std::fmt::{Display, Formatter};
4use std::str::FromStr;
5use thiserror::Error;
6
7/// Codex entries related to organic structures other than the ones already covered by
8/// [crate::exobiology::Genus], [crate::exobiology::Species] and [crate::exobiology::Variant].
9#[derive(Debug, Serialize, Clone, PartialEq, Eq, Hash)]
10#[cfg_attr(not(feature = "allow-unknown"), non_exhaustive)]
11pub enum CodexOrganicStructureEntry {
12    StolonTree,
13
14    #[cfg(feature = "allow-unknown")]
15    #[cfg_attr(docsrs, doc(cfg(feature = "allow-unknown")))]
16    Unknown(String),
17}
18
19impl CodexOrganicStructureEntry {
20    /// Whether the current variant is unknown.
21    #[cfg(feature = "allow-unknown")]
22    #[cfg_attr(docsrs, doc(cfg(feature = "allow-unknown")))]
23    pub fn is_unknown(&self) -> bool {
24        matches!(self, CodexOrganicStructureEntry::Unknown(_))
25    }
26}
27
28#[derive(Debug, Error)]
29pub enum CodexOrganicStructureError {
30    #[error("Failed to parse planet codex entry: '{0}'")]
31    FailedToParse(String),
32
33    #[error("Unknown planet codex entry: '{0}'")]
34    UnknownEntry(String),
35}
36
37impl FromStr for CodexOrganicStructureEntry {
38    type Err = CodexOrganicStructureError;
39
40    fn from_str(s: &str) -> Result<Self, Self::Err> {
41        let Some(captures) = CODEX_REGEX.captures(s) else {
42            return Err(CodexOrganicStructureError::FailedToParse(s.to_string()));
43        };
44
45        let string: &str = &captures
46            .get(1)
47            .expect("Should have been captured already")
48            .as_str()
49            .to_ascii_lowercase();
50
51        Ok(match string {
52            "l_seed_sdrt02_v3" => CodexOrganicStructureEntry::StolonTree,
53
54            #[cfg(feature = "allow-unknown")]
55            _ => CodexOrganicStructureEntry::Unknown(string.to_string()),
56
57            #[cfg(not(feature = "allow-unknown"))]
58            _ => return Err(CodexOrganicStructureError::UnknownEntry(string.to_string())),
59        })
60    }
61}
62
63impl Display for CodexOrganicStructureEntry {
64    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
65        write!(
66            f,
67            "{}",
68            match self {
69                CodexOrganicStructureEntry::StolonTree => "Stolon Tree",
70
71                #[cfg(feature = "allow-unknown")]
72                CodexOrganicStructureEntry::Unknown(unknown) =>
73                    return write!(f, "Unknown organic structure codex entry: {unknown}"),
74            }
75        )
76    }
77}