molecular-formulas 0.1.8

A Rust crate for parsing, manipulating, and analyzing molecular formulas.
Documentation
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Properties that can be computed from molecular formulas.

use core::{fmt::Display, iter::repeat_n};

use crate::{ChargeLike, ChargedMolecularTree, CountLike, MolecularTree, prelude::Element};

mod chemical_formula;
mod inchi_formula;
mod mineral_formula;
mod residual_formula;
pub use chemical_formula::*;
use elements_rs::Isotope;
pub use inchi_formula::*;
pub use mineral_formula::*;
use num_traits::{CheckedAdd, CheckedMul, ConstZero};
pub use residual_formula::*;

/// Trait defining metadata associated with a molecular formula.
pub trait MolecularFormulaMetadata: Sized {
    /// The count type used in the molecular formula.
    type Count: CountLike;
}

/// Trait for computing various molecular properties.
pub trait MolecularFormula: MolecularFormulaMetadata + Display + From<Element> + Clone {
    /// The tree type used in the molecular formula.
    type Tree: MolecularTree<Self::Count>;

    /// Iterates over the counted mixtures in the molecular formula.
    ///
    /// # Implementation Notes
    ///
    /// Returns an iterator over the mixtures in the formula, each represented
    /// as a tuple of (`Self::Tree`, `Self::Count`), where the first element is
    /// the tree representing the mixture, and the second element is its
    /// count.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("NaCl.2H2O").unwrap();
    /// let mixtures: Vec<_> = formula.counted_mixtures().collect();
    /// assert_eq!(mixtures.len(), 2);
    /// let (count, tree) = &mixtures[1];
    /// assert_eq!(*count, 2);
    /// ```
    fn counted_mixtures(&self) -> impl Iterator<Item = (Self::Count, &Self::Tree)>;

    /// Iterates mutably over the counted mixtures in the molecular formula.
    fn counted_mixtures_mut(&mut self) -> impl Iterator<Item = (Self::Count, &mut Self::Tree)>;

    /// Into iterates over the counted mixtures in the molecular formula.
    ///
    /// # Implementation Notes
    ///
    /// Returns an iterator over the mixtures in the formula, each represented
    /// as a tuple of (`Self::Tree`, `Self::Count`), where the first element is
    /// the tree representing the mixture, and the second element is its
    /// count.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("NaCl.2H2O").unwrap();
    ///
    /// let mixtures: Vec<_> = formula.clone().into_counted_mixtures().collect();
    /// assert_eq!(mixtures.len(), 2);
    /// let (count, tree) = &mixtures[1];
    /// assert_eq!(*count, 2);
    /// ```
    fn into_counted_mixtures(self) -> impl Iterator<Item = (Self::Count, Self::Tree)>;

    /// Iterates over the mixtures in the molecular formula, repeating them
    /// according to their counts.
    fn mixtures(&self) -> impl Iterator<Item = &Self::Tree> {
        self.counted_mixtures().flat_map(|(count, tree)| {
            repeat_n(tree, count.try_into().ok().expect("Count type cannot be converted to usize - do you have an extremely large mixture count?"))
        })
    }

    /// Returns the number of mixtures in the molecular formula.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("NaCl.2H2O").unwrap();
    /// assert_eq!(formula.number_of_mixtures(), 3);
    /// ```
    fn number_of_mixtures(&self) -> usize {
        self.counted_mixtures()
            .map(|(count, _)| {
                let count: usize =
                    count.try_into().ok().expect("Count type cannot be converted to usize - do you have an extremely large mixture count?");
                count
            })
            .sum()
    }

    /// Returns the number of elements present in the molecular formula,
    /// counting repeating units according to their counts.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("C6H12O6").unwrap();
    /// assert_eq!(formula.number_of_elements(), 24);
    /// ```
    fn number_of_elements(&self) -> usize {
        self.counted_mixtures()
            .map(|(count, tree)| {
                let count: usize =
                    count.try_into().ok().expect("Count type cannot be converted to usize - do you have an extremely large mixture count?");
                count * tree.number_of_elements()
            })
            .sum()
    }

