chem_eq/
lib.rs

1//! # `chem-eq`
2//!
3//! `chem-eq` parses chemical equations into elements, mol ratio,
4//! direction of reaction and more.
5//!
6//! The main type is [`Equation`]
7//!
8
9#![cfg_attr(docsrs, feature(doc_cfg))]
10#![deny(missing_docs)]
11
12use std::str::FromStr;
13
14pub use crate::{
15    compound::Compound,
16    element::Element,
17    equation::{Equation, ReactionQuotient},
18};
19
20#[cfg(feature = "balance")]
21#[cfg_attr(docsrs, doc(cfg(feature = "balance")))]
22pub mod balance;
23mod compound;
24mod display;
25mod element;
26mod equation;
27pub mod error;
28mod parse;
29
30/// Avagadro's number, approximately equal to 6.02 * 10^23
31pub const AVAGADRO_CONSTANT: f64 = 6.02214e23;
32
33/// The state of matter of a Compound. Including:
34/// - solid
35/// - liquid
36/// - gas
37/// - aqueous
38#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
39#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40pub enum State {
41    /// Solid
42    Solid,
43    /// Liquid
44    Liquid,
45    /// Gas
46    Gas,
47    /// Aqueous
48    #[default]
49    Aqueous,
50}
51
52impl FromStr for State {
53    type Err = &'static str;
54
55    fn from_str(s: &str) -> Result<Self, Self::Err> {
56        match s {
57            "s" => Ok(Self::Solid),
58            "l" => Ok(Self::Liquid),
59            "g" => Ok(Self::Gas),
60            "aq" => Ok(Self::Aqueous),
61            _ => Err("Invalid state."),
62        }
63    }
64}
65
66/// Direction a reaction is heading in.
67/// - left
68/// - right
69/// - reversible
70#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
71#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72pub enum Direction {
73    /// Products are on the left, reactants are on the right.
74    Left,
75    /// Products are on the right, reactants on the left.
76    #[default]
77    Right,
78    /// Reaction can work in both directions.
79    Reversible,
80}
81
82impl FromStr for Direction {
83    type Err = &'static str;
84
85    fn from_str(s: &str) -> Result<Self, Self::Err> {
86        match s {
87            "<-" => Ok(Self::Left),
88            "->" => Ok(Self::Right),
89            "<->" => Ok(Self::Reversible),
90            _ => Err("Invalid direction."),
91        }
92    }
93}