1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::fmt::Display;

use crate::CellParseError;

use super::ParsableUnit;

#[derive(Debug, Default, Clone, Copy)]
pub enum LengthUnit {
    Bohr,
    Meter,
    Centimeter,
    Nanometer,
    #[default]
    Ang,
}

impl Display for LengthUnit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LengthUnit::Bohr => f.write_str("bohr"),
            LengthUnit::Meter => f.write_str("m"),
            LengthUnit::Centimeter => f.write_str("cm"),
            LengthUnit::Nanometer => f.write_str("nm"),
            LengthUnit::Ang => f.write_str("ang"),
        }
    }
}

impl ParsableUnit for LengthUnit {
    fn parse_from_str(input: &str) -> Result<Self, CellParseError> {
        match input {
            "bohr" => Ok(LengthUnit::Bohr),
            "a0" => Ok(LengthUnit::Bohr),
            "m" => Ok(LengthUnit::Meter),
            "cm" => Ok(LengthUnit::Centimeter),
            "nm" => Ok(LengthUnit::Nanometer),
            "ang" => Ok(LengthUnit::Ang),
            _ => Err(CellParseError::Invalid),
        }
    }
}

#[derive(Debug, Default, Clone, Copy)]
pub enum InvLengthUnit {
    Bohr,
    Meter,
    Nanometer,
    #[default]
    Ang,
}

impl Display for InvLengthUnit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            InvLengthUnit::Bohr => f.write_str("1/"),
            InvLengthUnit::Meter => f.write_str("1/m"),
            InvLengthUnit::Nanometer => f.write_str("1/nm"),
            InvLengthUnit::Ang => f.write_str("1/ang"),
        }
    }
}

impl ParsableUnit for InvLengthUnit {
    fn parse_from_str(input: &str) -> Result<Self, CellParseError> {
        match input {
            "1/" => Ok(InvLengthUnit::Bohr),
            "1/m" => Ok(InvLengthUnit::Meter),
            "1/nm" => Ok(InvLengthUnit::Nanometer),
            "1/ang" => Ok(InvLengthUnit::Ang),
            _ => Err(CellParseError::Invalid),
        }
    }
}