    /// Returns the number of non-hydrogen elements present in the molecular
    /// formula, counting repeating units according to their counts.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("C6H12O6").unwrap();
    ///
    /// assert_eq!(formula.number_of_non_hydrogens(), 12);
    /// ```
    fn number_of_non_hydrogens(&self) -> usize
    where
        usize: From<Self::Count>,
    {
        self.number_of_elements() - self.count_of_element::<usize>(Element::H).unwrap_or(0)
    }

    /// Iterates over the elements in the molecular formula.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use elements_rs::Element;
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("H2O").unwrap();
    /// let elements: Vec<_> = formula.elements().collect();
    /// assert_eq!(elements, vec![Element::H, Element::H, Element::O]);
    /// ```
    fn elements(&self) -> impl Iterator<Item = Element> {
        self.counted_mixtures().flat_map(|(count, tree)| {
            repeat_n(tree, count.try_into().ok().expect("Count type cannot be converted to usize - do you have an extremely large mixture count?")).flat_map(MolecularTree::elements)
        })
    }

    /// Iterates over the elements in the molecular formula, ignoring hydrogens.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use elements_rs::Element;
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("CH4").unwrap();
    /// let elements: Vec<_> = formula.non_hydrogens().collect();
    /// assert_eq!(elements, vec![Element::C]);
    /// ```
    fn non_hydrogens(&self) -> impl Iterator<Item = Element> {
        self.counted_mixtures().flat_map(|(count, tree)| {
            repeat_n(tree, count.try_into().ok().expect("Count type cannot be converted to usize - do you have an extremely large mixture count?")).flat_map(MolecularTree::non_hydrogens)
        })
    }

    /// Returns whether the molecular formula contains any elements.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("H2O").unwrap();
    /// assert!(formula.contains_elements());
    /// ```
    fn contains_elements(&self) -> bool {
        self.counted_mixtures().any(|(_, tree)| tree.contains_elements())
    }

    /// Returns whether the molecular formula contains any non-hydrogen
    /// elements.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("CH4").unwrap();
    /// assert!(formula.contains_non_hydrogens());
    /// let h2: ChemicalFormula = ChemicalFormula::from_str("H2").unwrap();
    /// assert!(!h2.contains_non_hydrogens());
    /// ```
    fn contains_non_hydrogens(&self) -> bool {
        self.counted_mixtures().any(|(_, tree)| tree.contains_non_hydrogens())
    }

    /// Returns whether the molecular formula contains the provided element.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use elements_rs::Element;
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("H2O").unwrap();
    /// assert!(formula.contains_element(Element::O));
    /// assert!(!formula.contains_element(Element::C));
    /// ```
    fn contains_element(&self, element: Element) -> bool {
        self.counted_mixtures().any(|(_, tree)| tree.contains_element(element))
    }

    /// Returns whether the molecular formula contains any isotopes.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("[13C]H4").unwrap();
    /// assert!(formula.contains_isotopes());
    /// let formula_no_iso: ChemicalFormula = ChemicalFormula::from_str("CH4").unwrap();
    /// assert!(!formula_no_iso.contains_isotopes());
    /// ```
    fn contains_isotopes(&self) -> bool {
        self.counted_mixtures().any(|(_, tree)| tree.contains_isotopes())
    }

    /// Returns whether the molecular formula contains the provided isotope.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use elements_rs::{Element, Isotope};
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("[13C]H4").unwrap();
    /// assert!(formula.contains_isotope(Isotope::try_from((Element::C, 13u16)).unwrap()));
    /// ```
    fn contains_isotope(&self, isotope: Isotope) -> bool {
        self.counted_mixtures().any(|(_, tree)| tree.contains_isotope(isotope))
    }

    /// Returns the number of elements of a specific type in the molecular
    /// formula.
    ///
    /// Returns None if the provided data type C cannot represent the count.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use elements_rs::Element;
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("H2O").unwrap();
    /// assert_eq!(formula.count_of_element::<u32>(Element::H), Some(2));
    /// assert_eq!(formula.count_of_element::<u32>(Element::O), Some(1));
    /// ```
    fn count_of_element<C>(&self, element: Element) -> Option<C>
    where
        C: From<Self::Count> + CheckedAdd + CheckedMul + ConstZero,
    {
        let mut total: C = C::zero();
        for (count, tree) in self.counted_mixtures() {
            total = total.checked_add(
                &C::from(count).checked_mul(&C::from(tree.count_of_element(element)?))?,
            )?;
        }
        Some(total)
    }

