7 releases

0.9.6 Aug 6, 2024
0.9.5 Aug 6, 2024
0.9.3 Jul 12, 2024

#146 in Science

MIT/Apache

52KB
1.5K SLoC

chemp is a tool for parsing chemical formulas.

takes molecular formula of substance. for given correct formula it extracts information about compound:

  • chemical composition
  • molar mass
  • mass percent of each element in composition
Usage
use chemp;

let compound = chemp::parse("MgSO4*7H2O").unwrap();

// getters for each component of compound
compound.components().values().for_each(|component| {
    // get mass of all atoms of element in compound
    component.mass();

    // get percent of component mass to compound mass
    component.mass_percent();

    // get atoms count of element in compound
    component.atoms_count();

    // get chemical element symbol
    component.chemical_element().symbol();

    // get chemical element atomic weight
    component.chemical_element().atomic_weight();
});

// list of elements in order they parsed
// nested groups are flattened
compound.composition().iter().for_each(|element| {
    // get subscript of element
    element.subscript();

    // get chemical element symbol
    element.chemical_element().symbol();

    // get chemical element atomic weight
    element.chemical_element().atomic_weight();     
});

// get molar mass of compound
compound.molar_mass();

println!("compound: {:#?}", compound);

// compound: Compound {
//     composition: [
//         Element {
//             chemical_element: Magnesium,
//             subscript: 1,
//         },
//         Element {
//             chemical_element: Sulfur,
//             subscript: 1,
//         },
//         Element {
//             chemical_element: Oxygen,
//             subscript: 4,
//         },
//         Element {
//             chemical_element: Hydrogen,
//             subscript: 14,
//         },
//         Element {
//             chemical_element: Oxygen,
//             subscript: 7,
//         },
//     ],
//     components: {
//         "O": Component {
//             chemical_element: Oxygen,
//             atoms_count: 11,
//             mass_percent: 71.40498,
//         },
//         "S": Component {
//             chemical_element: Sulfur,
//             atoms_count: 1,
//             mass_percent: 13.007879,
//         },
//         "Mg": Component {
//             chemical_element: Magnesium,
//             atoms_count: 1,
//             mass_percent: 9.861401,
//         },
//         "H": Component {
//             chemical_element: Hydrogen,
//             atoms_count: 14,
//             mass_percent: 5.725739,
//         },
//     },
//     molar_mass: 246.466,
// }
The parser grammar
substance = coefficient? component+ hydrate?
component = element | group
group = '(' component+ ')' subscript?
element = symbol subscript?
hydrate = '*' coefficient? water
symbol = uppercased | uppercased lowercased
subscript = digit+
coefficient = digit+
water = 'H2O'
uppercased = {'A'..'Z'}
lowercased = {'a'..'z'}
digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

Dependencies

~48KB