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
use atom::{ Atom };
use electron::{ ELECTRON };
use namings::{ ion_superscript, number_to_roman };
use molecule::{ Molecule };
use trait_element::{ Element };
use trait_properties::{ Properties };
use types::*;


#[derive(Debug, Eq, PartialEq, Clone, Hash)]
/// An Ion
pub struct Ion {
    /// The molecule of this ion
    pub molecule: Molecule,


    /// The charge of this ion
    pub charge: Option<IonCharge>
}


/// Get the charge an atom has based on its group
pub fn charge_of_atom(atom: Atom) -> Option<IonCharge> {
    let group = atom.group;
    let number = atom.number;

    if group == 1 { Some(1) }
    else if group == 2 { Some(2) }
    else if group == 15 && number <= 15 { Some(-3) }
    else if group == 16 && number <= 34 { Some(-2) }
    else if group == 17 && number <= 53 { Some(-1) }
    else if group == 18 { Some(0) }

    else {
        None
    }
}


impl Ion {
    /// Convert a string representation of an Ion into one
    pub fn from_string(symbol: String) -> Option<Ion> {
        let mut molecule = None;
        let mut charge: IonCharge = 0;
        let mut is_negative = false;

        let mut token = String::new();
        let mut set_charge = false;

        for c in symbol.chars() {
            if c == ';' {
                // Electron
                if token == "e" {
                    return Some(ELECTRON.clone());
                }

                molecule = Molecule::from_string(token);
                token = String::new();
                set_charge = true;
                continue;
            }

            if set_charge {
                if c == '-' {
                    is_negative = true;
                    continue;
                }

                charge *= 10;
                charge += to_number!(c) as i8;
                continue;
            }

            token.push(c);
        }

        // It's just a molecule
        if token.len() > 0 && ! set_charge {
            // Electron
            if token == "e" {
                return Some(ELECTRON.clone());
            }

            molecule = Molecule::from_string(token);
        }

        if is_negative {
            // assume - to mean -1
            if charge == 0 {
                charge = -1;
            } else {
                charge *= -1;
            }
        }

        if let Some(molecule) = molecule {
            Some(Ion {
                molecule: molecule,
                charge: Some(charge)
            })
        } else {
            None
        }
    }


    /// Convert a Molecule into an Ion
    pub fn from_molecule(molecule: Molecule) -> Ion {
        Ion {
            molecule: molecule,
            charge: None // Will be calculated later
        }
    }


    /// Calculate the charge of this Ion
    pub fn calculate_charge(&self) -> Option<IonCharge> {
        let mut charge = 0;

        for molecule_compound in self.molecule.compounds.iter() {
            if let Some(atom_charge) = charge_of_atom(molecule_compound.atom.clone()) {
                let mol_charge = (molecule_compound.amount as IonCharge) * atom_charge;

                charge += mol_charge;
            }
        }

        // HACK: This seems to be correct for now
        charge = charge % 8;

        Some(charge)
    }
}


impl Element for Ion {
    fn get_charge(&self) -> Option<IonCharge> {
        if let Some(charge) = self.charge {
            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 != 0 {
                symbol += &ion_superscript(charge);
            }
        }

        return symbol;
    }


    fn name(&self) -> String {
        let mut name = String::new();

        name += &self.molecule.name();

        if let Some(charge) = self.get_charge() {
            if charge != 0 {
                name += "(";
                name += &number_to_roman(charge);
                name += ")";
            }
        }

        return name;
    }


    fn mass(&self) -> AtomMass {
        self.molecule.mass()
    }
}