Skip to main content

castep_cell_io/param/general/
data_distribution.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 parallelization strategy used.
7///
8/// Keyword type: String
9///
10/// Default: Default
11///
12/// Example:
13/// DATA_DISTRIBUTION : Gvector
14#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
15pub enum DataDistribution {
16    KPoint,
17    GVector,
18    Mixed,
19    Default,
20}
21
22impl FromCellValue for DataDistribution {
23    fn from_cell_value(value: &CellValue<'_>) -> CResult<Self> {
24        match value_as_str(value)?.to_ascii_lowercase().as_str() {
25            "kpoint" => Ok(Self::KPoint),
26            "gvector" => Ok(Self::GVector),
27            "mixed" => Ok(Self::Mixed),
28            "default" => Ok(Self::Default),
29            other => Err(Error::Message(format!("unknown DataDistribution: {other}"))),
30        }
31    }
32}
33
34impl FromKeyValue for DataDistribution {
35    const KEY_NAME: &'static str = "DATA_DISTRIBUTION";
36
37    fn from_cell_value_kv(value: &CellValue<'_>) -> CResult<Self> {
38        Self::from_cell_value(value)
39    }
40}
41
42impl ToCell for DataDistribution {
43    fn to_cell(&self) -> Cell<'_> {
44        Cell::KeyValue("DATA_DISTRIBUTION", self.to_cell_value())
45    }
46}
47
48impl ToCellValue for DataDistribution {
49    fn to_cell_value(&self) -> CellValue<'_> {
50        CellValue::String(
51            match self {
52                DataDistribution::KPoint => "Kpoint",
53                DataDistribution::GVector => "Gvector",
54                DataDistribution::Mixed => "Mixed",
55                DataDistribution::Default => "Default",
56            }
57            .to_string(),
58        )
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65    use castep_cell_fmt::CellValue;
66
67    #[test]
68    fn test_case_insensitive() {
69        assert_eq!(DataDistribution::from_cell_value(&CellValue::Str("kpoint")).unwrap(), DataDistribution::KPoint);
70        assert_eq!(DataDistribution::from_cell_value(&CellValue::Str("KPOINT")).unwrap(), DataDistribution::KPoint);
71        assert_eq!(DataDistribution::from_cell_value(&CellValue::Str("gvector")).unwrap(), DataDistribution::GVector);
72        assert_eq!(DataDistribution::from_cell_value(&CellValue::Str("GVECTOR")).unwrap(), DataDistribution::GVector);
73    }
74
75    #[test]
76    fn test_all_variants() {
77        assert_eq!(DataDistribution::from_cell_value(&CellValue::Str("mixed")).unwrap(), DataDistribution::Mixed);
78        assert_eq!(DataDistribution::from_cell_value(&CellValue::Str("default")).unwrap(), DataDistribution::Default);
79    }
80
81    #[test]
82    fn test_invalid() {
83        assert!(DataDistribution::from_cell_value(&CellValue::Str("invalid")).is_err());
84    }
85
86    #[test]
87    fn test_key_name() {
88        assert_eq!(DataDistribution::KEY_NAME, "DATA_DISTRIBUTION");
89    }
90}
91