chemical_formula/lib.rs
1// chemical-formula-rs
2// Copyright (c) 2024 Ameyanagi <contact@ameyanagi.com>
3//
4// Licensed under the Apache License, Version 2.0
5// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
6// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. All files in the project carrying such notice may not be copied,
8// modified, or distributed except according to those terms.
9
10//! # chemical-formula-rs
11//!
12//! This crate provides a simple way to parse and manipulate chemical formulas including weight percent and nested formulas.
13//!
14//! The initial motivation was to parse a formula such as `Pt5wt%/SiO2`, which are heavily used annotation in the field of Heterogeneous catalysis.
15//! Another motivatation was to parse a formula that is nested such as `(Pt5wt%/SiO2)50wt%(CeO2)50wt%`, which can be used to describe a composite material.
16//! We also provide a way simple API to convert between molecular formula and weight percent.
17//!
18//! ## Installation
19//!
20//! Please use [cargo-edit](https://crates.io/crates/cargo-edit) to always add the latest version of this library:
21//!
22//! ```cmd
23//! cargo add chemical-formula
24//! ```
25//!
26//! ## Example
27//!
28//! ```rust
29//! use chemical_formula::prelude::*;
30//!
31//! fn main() {
32//! let formula = parse_formula("H2O").unwrap();
33//!
34//! println!("Orignal formula: {:?}", formula);
35//! // Orignal formula: ChemicalFormula { element: {O, H}, stoichiometry: {H: 2.0, O: 1.0}, wt_percent: {} }
36//!
37//! println!("Molecular weight: {:?}", formula.molecular_weight());
38//! // Molecular weight: Ok(18.015)
39//!
40//! println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
41//! // Wt%: ChemicalFormula { element: {O, H}, stoichiometry: {}, wt_percent: {H: 11.19067443796836, O: 88.80932556203165} }
42//!
43//! let formula = parse_formula("Pt5wt%/SiO2").unwrap();
44//!
45//! println!("Orignal formula: {:?}", formula);
46//! // Orignal formula: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {Si: 1.0, O: 2.0}, wt_percent: {Pt: 5.0} }
47//!
48//! println!(
49//! "Molecular Formula: {:?}",
50//! formula.to_molecular_formula().unwrap()
51//! );
52//! // Molecular Formula: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {Si: 1.0, Pt: 0.016209751480873558, O: 2.0}, wt_percent: {} }
53//!
54//! println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
55//! // Wt%: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {}, wt_percent: {Pt: 5.0, Si: 44.406487692026026, O: 50.59351230797397} }
56//!
57//! let formula = parse_formula("(Pt5wt%/SiO2)50wt%(CeO2)50wt%").unwrap();
58//!
59//! println!("Orignal formula: {:?}", formula);
60//! // Orignal formula: ChemicalFormula { element: {Si, Ce, Pt, O}, stoichiometry: {}, wt_percent: {Si: 22.203243846013017, O: 34.59233931398559, Pt: 2.5000000000000004, Ce: 40.70441684000139} }
61//!
62//! println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
63//! // Wt%: ChemicalFormula { element: {Si, Ce, Pt, O}, stoichiometry: {}, wt_percent: {Si: 22.203243846013017, O: 34.59233931398559, Pt: 2.5000000000000004, Ce: 40.70441684000139} }
64//! }
65//! ```
66//!
67//! ## License
68//!
69//! Licensed under either of
70//!
71//! - Apache License, Version 2.0
72//! ([LICENSE-APACHE](https://github.com/Ameyanagi/chemical-formula-rs/blob/main/LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
73//! - MIT license
74//! ([LICENSE-MIT](https://github.com/Ameyanagi/chemical-formula-rs/blob/main/LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
75//!
76//! at your option.
77pub mod element;
78pub mod parser;
79pub mod prelude;