chem-eq 0.3.2

Parse chemical equations and balance, get mol ratios and use ΔH
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
use std::str::FromStr;

use itertools::Itertools;

use crate::{
    compound::Compound,
    error::{ConcentrationError, ConcentrationNameError, EquationError},
    parse, Direction, State,
};

// Used for rustdoc
#[cfg(all(doc, feature = "balance"))]
#[allow(unused)]
use crate::balance::EquationBalancer;

/// A Chemical Equation. Containing a left and right side. Also keeps
/// track of the mol ratio.
///
/// Eg: `4Fe + 3O2 -> 2Fe2O3`
#[derive(Debug, Default, Clone, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Equation {
    pub(crate) left: Vec<Compound>,
    pub(crate) right: Vec<Compound>,
    pub(crate) direction: Direction,
    pub(crate) equation: String,
    pub(crate) delta_h: f32,
    pub(crate) temperature: Option<f32>,
    pub(crate) volume: Option<f32>,
}

impl PartialEq for Equation {
    fn eq(&self, other: &Self) -> bool {
        self.left() == other.left()
            && self.right() == other.right()
            && self.direction() == other.direction()
            && self.equation() == other.equation()
            && self.delta_h() == other.delta_h()
            && self.temperature() == other.temperature()
            && self.volume() == other.volume()
    }
}
impl Eq for Equation {}

impl TryFrom<&str> for Equation {
    type Error = EquationError;

    fn try_from(s: &str) -> Result<Self, EquationError> {
        Self::new(s)
    }
}

impl FromStr for Equation {
    type Err = EquationError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s)
    }
}

impl Equation {
    /// Create an [`Equation`] from a [`str`]. Fails if the string couldn't
    /// be parsed.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, error::EquationError};
    ///
    /// let eq = Equation::new("2H2 + O2 -> 2H2O");
    /// assert!(eq.is_ok());
    ///
    /// let eq = Equation::new("H2b + bad_name == joe");
    /// assert!(matches!(eq, Err(EquationError::ParsingError(_))));
    /// ```
    pub fn new(input: &str) -> Result<Self, EquationError> {
        match parse::parse_equation(input) {
            Ok((i, _)) if !i.trim().is_empty() => Err(EquationError::TooMuchInput(i.to_string())),
            Ok((_, eq)) if eq.is_valid() => Ok(eq),
            Ok(_) => Err(EquationError::IncorrectEquation),
            Err(nom::Err::Error(e) | nom::Err::Failure(e)) => {
                Err(EquationError::ParsingError(e.into()))
            }
            // no streaming parsers were used
            Err(nom::Err::Incomplete(_)) => unreachable!(),
        }
    }

    /// Get the mol ratio of the equation (left over right). Will count any compound
    /// with no specified state.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// // returns left over right
    /// // if states aren't given, everything is counted
    /// let eq = Equation::new("2H2 + O2 -> 2H2O").unwrap();
    /// assert_eq!(eq.mol_ratio(), (3, 2));
    ///
    /// // doesn't matter how bad an equation this is...
    /// let eq = Equation::new("4FeH3(s) + 3O2(g) -> 2Fe2O3(s) + 6H2(g)").unwrap();
    /// assert_eq!(eq.mol_ratio(), (3, 6));
    /// ```
    pub fn mol_ratio(&self) -> (usize, usize) {
        let left = self
            .left
            .iter()
            .filter(|c| {
                c.state
                    .as_ref()
                    .map_or(true, |s| matches!(s, State::Aqueous | State::Gas))
            })
            .map(|c| c.coefficient)
            .sum::<usize>();
        let right = self
            .right
            .iter()
            .filter(|c| {
                c.state
                    .as_ref()
                    .map_or(true, |s| matches!(s, State::Aqueous | State::Gas))
            })
            .map(|c| c.coefficient)
            .sum::<usize>();
        if left == 0 && right == 0 {
            (1, 1)
        } else {
            (left, right)
        }
    }

    /// Get a vec of each unique element name
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// let eq = Equation::new("2O2 + H2 -> 2H2O").unwrap();
    /// assert_eq!(eq.uniq_elements().len(), 2);
    ///
    /// let eq =
    ///     Equation::new("3(NH4)2SO4(aq) + Fe3(PO4)2(s) <- 2(NH4)3PO4(aq) + 3FeSO4(aq)").unwrap();
    /// assert_eq!(eq.uniq_elements().len(), 6);
    /// ```
    pub fn uniq_elements(&self) -> Vec<&str> {
        // get the name of every element in the equation
        let element_names = self
            .iter_compounds()
            .flat_map(|c| &c.elements)
            .map(|e| e.symbol())
            .unique()
            .collect::<Vec<&str>>();

        element_names
    }

