1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::MathML;
use std::fmt::{Display, Formatter};

mod display;

/// The [`<mn>`](https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mn) element represents a numeric literal which is normally a sequence of digits with a possible separator (a dot or a comma).
///
/// However, it is also allowed to have arbitrary text in it which is actually a numeric quantity, for example "eleven".
#[derive(Debug, Clone, PartialEq)]
pub struct MathNumber {
    number: String,
}

impl MathNumber {
    /// Creates a new [`MathNumber`] with the given number.
    pub fn new<S>(text: S) -> Self
    where
        S: ToString,
    {
        Self { number: text.to_string() }
    }
}

macro_rules! make_number {
    ($($t:ty),*) => {
        $(
            impl From<$t> for MathNumber {
                fn from(value: $t) -> Self {
                    Self::new(value)
                }
            }
        )*
    };
}

make_number!(i8, i16, i32, i64, i128, isize);
make_number!(u8, u16, u32, u64, u128, usize);
make_number!(f32, f64);

impl MathML {
    /// Creates a new [`MathNumber`] with the given number.
    pub fn number<N>(n: N) -> Self
    where
        N: Into<MathNumber>,
    {
        n.into().into()
    }
}