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
use electron::ELECTRON;
use namings::*;
use molecule::Molecule;
use trait_element::Element;
use trait_properties::Properties;
use types::*;
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub struct Ion {
pub molecule: Molecule,
pub charge: Option<AtomCharge>,
}
impl Ion {
pub fn from_string(symbol: String) -> Option<Ion> {
let mut molecule = None;
let mut charge = 0;
let mut is_negative = false;
let mut is_positive = true;
let mut token = String::new();
let mut set_charge = false;
for c in symbol.chars() {
if set_charge {
if c == '-' {
is_negative = true;
continue;
}
if c == '+' {
is_positive = true;
continue;
}
if !is_number!(c) {
panic!("Expected charge, but got {}", c);
}
charge *= 10;
charge += to_number!(c) as i8;
continue;
}
if c == ';' {
if token == "e" {
return Some(ELECTRON.clone());
}
molecule = Molecule::from_string(token);
token = String::new();
set_charge = true;
continue;
}
token.push(c);
}
if !token.is_empty() && !set_charge {
if token == "e" {
return Some(ELECTRON.clone());
}
molecule = Molecule::from_string(token);
}
if is_negative {
if charge == 0 {
charge = -1;
} else {
charge *= -1;
}
}
if is_positive && charge == 0 {
charge = 1;
}
let charge_option = if set_charge {
Some(AtomCharge::from(charge))
} else {
None
};
if let Some(molecule) = molecule {
Some(Ion {
molecule: molecule,
charge: charge_option,
})
} else {
None
}
}
pub fn from_molecule(molecule: Molecule) -> Ion {
Ion {
molecule: molecule,
charge: None,
}
}
pub fn calculate_charge(&self) -> Option<AtomCharge> {
let mut charge = AtomCharge::from(0);
for molecule_compound in &self.molecule.compounds {
if let Some(atom_charge) = molecule_compound.atom.charge_by_group() {
let mol_charge = AtomCharge::from(
(molecule_compound.amount as AtomCharge_type) *
(atom_charge.0 as AtomCharge_type),
);
charge += mol_charge;
}
}
charge %= 8;
Some(charge)
}
}
impl Element for Ion {
fn get_charge(&self) -> Option<AtomCharge> {
if let Some(charge) = self.charge.clone() {
Some(charge)
} else {
self.calculate_charge()
}
}
fn get_molecule(&self) -> Option<&Molecule> {
Some(&self.molecule)
}
}
impl Properties for Ion {
fn symbol(&self) -> String {
let mut symbol = String::new();
symbol += &self.molecule.symbol();
if let Some(charge) = self.get_charge() {
if charge != AtomCharge::from(0) {
symbol += &ion_superscript(&charge);
}
}
symbol
}
fn name(&self) -> String {
let mut name = String::new();
name += &self.molecule.name();
if let Some(charge) = self.get_charge() {
if charge != AtomCharge::from(0) {
name += "(";
name += &number_to_roman(charge.0);
name += ")";
}
}
name
}
fn mass(&self) -> AtomMass {
self.molecule.mass()
}
}