    /// Returns the number of isotopes of a specific type in the molecular
    /// formula.
    ///
    /// Returns None if the provided data type C cannot represent the count.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use elements_rs::{Element, Isotope};
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("[13C]2H4").unwrap();
    /// assert_eq!(
    ///     formula.count_of_isotope::<u32>(Isotope::try_from((Element::C, 13u16)).unwrap()),
    ///     Some(2)
    /// );
    /// ```
    fn count_of_isotope<C>(&self, isotope: Isotope) -> Option<C>
    where
        C: From<Self::Count> + CheckedAdd + CheckedMul + ConstZero,
    {
        let mut total: C = C::zero();
        for (count, tree) in self.counted_mixtures() {
            total = total.checked_add(
                &C::from(count).checked_mul(&C::from(tree.count_of_isotope(isotope)?))?,
            )?;
        }
        Some(total)
    }

    /// Returns the isotopologue mass of the molecular formula without
    /// considering any charge.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("H2O").unwrap();
    /// let mass = formula.isotopologue_mass();
    /// assert!(mass > 18.0 && mass < 18.1); // atomic mass of H ~ 1.008, O ~ 15.999
    /// ```
    fn isotopologue_mass(&self) -> f64 {
        let mut total_mass = 0.0;
        for (count, tree) in self.counted_mixtures() {
            let count: f64 = count.into();
            total_mass += count * tree.isotopologue_mass();
        }
        total_mass
    }

    /// Returns whether the molecular formula is a noble gas compound.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("He").unwrap();
    /// assert!(formula.is_noble_gas_compound());
    /// let water: ChemicalFormula = ChemicalFormula::from_str("H2O").unwrap();
    /// assert!(!water.is_noble_gas_compound());
    /// ```
    fn is_noble_gas_compound(&self) -> bool {
        self.counted_mixtures().all(|(_, tree)| tree.is_noble_gas_compound())
    }

