castep_cell_io/param/nmr/
magres_method.rs1use castep_cell_fmt::{Cell, CellValue, ToCell, ToCellValue};
2use castep_cell_fmt::parse::{FromCellValue, FromKeyValue};
3use castep_cell_fmt::{CResult, Error};
4use castep_cell_fmt::query::value_as_str;
5
6#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
15#[derive(Default)]
16pub enum MagresMethod {
17 #[default]
19 Crystal,
20 Molecular,
22}
23
24
25impl FromCellValue for MagresMethod {
26 fn from_cell_value(value: &CellValue<'_>) -> CResult<Self> {
27 match value_as_str(value)?.to_ascii_lowercase().as_str() {
28 "crystal" => Ok(Self::Crystal),
29 "molecular" => Ok(Self::Molecular),
30 other => Err(Error::Message(format!("unknown MagresMethod: {other}"))),
31 }
32 }
33}
34
35impl FromKeyValue for MagresMethod {
36 const KEY_NAME: &'static str = "MAGRES_METHOD";
37
38 fn from_cell_value_kv(value: &CellValue<'_>) -> CResult<Self> {
39 Self::from_cell_value(value)
40 }
41}
42
43impl ToCell for MagresMethod {
44 fn to_cell(&self) -> Cell<'_> {
45 Cell::KeyValue("MAGRES_METHOD", self.to_cell_value())
46 }
47}
48
49impl ToCellValue for MagresMethod {
50 fn to_cell_value(&self) -> CellValue<'_> {
51 CellValue::String(
52 match self {
53 MagresMethod::Crystal => "Crystal",
54 MagresMethod::Molecular => "Molecular",
55 }
56 .to_string(),
57 )
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64 use castep_cell_fmt::CellValue;
65
66 #[test]
67 fn test_case_insensitive() {
68 assert_eq!(MagresMethod::from_cell_value(&CellValue::Str("crystal")).unwrap(), MagresMethod::Crystal);
69 assert_eq!(MagresMethod::from_cell_value(&CellValue::Str("CRYSTAL")).unwrap(), MagresMethod::Crystal);
70 assert_eq!(MagresMethod::from_cell_value(&CellValue::Str("molecular")).unwrap(), MagresMethod::Molecular);
71 }
72
73 #[test]
74 fn test_invalid() {
75 assert!(MagresMethod::from_cell_value(&CellValue::Str("invalid")).is_err());
76 }
77
78 #[test]
79 fn test_key_name() {
80 assert_eq!(MagresMethod::KEY_NAME, "MAGRES_METHOD");
81 }
82}