3 releases (breaking)

0.3.0 May 19, 2022
0.2.0 Apr 22, 2022
0.1.0 Mar 30, 2022

#234 in Biology

MIT license

24KB
568 lines

chem-parse Crates.io

A parser for simple chemical formulas.

Get Started

Add the dependency to your Cargo.toml file.

[dependencies]
chem-parse = "0.1.0"

Parse a forumula unit

use std::error::Error;
use chem_parse::parse;

fn main() -> Result<(), Box<dyn Error>> {
    let string = String::from("Fe2O3");
    let ast = parse(string)?;
    // Ast: ForumulaUnit(1, [Element(2, "Fe"), Element(3, "O")])
    println!("Ast: {:?}", ast);
    Ok(())
}

Parse an equation

use std::error::Error;
use chem_parse::parse;

fn main() -> Result<(), Box<dyn Error>> {
    let string = String::from("4Fe+3O2->2Fe2O");
    let ast = parse(string)?;
    // Node: comment broken up into multiple lines to save space
    // Ast: Equation(
    //   Reactants([ForumulaUnit(4, [Element(1, "Fe")]), ForumulaUnit(3, [Element(2, "O")])]),
    //   Products([ForumulaUnit(2, [Element(2, "Fe"), Element(1, "O")])])
    // )
    println!("Ast: {:?}", ast);
    Ok(())
}

No runtime deps