    /// Returns whether the molecular formula is sorted according to Hill
    /// system.
    ///
    /// If the formula contains carbon atoms, they must be listed first,
    /// followed by hydrogen atoms, and then all other elements in
    /// alphabetical order. If the formula does not contain carbon atoms,
    /// all elements must be listed in alphabetical order, including hydrogen.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula1: ChemicalFormula = ChemicalFormula::from_str("C6H12O6").unwrap();
    /// assert!(formula1.is_hill_sorted(), "Formula `C6H12O6` should be Hill sorted");
    /// let formula2: ChemicalFormula = ChemicalFormula::from_str("H2O").unwrap();
    /// assert!(formula2.is_hill_sorted(), "Formula `H2O` should be Hill sorted");
    /// let formula3: ChemicalFormula = ChemicalFormula::from_str("C2H5OH").unwrap();
    /// assert!(!formula3.is_hill_sorted(), "Formula `C2H5OH` should not be Hill sorted");
    /// let formula4: ChemicalFormula = ChemicalFormula::from_str("NaCl").unwrap();
    /// assert!(!formula4.is_hill_sorted(), "Formula `NaCl` should not be Hill sorted");
    /// let formula5: ChemicalFormula = ChemicalFormula::from_str("C2H6O").unwrap();
    /// assert!(formula5.is_hill_sorted(), "Formula `C2H6O` should be Hill sorted");
    /// let formula6: ChemicalFormula = ChemicalFormula::from_str("C6H8O6").unwrap();
    /// assert!(formula6.is_hill_sorted(), "Formula `C6H8O6` should be Hill sorted");
    /// let formula7: ChemicalFormula = ChemicalFormula::from_str("C16H25NS").unwrap();
    /// assert!(formula7.is_hill_sorted(), "Formula `C16H25NS` should be Hill sorted");
    /// let formula8: ChemicalFormula = ChemicalFormula::from_str("C28H23ClO7").unwrap();
    /// assert!(formula8.is_hill_sorted(), "Formula `{formula8}` should be Hill sorted");
    /// let formula9: ChemicalFormula = ChemicalFormula::from_str("CBr2F2").unwrap();
    /// assert!(formula9.is_hill_sorted(), "Formula `CBr2F2` should be Hill sorted");
    /// let formula10: ChemicalFormula = ChemicalFormula::from_str("C").unwrap();
    /// assert!(formula10.is_hill_sorted(), "Formula `C` should be Hill sorted");
    /// let formula11: ChemicalFormula = ChemicalFormula::from_str("H").unwrap();
    /// assert!(formula11.is_hill_sorted(), "Formula `H` should be Hill sorted");
    /// let formula12: ChemicalFormula = ChemicalFormula::from_str("C2").unwrap();
    /// assert!(formula12.is_hill_sorted(), "Formula `C2` should be Hill sorted");
    /// let mixture: ChemicalFormula = ChemicalFormula::from_str("C32H34N4O4.Ni").unwrap();
    /// assert!(mixture.is_hill_sorted(), "Mixture `C32H34N4O4.Ni` should be Hill sorted");
    /// let mixture2: ChemicalFormula = ChemicalFormula::from_str("ClH.Na").unwrap();
    /// assert!(mixture2.is_hill_sorted(), "Mixture `ClH.Na` should be Hill sorted");
    /// let mixture3: ChemicalFormula = ChemicalFormula::from_str("C20H18F3N4O8P.Na").unwrap();
    /// assert!(mixture3.is_hill_sorted(), "Mixture `{mixture3}` should be Hill sorted");
    /// let unsorted_mixture1: ChemicalFormula = ChemicalFormula::from_str("C32H34O4N4.Ni").unwrap();
    /// assert!(
    ///     !unsorted_mixture1.is_hill_sorted(),
    ///     "Mixture `C32H34O4N4.Ni` should not be Hill sorted"
    /// );
    /// let unsorted_mixture2: ChemicalFormula = ChemicalFormula::from_str("HCl.Na").unwrap();
    /// assert!(!unsorted_mixture2.is_hill_sorted(), "Mixture `HCl.Na` should not be Hill sorted");
    /// let unsorted_mixture3: ChemicalFormula =
    ///     ChemicalFormula::from_str("C15H18O7.C15O6H16").unwrap();
    /// assert!(
    ///     !unsorted_mixture3.is_hill_sorted(),
    ///     "Mixture `C15H18O7.C15O6H16` should not be Hill sorted"
    /// );
    /// let unsorted_formula: ChemicalFormula = ChemicalFormula::from_str("CH2SCl2O3").unwrap();
    /// assert!(!unsorted_formula.is_hill_sorted(), "Formula `CH2SCl2O3` should not be Hill sorted");
    /// let unsorted_formula2: ChemicalFormula = ChemicalFormula::from_str("C6H18NaNSi4").unwrap();
    /// assert!(!unsorted_formula2.is_hill_sorted(), "Formula `C6H18NaNSi4` should not be Hill sorted");
    /// ```
    #[must_use]
    fn is_hill_sorted(&self) -> bool {
        self.counted_mixtures().all(|(_, tree)| tree.is_hill_sorted())
    }

    /// Returns the element at the specified index in the molecular formula,
    /// counting repeating units according to their counts.
    ///
    /// # Example
    ///
    /// ```rust
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use elements_rs::Element;
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::try_from("H2O")?;
    /// assert_eq!(formula.get_element(0), Some(Element::H));
    /// assert_eq!(formula.get_element(1), Some(Element::H));
    /// assert_eq!(formula.get_element(2), Some(Element::O));
    /// assert_eq!(formula.get_element(3), None);
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    fn get_element(&self, index: usize) -> Option<Element> {
        self.elements().enumerate().find_map(
            |(i, element)| {
                if i == index { Some(element) } else { None }
            },
        )
    }