    /// Count how many compounds are in the whole equation.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// let eq = Equation::new("O2 + 2H2 -> 2H2O").unwrap();
    /// assert_eq!(eq.num_compounds(), 3);
    ///
    /// let eq = Equation::new("3(NH4)2SO4(aq) + Fe3(PO4)2(s) <- 2(NH4)3PO4(aq) + 3FeSO4(aq)").unwrap();
    /// assert_eq!(eq.num_compounds(), 4);
    /// ```
    pub fn num_compounds(&self) -> usize {
        self.left.len() + self.right.len()
    }

    /// Check if an equation is valid.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, error::EquationError};
    ///
    /// let eq = Equation::new("O2 + 2H2 -> 2H2O");
    /// assert!(eq.is_ok());
    ///
    /// let eq = Equation::new("Fe + S8 -> Fe2O3");
    /// // fails because the equation doesn't have sulfur and oxygen on both sides
    /// assert_eq!(eq, Err(EquationError::IncorrectEquation));
    /// ```
    pub(crate) fn is_valid(&self) -> bool {
        let mut left_elements = self
            .left
            .iter()
            .flat_map(|c| &c.elements)
            .map(|e| e.symbol())
            .unique()
            .collect::<Vec<&str>>();
        let mut right_elements = self
            .right
            .iter()
            .flat_map(|c| &c.elements)
            .map(|e| e.symbol())
            .unique()
            .collect::<Vec<&str>>();

        // sort to make sure comparisons work
        left_elements.sort_unstable();
        right_elements.sort_unstable();

        // simple verification that the same elements are on both sides
        left_elements == right_elements
    }

    /// Reconstruct original equation without using the saved original string.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// let eq = Equation::new("O2 + H2 -> H2O").unwrap();
    /// assert_eq!(eq.reconstruct(), "1O2 + 1H2 -> 1H2O1");
    /// ```
    pub fn reconstruct(&self) -> String {
        format!(
            "{} {} {}",
            self.left
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<String>>()
                .join(" + "),
            self.direction,
            self.right
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<String>>()
                .join(" + "),
        )
    }

