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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// [[file:../gchemol-core.note::*imports][imports:1]]
use serde::*;
// imports:1 ends here

// [[file:../gchemol-core.note::cab264f1][cab264f1]]
const ELEMENT_DATA: [(&str, &str); 118] = [
    ("H", "Hydrogen"),
    ("He", "Helium"),
    ("Li", "Lithium"),
    ("Be", "Beryllium"),
    ("B", "Boron"),
    ("C", "Carbon"),
    ("N", "Nitrogen"),
    ("O", "Oxygen"),
    ("F", "Fluorine"),
    ("Ne", "Neon"),
    ("Na", "Sodium"),
    ("Mg", "Magnesium"),
    ("Al", "Aluminum"),
    ("Si", "Silicon"),
    ("P", "Phosphorus"),
    ("S", "Sulfur"),
    ("Cl", "Chlorine"),
    ("Ar", "Argon"),
    ("K", "Potassium"),
    ("Ca", "Calcium"),
    ("Sc", "Scandium"),
    ("Ti", "Titanium"),
    ("V", "Vanadium"),
    ("Cr", "Chromium"),
    ("Mn", "Manganese"),
    ("Fe", "Iron"),
    ("Co", "Cobalt"),
    ("Ni", "Nickel"),
    ("Cu", "Copper"),
    ("Zn", "Zinc"),
    ("Ga", "Gallium"),
    ("Ge", "Germanium"),
    ("As", "Arsenic"),
    ("Se", "Selenium"),
    ("Br", "Bromine"),
    ("Kr", "Krypton"),
    ("Rb", "Rubidium"),
    ("Sr", "Strontium"),
    ("Y", "Yttrium"),
    ("Zr", "Zirconium"),
    ("Nb", "Niobium"),
    ("Mo", "Molybdenum"),
    ("Tc", "Technetium"),
    ("Ru", "Ruthenium"),
    ("Rh", "Rhodium"),
    ("Pd", "Palladium"),
    ("Ag", "Silver"),
    ("Cd", "Cadmium"),
    ("In", "Indium"),
    ("Sn", "Tin"),
    ("Sb", "Antimony"),
    ("Te", "Tellurium"),
    ("I", "Iodine"),
    ("Xe", "Xenon"),
    ("Cs", "Cesium"),
    ("Ba", "Barium"),
    ("La", "Lanthanum"),
    ("Ce", "Cerium"),
    ("Pr", "Praesodymium"),
    ("Nd", "Neodymium"),
    ("Pm", "Promethium"),
    ("Sm", "Samarium"),
    ("Eu", "Europium"),
    ("Gd", "Gadolinium"),
    ("Tb", "Terbium"),
    ("Dy", "Dyprosium"),
    ("Ho", "Holmium"),
    ("Er", "Erbium"),
    ("Tm", "Thulium"),
    ("Yb", "Ytterbium"),
    ("Lu", "Lutetium"),
    ("Hf", "Hafnium"),
    ("Ta", "Tantalium"),
    ("W", "Wolfram"),
    ("Re", "Rhenium"),
    ("Os", "Osmium"),
    ("Ir", "Iridium"),
    ("Pt", "Platinum"),
    ("Au", "Gold"),
    ("Hg", "Mercury"),
    ("Tl", "Thallium"),
    ("Pb", "Lead"),
    ("Bi", "Bismuth"),
    ("Po", "Polonium"),
    ("At", "Astatine"),
    ("Rn", "Radon"),
    ("Fr", "Francium"),
    ("Ra", "Radium"),
    ("Ac", "Actinium"),
    ("Th", "Thorium"),
    ("Pa", "Protactinium"),
    ("U", "Uranium"),
    ("Np", "Neptunium"),
    ("Pu", "Plutonium"),
    ("Am", "Americium"),
    ("Cm", "Curium"),
    ("Bk", "Berkelium"),
    ("Cf", "Californium"),
    ("Es", "Einsteinium"),
    ("Fm", "Fermium"),
    ("Mv", "Mendelevium"),
    ("No", "Nobelium"),
    ("Lr", "Lawrencium"),
    ("Rf", "Rutherfordium"),
    ("Db", "Dubnium"),
    ("Sg", "Seaborgium"),
    ("Bh", "Bohrium"),
    ("Hs", "Hassium"),
    ("Mt", "Meitnerium"),
    ("Uun", "Ununnilium"),
    ("Uuu", "Unununium"),
    ("Uub", "Ununbium"),
    ("Uut", "Ununtrium"),
    ("Uuq", "Ununquadium"),
    ("Uup", "Ununpentium"),
    ("Uuh", "Ununhexium"),
    ("Uus", "Ununseptium"),
    ("Uuo", "Ununoctium"),
];
// cab264f1 ends here

