mathml_rs/structs/
numbers.rs

1use std::str::FromStr;
2
3#[derive(Debug, Clone)]
4pub enum NumType {
5    Real,
6    Integer,
7    Rational,
8    ComplexCartesian,
9    ComplexPolar,
10    Constant,
11    ENotation,
12}
13
14impl FromStr for NumType {
15    type Err = ();
16
17    fn from_str(s: &str) -> Result<NumType, ()> {
18        match s {
19            "integer" => Ok(NumType::Integer),
20            "real" | "double" => Ok(NumType::Real),
21            "e-notation" => Ok(NumType::ENotation),
22            "rational" => Ok(NumType::Rational),
23            "complex-cartesian" => Ok(NumType::ComplexCartesian),
24            "complex-polar" => Ok(NumType::ComplexPolar),
25            "constant" => Ok(NumType::Constant),
26            _ => Err(()),
27        }
28    }
29}
30
31#[derive(Debug, Clone)]
32pub enum Number {
33    Real(f64),
34    Integer(i32),
35    Rational(i64, i64),
36    ComplexCartesian(f64, f64),
37    ComplexPolar(f64, f64),
38    Constant(String),
39    ENotation(f64, i64),
40}
41
42impl Eq for Number {}
43impl PartialEq for Number {
44    fn eq(&self, other: &Self) -> bool {
45        use Number::*;
46        match (self, other) {
47            (Real(r), Real(r2)) => approx::abs_diff_eq!(r, r2),
48            (Integer(r1), Integer(r2)) => r1 == r2,
49            (Rational(a, b), Rational(c, d)) => (a == c) && (b == d),
50            (ComplexPolar(a, b), ComplexPolar(c, d))
51            | (ComplexCartesian(a, b), ComplexCartesian(c, d)) => {
52                approx::abs_diff_eq!(a, c) && approx::abs_diff_eq!(b, d)
53            }
54            (Constant(a), Constant(b)) => a == b,
55            (ENotation(a, b), ENotation(c, d)) => a == c && b == d,
56            _ => false,
57        }
58    }
59}