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
use atom::Atom;
use namings::*;
use trait_element::Element;
use trait_properties::Properties;
use types::*;


#[derive(Debug, Eq, PartialEq, Clone, Hash)]
/// A molecule
pub struct Molecule {
    /// The compounds it contains
    pub compounds: Vec<MoleculeCompound>,
}


#[derive(Debug, Eq, PartialEq, Clone, Hash)]
/// A compound of a molecule
pub struct MoleculeCompound {
    /// The atom it uses
    pub atom: Atom,

    /// The amount
    pub amount: u8,
}


impl Molecule {
    /// Convert a string representation of a molecule into one
    pub fn from_string(string: String) -> Option<Molecule> {
        let mut compounds = vec![];

        let mut token = String::new();

        for c in string.chars() {
            // Ignore whitespace
            if is_whitespace!(c) {
                continue;
            }

            if is_upper!(c) && !token.is_empty() {
                let compound = MoleculeCompound::from_string(token).unwrap();

                compounds.push(compound);
                token = String::new();
            }

            token.push(c);
        }

        // If some tokens remain, convert it into a compound
        if !token.is_empty() {
            if let Some(compound) = MoleculeCompound::from_string(token) {
                compounds.push(compound);
            }
        }


        if !compounds.is_empty() {
            Some(Molecule { compounds: compounds })
        } else {
            None
        }
    }
}



impl MoleculeCompound {
    /// Takes a symbol string representing a MoleculeCompound, and turns it into one
    pub fn from_string(string: String) -> Option<MoleculeCompound> {
        let mut amount = 0;

        let mut token = String::new();

        for c in string.chars() {
            if is_letter!(c) {
                token.push(c);
            } else {
                amount *= 10;
                amount += to_number!(c);
            }
        }

        // If no amount given, assume 1
        if amount == 0 {
            amount = 1;
        }


        if let Some(atom) = Atom::from_string(token) {
            Some(MoleculeCompound {
                atom: atom,
                amount: amount,
            })
        } else {
            None
        }
    }


    /// Converts an Atom into a MoleculeCompound, taking care of diatomic ones
    pub fn from_atom(atom: Atom) -> MoleculeCompound {
        let amount = if atom.diatomic { 2 } else { 1 };

        MoleculeCompound {
            atom: atom,
            amount: amount,
        }
    }
}


impl Properties for Molecule {
    fn symbol(&self) -> String {
        let mut symbol = String::new();

        for compound in &self.compounds {
            symbol += &compound.symbol();
        }

        symbol
    }


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

        // TODO: Add special cases
        // NOTE: https://www.youtube.com/watch?v=mlRhLicNo8Q
        for compound in &self.compounds {
            name += &compound.name();
        }

        name
    }


    fn mass(&self) -> AtomMass {
        let mut mass = AtomMass::from(0.0);

        for compound in &self.compounds {
            mass += AtomMass::from(compound.mass());
        }

        mass
    }
}


impl Properties for MoleculeCompound {
    fn symbol(&self) -> String {
        let mut symbol = String::new();

        symbol += &self.atom.symbol();

        if self.amount > 1 {
            symbol += &subscript(self.amount);
        }

        symbol
    }


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

        if self.amount > 1 {
            name += &number_to_greek(self.amount);
        }

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

        name
    }


    fn mass(&self) -> AtomMass {
        self.atom.mass.clone() * (AtomMass_type::from(self.amount))
    }
}


impl Element for Molecule {
    fn get_charge(&self) -> Option<AtomCharge> {
        Some(AtomCharge::from(0))
    }


    fn get_molecule(&self) -> Option<&Molecule> {
        Some(self)
    }
}