stoik/
lib.rs

1//! # Stoik
2//! Stoicimetic utilities written in rust.
3//!
4//! To see an example how this is used in the real world, look at the [CLI](TODO)
5//!
6//! ## Example use
7//!
8//! ```
9//! use stoik::formula::Molecule;
10//! use stoik::StoikError;
11//!
12//! let formula = "Rh2(SO4)3";
13//! match Molecule::from_formula(formula) {
14//!     Err(e) => match e {
15//!         StoikError::InvalidToken(loc) => {
16//!             println!("{}",
17//!                 loc.format_msg(formula, "Malformed formula", "Illegal token")
18//!             )
19//!         }
20//!         StoikError::NumberFirst(loc) => println!("{}",
21//!             loc.format_msg(
22//!                 formula,
23//!                 "Malformed formula",
24//!                 "Compound groups cannot start\nwith numbers",
25//!             )
26//!         ),
27//!         StoikError::UnpairedParenthesis(loc) => {
28//!             println!("{}",
29//!                 loc.format_msg(formula, "Malformed formula", "Unpaired parenthesis")
30//!             )
31//!         }
32//!         StoikError::UnpairedBracket(loc) => {
33//!             println!("{}",
34//!                 loc.format_msg(formula, "Malformed formula", "Unpaired bracket")
35//!             )
36//!         }
37//!         e => println!("{e}"),
38//!     },
39//!     Ok(mol) => {
40//!         println!("{formula} contains:")
41//!         //...
42//!     }
43//! }
44//! ```
45#![warn(missing_docs)]
46
47mod err;
48pub mod formula;
49
50pub use err::StoikError;
51
52#[cfg(test)]
53mod tests {
54    use std::collections::HashMap;
55
56    use crate::formula::Molecule;
57
58    #[test]
59    fn overall_test() {
60        let mol = Molecule::from_formula("5(H2O)3((FeW)5CrMo2V)6CoMnSi");
61        assert!(mol.is_ok());
62        let mol = mol.unwrap();
63        assert_eq!(mol.moles, 5);
64        assert_eq!(
65            mol.get_map(),
66            HashMap::from([
67                ("V".to_string(), 30,),
68                ("Fe".to_string(), 150,),
69                ("Mo".to_string(), 60,),
70                ("Cr".to_string(), 30,),
71                ("Mn".to_string(), 5,),
72                ("Co".to_string(), 5,),
73                ("Si".to_string(), 5,),
74                ("O".to_string(), 15,),
75                ("H".to_string(), 30,),
76                ("W".to_string(), 150,),
77            ])
78        );
79    }
80}