Skip to main content

castep_cell_io/param/general/
num_backup_iter.rs

1use castep_cell_fmt::{Cell, CellValue, ToCell, ToCellValue, CResult};
2use castep_cell_fmt::parse::FromKeyValue;
3use castep_cell_fmt::query::value_as_i32;
4
5/// Specifies the number of geometry optimization or molecular dynamics
6/// iterations between updates of the backup restart files.
7///
8/// Keyword type: Integer
9///
10/// Default: 5
11///
12/// Example:
13/// NUM_BACKUP_ITER : 2
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub struct NumBackupIter(pub i32); // i32 to allow negative values, though spec says > 0
16
17impl Default for NumBackupIter {
18    fn default() -> Self {
19        Self(5) // Default is 5
20    }
21}
22
23impl FromKeyValue for NumBackupIter {
24    const KEY_NAME: &'static str = "NUM_BACKUP_ITER";
25
26    fn from_cell_value_kv(value: &CellValue<'_>) -> CResult<Self> {
27        Ok(Self(value_as_i32(value)?))
28    }
29}
30
31impl ToCell for NumBackupIter {
32    fn to_cell(&self) -> Cell<'_> {
33        Cell::KeyValue("NUM_BACKUP_ITER", CellValue::Int(self.0))
34    }
35}
36
37impl ToCellValue for NumBackupIter {
38    fn to_cell_value(&self) -> CellValue<'_> {
39        CellValue::Int(self.0)
40    }
41}
42
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_from_cell_value() {
50        let val = CellValue::Int(2);
51        let result = NumBackupIter::from_cell_value_kv(&val).unwrap();
52        assert_eq!(result.0, 2);
53    }
54
55    #[test]
56    fn test_key_name() {
57        assert_eq!(NumBackupIter::KEY_NAME, "NUM_BACKUP_ITER");
58    }
59}
60