Crates.io | chem-parse |
lib.rs | chem-parse |
version | 0.3.0 |
source | src |
created_at | 2022-03-30 18:13:05.937404 |
updated_at | 2022-05-19 19:12:08.081292 |
description | A parser for simple chemical forumulas. |
homepage | |
repository | https://github.com/Allan-Jacobs/rust-chemistry-parser |
max_upload_size | |
id | 559332 |
size | 28,896 |
A parser for simple chemical formulas.
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(())
}