    /// Returns the element at the specified index in the molecular formula,
    /// not counting repeating units according to their counts, and ignoring
    /// any hydrogens (used typically for InchI parsing).
    ///
    /// # Example
    ///
    /// ```rust
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use elements_rs::Element;
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::try_from("H2O")?;
    /// assert_eq!(formula.get_non_hydrogen(0), Some(Element::O));
    /// assert_eq!(formula.get_non_hydrogen(1), None);
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    fn get_non_hydrogen(&self, index: usize) -> Option<Element> {
        self.non_hydrogens()
            .enumerate()
            .find_map(|(i, element)| if i == index { Some(element) } else { None })
    }

    /// Returns a version of the molecular formula with all isotopes converted
    /// to their elemental forms.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("[13C]H4").unwrap();
    /// let elemental = formula.isotopic_normalization();
    /// assert_eq!(elemental.to_string(), "CH₄");
    ///
    /// let mixed_formula: ChemicalFormula = ChemicalFormula::from_str("[13C]T4.O[18O]").unwrap();
    /// let mixed_elemental = mixed_formula.isotopic_normalization();
    /// assert_eq!(mixed_elemental.to_string(), "CH₄.OO");
    /// ```
    #[must_use]
    fn isotopic_normalization(&self) -> Self {
        let mut formula = self.clone();
        for (_, tree) in formula.counted_mixtures_mut() {
            *tree = tree.isotopic_normalization();
        }
        formula
    }
}

/// A molecular formula that can hold a charge.
pub trait ChargedMolecularFormulaMetadata: MolecularFormulaMetadata {
    /// The charge type used in the molecular formula.
    type Charge: ChargeLike + TryFrom<Self::Count>;
}

/// Trait for computing various charged molecular properties.
pub trait ChargedMolecularFormula:
    MolecularFormula<Tree: ChargedMolecularTree<Self::Count, Self::Charge>>
    + ChargedMolecularFormulaMetadata
{
    /// Returns the overall charge of the molecular formula.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("H2O+2").unwrap();
    /// assert_eq!(formula.charge(), 2.0);
    /// let neutral: ChemicalFormula = ChemicalFormula::from_str("H2O").unwrap();
    /// assert_eq!(neutral.charge(), 0.0);
    /// ```
    fn charge(&self) -> f64 {
        self.counted_mixtures()
            .map(|(count, tree)| {
                let count: f64 = count.into();
                count * tree.charge()
            })
            .sum()
    }

    /// Returns the isotopologue mass with charge considered.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let neutral: ChemicalFormula = ChemicalFormula::from_str("H").unwrap();
    /// let cation: ChemicalFormula = ChemicalFormula::from_str("H+").unwrap();
    ///
    /// // Mass of H+ should be less than neutral H (electrons have mass)
    /// assert!(cation.isotopologue_mass_with_charge() < neutral.isotopologue_mass_with_charge());
    /// ```
    fn isotopologue_mass_with_charge(&self) -> f64 {
        self.counted_mixtures()
            .map(|(count, tree)| {
                let count: f64 = count.into();
                count * tree.isotopologue_mass_with_charge()
            })
            .sum()
    }

    /// Returns the isotopologue mass over charge ratio.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("H2O+2").unwrap();
    /// let mz = formula.isotopologue_mass_over_charge();
    /// // Mass ~18, charge 2, so m/z ~9
    /// assert!(mz > 9.0 && mz < 9.1);
    /// ```
    fn isotopologue_mass_over_charge(&self) -> f64 {
        self.isotopologue_mass_with_charge() / self.charge()
    }

    /// Returns the molar mass.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::str::FromStr;
    ///
    /// use molecular_formulas::prelude::*;
    ///
    /// let formula: ChemicalFormula = ChemicalFormula::from_str("H2O").unwrap();
    /// let molar_mass = formula.molar_mass();
    /// // Molar mass of water is approx 18.015 g/mol
    /// assert!(molar_mass > 18.0 && molar_mass < 18.02);
    /// ```
    fn molar_mass(&self) -> f64 {
        self.counted_mixtures()
            .map(|(count, tree)| {
                let count: f64 = count.into();
                count * tree.molar_mass()
            })
            .sum()
    }
}

impl<M> ChargedMolecularFormula for M where
    M: MolecularFormula<Tree: ChargedMolecularTree<M::Count, M::Charge>>
        + ChargedMolecularFormulaMetadata
{
}