castep_param_io/param/general/
write_props.rs

1use std::fmt::Display;
2
3use derive_builder::Builder;
4use serde::{Deserialize, Serialize};
5
6use crate::param::KeywordDisplay;
7
8#[derive(
9    Debug,
10    Clone,
11    Copy,
12    PartialEq,
13    Eq,
14    PartialOrd,
15    Ord,
16    Hash,
17    Serialize,
18    Deserialize,
19    Default,
20    Builder,
21)]
22#[builder(setter(into, strip_option), default)]
23pub struct WriteProperties {
24    pub orbitals: Option<bool>,
25    pub formatted_elf: Option<bool>,
26    pub formatted_density: Option<bool>,
27    pub formatted_potential: Option<bool>,
28}
29
30impl WriteProperties {
31    pub fn orbitals(&self) -> Option<bool> {
32        self.orbitals
33    }
34
35    pub fn set_orbitals(&mut self, orbitals: Option<bool>) {
36        self.orbitals = orbitals;
37    }
38
39    pub fn formatted_elf(&self) -> Option<bool> {
40        self.formatted_elf
41    }
42
43    pub fn set_formatted_elf(&mut self, formatted_elf: Option<bool>) {
44        self.formatted_elf = formatted_elf;
45    }
46
47    pub fn formatted_density(&self) -> Option<bool> {
48        self.formatted_density
49    }
50
51    pub fn set_formatted_density(&mut self, formatted_density: Option<bool>) {
52        self.formatted_density = formatted_density;
53    }
54
55    pub fn formatted_potential(&self) -> Option<bool> {
56        self.formatted_potential
57    }
58
59    pub fn set_formatted_potential(&mut self, formatted_potential: Option<bool>) {
60        self.formatted_potential = formatted_potential;
61    }
62}
63
64impl Display for WriteProperties {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        let output = [
67            self.orbitals().map(|b| format!("WRITE_ORBITALS : {}", b)),
68            self.formatted_elf
69                .map(|b| format!("WRITE_FORMATTED_ELF : {}", b)),
70            self.formatted_density
71                .map(|b| format!("WRITE_FORMATTED_DENSITY : {}", b)),
72            self.formatted_potential
73                .map(|b| format!("WRITE_FORMATTED_POTENTIAL : {}", b)),
74        ]
75        .into_iter()
76        .flatten()
77        .collect::<Vec<String>>()
78        .join("\n");
79        write!(f, "{}", output)
80    }
81}
82
83impl KeywordDisplay for WriteProperties {
84    fn field(&self) -> String {
85        String::new()
86    }
87    fn output(&self) -> String {
88        self.to_string()
89    }
90}
91
92#[cfg(test)]
93mod test {
94    use super::WriteProperties;
95
96    #[test]
97    fn write_properties() {
98        let write_prop = WriteProperties::default();
99        assert_eq!("", write_prop.to_string());
100        let mut write_prop = WriteProperties::default();
101        write_prop.set_formatted_density(Some(true));
102        write_prop.set_formatted_elf(Some(true));
103        write_prop.set_formatted_potential(Some(false));
104        let target = r#"WRITE_FORMATTED_ELF : TRUE
105WRITE_FORMATTED_DENSITY : TRUE
106WRITE_FORMATTED_POTENTIAL : FALSE"#;
107        assert_eq!(target, write_prop.to_string());
108    }
109}