    /// Create an iterator over all compounds of an equation
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, Compound};
    ///
    /// let eq = Equation::new("O2 + H2 -> H2O").unwrap();
    /// assert_eq!(eq.iter_compounds().collect::<Vec<&Compound>>().len(), 3);
    /// ```
    // Mostly as a convenience method as this appears in multiple places
    pub fn iter_compounds(&self) -> impl Iterator<Item = &Compound> {
        self.left.iter().chain(self.right.iter())
    }

    /// Create a mutable iterator over all compounds of an equation.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, Compound};
    ///
    /// let mut eq = Equation::new("O2 + H2 -> H2O").unwrap();
    /// assert_eq!(eq.iter_compounds_mut().collect::<Vec<&mut Compound>>().len(), 3);
    /// ```
    // Mostly as a convenience method as this appears in multiple places
    pub fn iter_compounds_mut(&mut self) -> impl Iterator<Item = &mut Compound> {
        self.left.iter_mut().chain(self.right.iter_mut())
    }

    /// Check if the equation is balanced
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// let eq = Equation::new("C + 2H2O -> CO2 + 2H2").unwrap();
    /// assert!(eq.is_balanced());
    ///
    /// let eq = Equation::new("Mg(OH)2 + Fe -> Fe(OH)3 + Mg").unwrap();
    /// assert!(!eq.is_balanced());
    /// ```
    #[cfg(feature = "balance")]
    #[cfg_attr(docsrs, doc(cfg(feature = "balance")))]
    pub fn is_balanced(&self) -> bool {
        use std::collections::HashMap;
        let mut lhs: HashMap<&str, usize> = HashMap::default();
        let mut rhs: HashMap<&str, usize> = HashMap::default();

        // left hand side
        for cmp in &self.left {
            for el in &cmp.elements {
                let count = lhs.get(el.symbol()).unwrap_or(&0);
                lhs.insert(el.symbol(), count + el.count * cmp.coefficient);
            }
        }

        // right hand side
        for cmp in &self.right {
            for el in &cmp.elements {
                let count = rhs.get(el.symbol()).unwrap_or(&0);
                rhs.insert(el.symbol(), count + el.count * cmp.coefficient);
            }
        }

        // different amount of elements
        lhs.len() == rhs.len()
            && lhs.keys().all(|k| {
                if rhs.contains_key(k) {
                    return lhs.get(k).unwrap() == rhs.get(k).unwrap();
                }
                false
            })
    }

    /// Create an [`EquationBalancer`], mostly as a convenience method.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// let eq = Equation::new("H2 + O2 -> H2O").unwrap().to_balancer().balance().unwrap();
    /// assert_eq!(eq.equation(), "2H2 + O2 -> 2H2O".to_string());
    /// ```
    #[cfg(feature = "balance")]
    #[cfg_attr(docsrs, doc(cfg(feature = "balance")))]
    pub fn to_balancer(&self) -> crate::balance::EquationBalancer {
        use crate::balance::EquationBalancer;

        EquationBalancer::new(self)
    }

    /// Check whether an equation is exothermic
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// let mut eq = Equation::new("2Mg(s) + O2(g) -> 2MgO(s)").unwrap();
    /// eq.set_delta_h(-601.1);
    /// assert!(eq.is_exothermic());
    /// ```
    pub fn is_exothermic(&self) -> bool {
        self.delta_h() < 0.0
    }

    /// Check whether an equation is endothermic
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// let mut eq = Equation::new("6CO2 + 6H2O -> C6H12O6 + 6O2").unwrap();
    /// eq.set_delta_h(2802.7);
    /// assert!(eq.is_endothermic());
    /// ```
    pub fn is_endothermic(&self) -> bool {
        self.delta_h() > 0.0
    }

    /// Get an iterator over each compounds name.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// let eq = Equation::new("H2 + O2 -> H2O").unwrap();
    /// assert_eq!(vec!["H2", "O2", "H2O"], eq.compound_names().collect::<Vec<&str>>());
    ///
    /// let eq = Equation::new("Fe2O3 <- Fe + O2").unwrap();
    /// assert_eq!(vec!["Fe2O3", "Fe", "O2"], eq.compound_names().collect::<Vec<&str>>());
    /// ```
    pub fn compound_names(&self) -> impl Iterator<Item = &str> {
        self.equation()
            .split(' ')
            .filter(|s| !matches!(*s, "+" | "<-" | "<->" | "->"))
    }

    /// Get an iterator for each concentration in an equation
    pub fn concentrations(&self) -> impl Iterator<Item = &f32> {
        self.iter_compounds().map(|cmp| &cmp.concentration)
    }

    /// Get a mutable iterator for each concentration in an equation
    pub fn concentrations_mut(&mut self) -> impl Iterator<Item = &mut f32> {
        self.iter_compounds_mut().map(|cmp| &mut cmp.concentration)
    }

    /// Get an iterator yielding compound names and concentrations
    pub fn name_and_concentration(&self) -> impl Iterator<Item = (&str, &f32)> {
        self.compound_names().zip(self.concentrations())
    }

    /// Get a mutable iterator yielding compound names and mutable concentrations
    pub fn name_and_concentration_mut(&mut self) -> impl Iterator<Item = (String, &mut f32)> {
        self.equation
            .split(' ')
            .filter(|s| !matches!(*s, "+" | "<-" | "<->" | "->"))
            .map(ToString::to_string)
            .collect_vec()
            .into_iter()
            .zip(self.concentrations_mut())
    }

    /// Get a vec of all concentrations
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
    /// assert_eq!(eq.get_concentrations(), vec![0.0, 0.0, 0.0]);
    ///
    /// eq.set_concentrations(&[1.0, 2.0, 3.0]);
    /// assert_eq!(eq.get_concentrations(), vec![1.0, 2.0, 3.0]);
    /// ```
    pub fn get_concentrations(&self) -> Vec<f32> {
        self.concentrations().copied().collect()
    }

    /// Set concentrations with a slice. A convenience method to quickly set all
    /// compounds to have a concentration.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, error::ConcentrationError};
    ///
    /// let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
    /// eq.set_concentrations(&[1.0, 2.0, 3.0]).unwrap();
    /// assert_eq!(eq.get_concentrations(), vec![1.0, 2.0, 3.0]);
    ///
    /// assert_eq!(eq.set_concentrations(&[1.0, 34.0]), Err(ConcentrationError::WrongSliceSize));
    /// ```
    pub fn set_concentrations(&mut self, concentrations: &[f32]) -> Result<(), ConcentrationError> {
        // check assumptions
        if concentrations.len() != self.num_compounds() {
            return Err(ConcentrationError::WrongSliceSize);
        }
        if concentrations.iter().any(|&c| c.is_nan()) {
            return Err(ConcentrationError::NAN);
        }

        for (orig, new) in self.concentrations_mut().zip(concentrations.iter()) {
            *orig = *new;
        }

        Ok(())
    }

    /// Get a singular compounds concentration by its name.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, error::ConcentrationNameError};
    ///
    /// let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
    /// eq.set_concentration_by_name("O2", 0.25).unwrap();
    /// assert_eq!(eq.get_concentration_by_name("O2"), Ok(0.25));
    ///
    /// assert_eq!(eq.get_concentration_by_name("joe"), Err(ConcentrationNameError::NotFound));
    /// ```
    pub fn get_concentration_by_name(&self, name: &str) -> Result<f32, ConcentrationNameError> {
        // I don't like the collecting here...
        // but I can't avoid double borrowing self as mutable and immutable
        let (_name, cmp) = self
            .compound_names()
            .map(ToString::to_string)
            .collect_vec()
            .into_iter()
            .zip(self.iter_compounds())
            .find(|(n, _c)| *n == name)
            .ok_or(ConcentrationNameError::NotFound)?;
        Ok(cmp.concentration)
    }

    /// Get a compound by name
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, Compound};
    ///
    /// let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
    /// let cmp = eq.get_compound_by_name("O2");
    /// assert_eq!(cmp.unwrap(), &Compound::parse("O2").unwrap());
    ///
    /// assert!(eq.get_compound_by_name("joe").is_none());
    /// ```
    pub fn get_compound_by_name(&self, name: &str) -> Option<&Compound> {
        self.compound_names()
            .zip(self.iter_compounds())
            .find(|(n, _c)| *n == name)
            .map(|(_n, c)| c)
    }

    /// Get a mutable reference to a compound by name
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, Compound};
    ///
    /// let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
    /// let cmp = eq.get_compound_by_name_mut("O2");
    /// assert_eq!(cmp.unwrap(), &mut Compound::parse("O2").unwrap());
    ///
    /// assert!(eq.get_compound_by_name("joe").is_none());
    /// ```
    pub fn get_compound_by_name_mut(&mut self, name: &str) -> Option<&mut Compound> {
        let i = self
            .compound_names()
            .enumerate()
            .find(|(_i, n)| *n == name)
            .map(|(i, _n)| i)?;
        self.nth_compound_mut(i)
    }

    /// Set a singular compounds concentration by its name.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, error::ConcentrationNameError};
    ///
    /// let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
    /// eq.set_concentration_by_name("O2", 0.25).unwrap();
    /// assert_eq!(eq.get_concentrations(), vec![0.0, 0.25, 0.0]);
    ///
    /// assert_eq!(eq.set_concentration_by_name("joe", 24.0), Err(ConcentrationNameError::NotFound));
    /// assert_eq!(eq.set_concentration_by_name("H2O", f32::NAN), Err(ConcentrationNameError::NAN));
    /// ```
    pub fn set_concentration_by_name(
        &mut self,
        name: &str,
        concentration: f32,
    ) -> Result<(), ConcentrationNameError> {
        if concentration.is_nan() {
            return Err(ConcentrationNameError::NAN);
        }
        // I don't like the collecting here...
        // but I can't avoid double borrowing self as mutable and immutable
        let (_name, cmp) = self
            .compound_names()
            .map(ToString::to_string)
            .collect_vec()
            .into_iter()
            .zip(self.iter_compounds_mut())
            .find(|(n, _c)| *n == name)
            .ok_or(ConcentrationNameError::NotFound)?;
        cmp.concentration = concentration;
        Ok(())
    }

    /// Get the k expression or Kc of an equation. Returns [`None`] if one side
    /// has a compound with a concentration of 0
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chem_eq::Equation;
    ///
    /// let eq = Equation::new("H2 + O2 -> H2O").unwrap();
    /// // is nan because all compounds have an initial concentration of 0M
    /// assert!(eq.equilibrium_constant().is_none());
    ///
    /// let mut eq = Equation::new("N2 + 2O2 -> 2NO2").unwrap();
    /// eq.set_concentrations(&[0.25, 0.50, 0.75]).unwrap();
    /// assert_eq!(eq.equilibrium_constant().unwrap(), (0.75 * 0.75) / (0.25 * 0.5 * 0.5));
    /// ```
    pub fn equilibrium_constant(&self) -> Option<f32> {
        match self.reaction_quotient() {
            ReactionQuotient::Val(q) => Some(q),
            _ => None,
        }
    }

    /// Get Qc of the equation, called the reaction coefficient. It's
    /// calculated the same way as the k-expression, however it can apply to non
    /// equilibrium concentrations.
    ///
    /// ## Returns
    ///
    /// Assuming the hypothetical reaction where a, b, c, and d are the
    /// coefficents of A, B, C and D respectively:
    /// ```text
    /// aA + bB <-> cC + dD
    /// ```
    ///
    /// Since `Qc = ([C]^c * [D]^d) / ([A]^a * [B]^b)`,
    /// This function will return:
    /// - [`ReactionQuotient::BothSidesZero`] if `[C]^c * [D]^d` and `[A]^a * [B]^b` are both 0
    /// - [`ReactionQuotient::LeftZero`] if `[A]^a * [B]^b` is 0
    /// - [`ReactionQuotient::RightZero`] if `[C]^c * [D]^d` is 0
    /// - otherwise the result of the above equation contained in [`ReactionQuotient::Val`]
    pub fn reaction_quotient(&self) -> ReactionQuotient {
        // skip compounds that are solid or liquid
        let left = self
            .left
            .iter()
            .filter(|c| matches!(c.state, Some(State::Aqueous | State::Gas) | None))
            .fold(1.0, |acc, cmp| {
                acc * cmp.concentration.powf(cmp.coefficient as f32)
            });

        let right = self
            .right
            .iter()
            .filter(|c| matches!(c.state, Some(State::Aqueous | State::Gas) | None))
            .fold(1.0, |acc, cmp| {
                acc * cmp.concentration.powf(cmp.coefficient as f32)
            });

        if left == 0.0 && right == 0.0 {
            ReactionQuotient::BothSidesZero
        } else if right == 0.0 {
            ReactionQuotient::RightZero
        } else if left == 0.0 {
            ReactionQuotient::LeftZero
        } else {
            ReactionQuotient::Val(right / left)
        }
    }

    /// Get the nth compound of the equation.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, Compound};
    ///
    /// let eq = Equation::new("H2 + O2 -> H2O").unwrap();
    /// assert_eq!(eq.nth_compound(0).unwrap(), &Compound::parse("H2").unwrap());
    /// assert_eq!(eq.nth_compound(1).unwrap(), &Compound::parse("O2").unwrap());
    /// assert_eq!(eq.nth_compound(2).unwrap(), &Compound::parse("H2O").unwrap());
    /// assert!(eq.nth_compound(3).is_none());
    /// ```
    pub fn nth_compound(&self, idx: usize) -> Option<&Compound> {
        if idx < self.left.len() {
            Some(&self.left[idx])
        } else if idx < self.left.len() + self.right.len() {
            Some(&self.right[idx - self.left.len()])
        } else {
            None
        }
    }

    /// Get the nth compound of the equation mutably.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use chem_eq::{Equation, Compound};
    ///
    /// let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
    /// assert_eq!(eq.nth_compound_mut(0).unwrap(), &mut Compound::parse("H2").unwrap());
    /// assert_eq!(eq.nth_compound_mut(1).unwrap(), &mut Compound::parse("O2").unwrap());
    /// assert_eq!(eq.nth_compound_mut(2).unwrap(), &mut Compound::parse("H2O").unwrap());
    /// assert!(eq.nth_compound_mut(3).is_none());
    /// ```
    pub fn nth_compound_mut(&mut self, idx: usize) -> Option<&mut Compound> {
        if idx < self.left.len() {
            Some(&mut self.left[idx])
        } else if idx < self.left.len() + self.right.len() {
            Some(&mut self.right[idx - self.left.len()])
        } else {
            None
        }
    }

    /// Getter for the left side of the equation.
    pub fn left(&self) -> &[Compound] {
        self.left.as_ref()
    }

    /// Mut getter for the right side of the equation
    pub fn left_mut(&mut self) -> &mut Vec<Compound> {
        &mut self.left
    }

    /// Getter for the right side of the equation
    pub fn right(&self) -> &[Compound] {
        self.right.as_ref()
    }

    /// Mut getter for the right side of the equation
    pub fn right_mut(&mut self) -> &mut Vec<Compound> {
        &mut self.right
    }

    /// Getter for the direction of the equation
    pub const fn direction(&self) -> &Direction {
        &self.direction
    }

    /// Getter for the equation as text
    pub fn equation(&self) -> &str {
        self.equation.as_ref()
    }

    /// Getter for `delta_h` in kJ
    pub const fn delta_h(&self) -> f32 {
        self.delta_h
    }

    /// Setter for `delta_h` in kJ
    pub fn set_delta_h(&mut self, delta_h: f32) {
        self.delta_h = delta_h;
    }

    /// Getter for `temperature` in degrees Celsius
    pub const fn temperature(&self) -> Option<f32> {
        self.temperature
    }

    /// Setter for `temperature` in degrees Celsius
    pub fn set_temperature(&mut self, temperature: f32) {
        self.temperature = Some(temperature);
    }

    /// Getter for the `volume` equation in litres
    pub const fn volume(&self) -> Option<f32> {
        self.volume
    }

    /// Setter for `volume` in Litres
    pub fn set_volume(&mut self, volume: f32) {
        self.volume = Some(volume);
    }
}

