chem-parse

Crates.iochem-parse
lib.rschem-parse
version0.3.0
sourcesrc
created_at2022-03-30 18:13:05.937404
updated_at2022-05-19 19:12:08.081292
descriptionA parser for simple chemical forumulas.
homepage
repositoryhttps://github.com/Allan-Jacobs/rust-chemistry-parser
max_upload_size
id559332
size28,896
Allan Jacobs (Allan-Jacobs)

documentation

README

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(())
}
Commit count: 41

cargo fmt