// [[file:../gchemol-core.note::*base][base:1]]
/// Represents different kind of atom, such as cheimcial element, dummy atom,
/// etc.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum AtomKind {
    /// Chemical element.
    Element(usize),

    /// Dummy atom for special purpose.
    Dummy(String),
}

use self::AtomKind::{Dummy, Element};

impl AtomKind {
    /// Element symbol.
    pub fn symbol(&self) -> &str {
        match &self {
            Element(num) => ELEMENT_DATA[num - 1].0,
            Dummy(sym) => sym,
        }
    }

    /// Atomic number.
    pub fn number(&self) -> usize {
        match &self {
            Element(num) => *num,
            Dummy(_) => 0,
        }
    }

    /// Element name.
    pub fn name(&self) -> &str {
        match &self {
            Element(num) => ELEMENT_DATA[num - 1].1,
            Dummy(sym) => sym,
        }
    }
}
// base:1 ends here

// [[file:../gchemol-core.note::3ffd6c5d][3ffd6c5d]]
gut::config::lazy_static! {
    /// Global cache for quick get element number from symbol
    pub static ref ELEMENTS: std::collections::HashMap<&'static str, usize> = {
        ELEMENT_DATA.iter().zip(1..).map(|((s, _), i)| (*s, i)).collect()
    };
}

impl std::fmt::Display for AtomKind {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{:}", self.symbol())
    }
}

impl std::convert::From<usize> for AtomKind {
    fn from(value: usize) -> Self {
        match value {
            0 => Dummy("dummy".into()),
            _ => Element(value),
        }
    }
}

impl std::convert::From<&str> for AtomKind {
    fn from(label: &str) -> Self {
        // from element symbol
        if let Some(&n) = ELEMENTS.get(label) {
            return Element(n);
        }

        // element specified in number
        if let Ok(x) = label.parse::<usize>() {
            return Element(x);
        }

        // element specified in symbol or long name
        let sym = label.to_uppercase();
        for (i, &(s, n)) in ELEMENT_DATA.iter().enumerate() {
            if s.to_uppercase() == sym || n.to_uppercase() == sym {
                return Element(i + 1);
            }
        }

        // set as dummy atom as the last resort
        Dummy(label.into())
    }
}

impl std::convert::From<String> for AtomKind {
    fn from(label: String) -> Self {
        Self::from(label.as_str())
    }
}

impl std::convert::From<&String> for AtomKind {
    fn from(label: &String) -> Self {
        Self::from(label.as_str())
    }
}
// 3ffd6c5d ends here

// [[file:../gchemol-core.note::*test][test:1]]
#[test]
fn test_element() {
    let h1: AtomKind = 1.into();
    let h2: AtomKind = "H".into();
    let h3: AtomKind = "h".into();
    let h4: AtomKind = "1".into();

    assert_eq!(h1, h2);
    assert_eq!(h1, h3);
    assert_eq!(h1, h4);

    let si: AtomKind = "SI".into();
    assert_eq!(si.number(), 14);
    assert_eq!(si.to_string(), "Si");
    assert_eq!(si.name(), "Silicon");

    // dummy atom
    let x: AtomKind = "X".into();
    assert_eq!(x.symbol(), "X");
    assert_eq!(x.number(), 0);

    let s = String::from("Fe");
    let fe: AtomKind = s.into();
    assert_eq!(fe.symbol(), "Fe");
}
// test:1 ends here