/// The result of [`Equation::reaction_quotient`].
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ReactionQuotient {
    /// The product of the concentration of the reactants was zero
    LeftZero,
    /// The product of the concentration of the products was zero
    RightZero,
    /// Both the reactant and products had product concentrations of zero
    BothSidesZero,
    /// The reaction quotient
    Val(f32),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn too_much_input() {
        assert_eq!(
            Equation::new("H2 + O2 -> H2O-2wowthisistoolong"),
            Err(EquationError::TooMuchInput(
                "-2wowthisistoolong".to_string()
            ))
        )
    }

    #[test]
    fn mol_ratio_basic() {
        let eq = Equation::new("2O2 + H2 -> H2O").unwrap();
        assert_eq!(eq.mol_ratio(), (3, 1));
    }

    #[test]
    fn mol_ratio_states() {
        let eq = Equation::new("2O2(g) + H2(g) -> H2O(l)").unwrap();
        assert_eq!(eq.mol_ratio(), (3, 0));
    }

    #[test]
    fn mol_ratio_more_states() {
        // doesn't matter how bad an equation this is...
        let eq = Equation::new("4FeH3(s) + 3O2(g) -> 2Fe2O3(s) + 6H2(g)").unwrap();
        assert_eq!(eq.mol_ratio(), (3, 6));
    }

    #[test]
    fn mol_ratio_no_aq() {
        // doesn't matter how bad an equation this is...
        // this one is _really_ bad though...
        let eq = Equation::new("Fe(s) + K2(s) -> FeK(l)").unwrap();
        assert_eq!(eq.mol_ratio(), (1, 1));
    }

    #[test]
    fn uniq_elements_no_repeat() {
        let eq = Equation::new("2O2 + H2 -> 2H2O").unwrap();
        assert_eq!(eq.uniq_elements().len(), 2);
    }

    #[test]
    fn uniq_elements_repeat() {
        let eq = Equation::new("C + 2H2O -> CO2 + 2H2").unwrap();
        assert_eq!(eq.uniq_elements().len(), 3);
    }

    #[test]
    fn uniq_long() {
        let eq =
            Equation::new("3(NH4)2SO4(aq) + Fe3(PO4)2(s) <- 2(NH4)3PO4(aq) + 3FeSO4(aq)").unwrap();
        assert_eq!(eq.uniq_elements().len(), 6);
    }

    #[test]
    fn num_compounds_short() {
        let eq = Equation::new("O2 + 2H2 -> 2H2O").unwrap();
        assert_eq!(eq.num_compounds(), 3);
    }

    #[test]
    fn num_compounds_long() {
        let eq =
            Equation::new("3(NH4)2SO4(aq) + Fe3(PO4)2(s) <- 2(NH4)3PO4(aq) + 3FeSO4(aq)").unwrap();
        assert_eq!(eq.num_compounds(), 4);
    }

    #[test]
    #[cfg(feature = "balance")]
    fn is_balanced_correct() {
        let eq = Equation::new("C + 2H2O -> CO2 + 2H2").unwrap();
        assert!(eq.is_balanced());
    }

    #[test]
    #[cfg(feature = "balance")]
    fn is_balanced_incorrect() {
        let eq = Equation::new("Mg(OH)2 + Fe -> Fe(OH)3 + Mg").unwrap();
        assert!(!eq.is_balanced());
    }
}