Skip to main content

castep_cell_io/param/general/
opt_strategy.rs

1use castep_cell_fmt::{Cell, CellValue, ToCell, ToCellValue, CResult};
2use castep_cell_fmt::parse::{FromCellValue, FromKeyValue};
3use castep_cell_fmt::query::value_as_str;
4use castep_cell_fmt::Error;
5
6/// Determines the optimization strategy used when there are multiple strategies
7/// available for the selected algorithm.
8///
9/// Keyword type: String
10///
11/// Default: OptStrategy::Default
12///
13/// Example:
14/// OPT_STRATEGY : Memory
15#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
16pub enum OptStrategy {
17    /// Maximizes performance at the cost of additional memory usage.
18    Speed,
19    /// Balances performance and memory usage.
20    #[default]
21    Default,
22    /// Minimizes memory usage at a cost of decreased performance.
23    Memory,
24}
25
26impl FromCellValue for OptStrategy {
27    fn from_cell_value(value: &CellValue<'_>) -> CResult<Self> {
28        match value_as_str(value)?.to_ascii_lowercase().as_str() {
29            "speed" => Ok(Self::Speed),
30            "default" => Ok(Self::Default),
31            "memory" => Ok(Self::Memory),
32            other => Err(Error::Message(format!("unknown OptStrategy: {other}"))),
33        }
34    }
35}
36
37impl FromKeyValue for OptStrategy {
38    const KEY_NAME: &'static str = "OPT_STRATEGY";
39
40    fn from_cell_value_kv(value: &CellValue<'_>) -> CResult<Self> {
41        Self::from_cell_value(value)
42    }
43}
44
45impl ToCell for OptStrategy {
46    fn to_cell(&self) -> Cell<'_> {
47        Cell::KeyValue("OPT_STRATEGY", self.to_cell_value())
48    }
49}
50
51impl ToCellValue for OptStrategy {
52    fn to_cell_value(&self) -> CellValue<'_> {
53        CellValue::String(
54            match self {
55                OptStrategy::Speed => "Speed",
56                OptStrategy::Default => "Default",
57                OptStrategy::Memory => "Memory",
58            }
59            .to_string(),
60        )
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use castep_cell_fmt::CellValue;
68
69    #[test]
70    fn test_case_insensitive() {
71        assert_eq!(OptStrategy::from_cell_value(&CellValue::Str("speed")).unwrap(), OptStrategy::Speed);
72        assert_eq!(OptStrategy::from_cell_value(&CellValue::Str("SPEED")).unwrap(), OptStrategy::Speed);
73        assert_eq!(OptStrategy::from_cell_value(&CellValue::Str("memory")).unwrap(), OptStrategy::Memory);
74        assert_eq!(OptStrategy::from_cell_value(&CellValue::Str("MEMORY")).unwrap(), OptStrategy::Memory);
75    }
76
77    #[test]
78    fn test_all_variants() {
79        assert_eq!(OptStrategy::from_cell_value(&CellValue::Str("default")).unwrap(), OptStrategy::Default);
80    }
81
82    #[test]
83    fn test_invalid() {
84        assert!(OptStrategy::from_cell_value(&CellValue::Str("invalid")).is_err());
85    }
86
87    #[test]
88    fn test_key_name() {
89        assert_eq!(OptStrategy::KEY_NAME, "OPT_STRATEGY");
90    }
91}
92