Skip to content

Latest commit

 

History

History
126 lines (84 loc) · 6.61 KB

README.md

File metadata and controls

126 lines (84 loc) · 6.61 KB

chemical-formula-rs

Lib.rs Crates.io Docs.rs

Rust 1.74

Crates.io - License

GitHub open issues open pull requests good first issues

This crate provides a simple way to parse and manipulate chemical formulas including weight percent and nested formulas.

The initial motivation was to parse a formula such as Pt5wt%/SiO2, which are heavily used annotation in the field of Heterogeneous catalysis. 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. We also provide a way simple API to convert between molecular formula and weight percent.

Installation

Please use cargo-edit to always add the latest version of this library:

cargo add chemical-formula

Example

use chemical_formula::prelude::*;

fn main() {
    let formula = parse_formula("H2O").unwrap();

    println!("Orignal formula: {:?}", formula);
    // Orignal formula: ChemicalFormula { element: {O, H}, stoichiometry: {H: 2.0, O: 1.0}, wt_percent: {} }

    println!("Molecular weight: {:?}", formula.molecular_weight());
    // Molecular weight: Ok(18.015)

    println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
    // Wt%: ChemicalFormula { element: {O, H}, stoichiometry: {}, wt_percent: {H: 11.19067443796836, O: 88.80932556203165} }

    let formula = parse_formula("Pt5wt%/SiO2").unwrap();

    println!("Orignal formula: {:?}", formula);
    // Orignal formula: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {Si: 1.0, O: 2.0}, wt_percent: {Pt: 5.0} }

    println!(
        "Molecular Formula: {:?}",
        formula.to_molecular_formula().unwrap()
    );
    // Molecular Formula: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {Si: 1.0, Pt: 0.016209751480873558, O: 2.0}, wt_percent: {} }

    println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
    // Wt%: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {}, wt_percent: {Pt: 5.0, Si: 44.406487692026026, O: 50.59351230797397} }

    let formula = parse_formula("(Pt5wt%/SiO2)50wt%(CeO2)50wt%").unwrap();

    println!("Orignal formula: {:?}", formula);
    // Orignal formula: ChemicalFormula { element: {Si, Ce, Pt, O}, stoichiometry: {}, wt_percent: {Si: 22.203243846013017, O: 34.59233931398559, Pt: 2.5000000000000004, Ce: 40.70441684000139} }

    println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
    // Wt%: ChemicalFormula { element: {Si, Ce, Pt, O}, stoichiometry: {}, wt_percent: {Si: 22.203243846013017, O: 34.59233931398559, Pt: 2.5000000000000004, Ce: 40.70441684000139} }
}

Comparison with other crates

There are other crates that deals with chemical formulas, but none of them can handle mixture of stoiometry and weight percent annotation. Following is a comparison of this crate with other crates.

Crate Can parse nested paranthesis Can parse weight percentage
chemical_formula (this work) Yes Yes
ATOMIO Yes No
acetylene_parser Yes No
chemical_elements Yes No
chem-parse ? No

For chem-parse, I couldn't check if it can parse nested paranthesis or not, since I failed to compile it.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

See CONTRIBUTING for more information.

Versioning

chemical-formula-rs strictly follows Semantic Versioning 2.0.0 with the following exceptions:

  • The minor version will not reset to 0 on major version changes (except for v1).
    Consider it the global feature level.
  • The patch version will not reset to 0 on major or minor version changes (except for v0.1 and v1).
    Consider it the global patch level.

This includes the Rust version requirement specified above.
Earlier Rust versions may be compatible, but this can change with minor or patch releases.

Which versions are affected by features and patches can be determined from the respective headings in CHANGELOG.md.