chem_eq/
error.rs

1//! Error types for `chem-eq`
2
3use crate::parse::util::Error;
4
5/// Errors type for issues with chemical equations
6#[derive(thiserror::Error, Clone, PartialEq, Eq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub enum EquationError {
9    /// The string couldn't be parsed into a chemical equation
10    #[error("couldn't parse the equation:\n{0}")]
11    #[cfg_attr(feature = "serde", serde(skip))]
12    ParsingError(Error<String>),
13    /// The equation is not valid. Eg: There are different elements on each side of the equation
14    #[error("this equation is not valid")]
15    IncorrectEquation,
16    /// The compound was parsed, but there was remaining input
17    #[error("too much input, remaining: {0:?}")]
18    TooMuchInput(String),
19}
20
21impl std::fmt::Debug for EquationError {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        std::fmt::Display::fmt(self, f)
24    }
25}
26
27// done for rustdoc
28#[cfg(doc)]
29#[allow(unused)]
30use crate::Compound;
31
32/// Errors for parsing a [`Compound`]
33#[derive(thiserror::Error, Clone, PartialEq, Eq)]
34#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
35pub enum CompoundError {
36    /// The input couldn't be parsed into a compound
37    #[error("couldn't parse the compound:\n{0}")]
38    #[cfg_attr(feature = "serde", serde(skip))]
39    ParsingError(Error<String>),
40    /// The compound was parsed, but there was remaining input
41    #[error("too much input, remaining: {0:?}")]
42    TooMuchInput(String),
43}
44
45impl std::fmt::Debug for CompoundError {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        std::fmt::Display::fmt(self, f)
48    }
49}
50
51// done for rustdoc
52#[cfg(doc)]
53#[allow(unused)]
54use crate::Equation;
55
56/// Error for [`Equation::set_concentrations`]
57#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
58#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
59pub enum ConcentrationError {
60    /// Slice length doesn't match [`Equation::num_compounds`]
61    #[error("slice not right size")]
62    WrongSliceSize,
63    /// A concentration value was NAN which is invalid
64    #[error("concentration value was NAN")]
65    NAN,
66}
67
68/// Error for [`Equation::set_concentration_by_name`] and [`Equation::get_concentration_by_name`]
69#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
71pub enum ConcentrationNameError {
72    /// Requested compound couldn't be found
73    #[error("compound not found")]
74    NotFound,
75    /// Concentration value was NAN, which is invalid
76    #[error("concentration value was NAN")]
77    NAN,
78}
79
80// done for rustdoc
81#[cfg(all(doc, feature = "balance"))]
82#[allow(unused)]
83use crate::balance::EquationBalancer;
84
85/// Error for [`EquationBalancer::balance`]
86#[cfg(feature = "balance")]
87#[cfg_attr(docsrs, doc(cfg(feature = "balance")))]
88#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)]
89#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
90pub enum BalanceError {
91    /// The equation was invalid
92    #[error("the equation is invalid")]
93    InvalidEquation,
94    /// The equation could not be balanced
95    #[error("equation could not be balanced")]
96    Infeasable,
97}
98
99#[cfg(doc)]
100#[allow(unused)]
101use crate::Element;
102
103/// Error for constructing an [`Element`]
104#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
105#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
106pub enum ElementError {
107    /// This element is not a member of the periodic table
108    #[error("Element was not part of periodic table: {0}")]
109    NotInPeriodicTable(String),
110    /// The input could no be parsed into an element
111    #[error("The element could not be parsed")]
112    #[cfg_attr(feature = "serde", serde(skip))]
113    ParseError(Error<String>),
114    /// The element was parsed, but there was remaining input
115    #[error("too much input, remaining: {0:?}")]
116    TooMuchInput(String),
117}