chem_eq/
display.rs

1use std::fmt::Display;
2
3use crate::{compound::Compound, Direction, Element, Equation, State};
4
5impl Display for Equation {
6    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7        write!(f, "{}", self.equation)
8    }
9}
10
11impl Display for Compound {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        let state = self
14            .state
15            .as_ref()
16            .map_or_else(Default::default, |s| format!("{}", s));
17        let mut elms = String::default();
18        for el in &self.elements {
19            elms.push_str(el.to_string().as_str());
20        }
21        write!(f, "{}{}{}", self.coefficient, elms, state)
22    }
23}
24
25impl Display for Element {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        write!(f, "{}{}", self.symbol(), self.count)
28    }
29}
30
31impl Display for State {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        let s = match self {
34            Self::Solid => "s",
35            Self::Liquid => "l",
36            Self::Gas => "g",
37            Self::Aqueous => "aq",
38        };
39        write!(f, "({})", s)
40    }
41}
42
43impl Display for Direction {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        let s = match self {
46            Self::Left => "<-",
47            Self::Right => "->",
48            Self::Reversible => "<->",
49        };
50        write!(f, "{}", s)
51    }
52}