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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::ops;
use crate::{Result, Error};

/// Representation of a roman digit
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Digit {
    I,
    V,
    X,
    L,
    C,
    D,
    M
}

impl Digit {
    /// Tries to converts a value that implements `Into<u32>` into a single roman digit
    ///
    /// # Examples
    /// ```rust
    /// # use septem::prelude::*;
    /// # use septem::*;
    ///
    /// let v: Digit = Digit::from_int(5u8).unwrap();
    /// assert_eq!(Digit::V, v);
    /// ```
    ///
    /// Returns `Digit` , or an `septem::Error`
    pub fn from_int<T: Into<u32>>(num: T) -> Result<Digit> {
        let num: u32 = num.into();
        use self::Digit::*;
        match num {
            1 => Ok(I),
            5 => Ok(V),
            10 => Ok(X),
            50 => Ok(L),
            100 => Ok(C),
            500 => Ok(D),
            1000 => Ok(M),
            _ => Err(Error::InvalidNumber(num))
        }
    }
}

impl Digit {
    /// Tries to converts a char into a single roman digit
    ///
    /// # Examples
    /// ```rust
    /// # use septem::prelude::*;
    /// # use septem::*;
    ///
    /// let v: Digit = Digit::from_char('v').unwrap();
    /// assert_eq!(Digit::V, v);
    /// ```
    ///
    /// Returns `Digit` , or an `septem::Error`
    pub fn from_char(c: char) -> Result<Digit> {
        use self::Digit::*;
        match c.to_uppercase().next() {
            Some('I') => Ok(I),
            Some('V') => Ok(V),
            Some('X') => Ok(X),
            Some('L') => Ok(L),
            Some('C') => Ok(C),
            Some('D') => Ok(D),
            Some('M') => Ok(M),
            _ => Err(Error::InvalidDigit(c))
        }
    }

    /// Tries to converts a byte into a single roman digit
    ///
    /// # Examples
    /// ```rust
    /// # use septem::prelude::*;
    /// # use septem::*;
    ///
    /// let v: Digit = Digit::from_byte(b'v').unwrap();
    /// assert_eq!(Digit::V, v);
    /// ```
    ///
    /// Returns `Digit` , or an `septem::Error`
    pub fn from_byte(b: u8) -> Result<Digit> {
        use self::Digit::*;
        match b {
            b'I' | b'i'=> Ok(I),
            b'V' | b'v'=> Ok(V),
            b'X' | b'x'=> Ok(X),
            b'L' | b'l'=> Ok(L),
            b'C' | b'c'=> Ok(C),
            b'D' | b'd'=> Ok(D),
            b'M' | b'm'=> Ok(M),
            _ => Err(Error::InvalidDigit(b.into()))
        }
    }

    pub fn to_lowercase(self) -> char {
        use self::Digit::*;
        match self {
            I => 'i',
            V => 'v',
            X => 'x',
            L => 'l',
            C => 'c',
            D => 'd',
            M => 'm'
        }
    }

    pub fn to_uppercase(self) -> char {
        use self::Digit::*;
        match self {
            I => 'I',
            V => 'V',
            X => 'X',
            L => 'L',
            C => 'C',
            D => 'D',
            M => 'M'
        }
    }
}

unsafe impl Send for Digit {}
unsafe impl Sync for Digit {}

impl From<Digit> for u32 {
    /// Converts from Digit to u32
    fn from(digit: Digit) -> u32 {
        *digit
    }
}

impl<'a> From<&'a Digit> for char {
    /// Converts from &Digit to char
    fn from(digit: &'a Digit) -> char {
        digit.to_uppercase()
    }
}

impl Display for Digit {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "{}", char::from(self))
    }
}

impl ops::Deref for Digit {
    type Target = u32;

    /// Returns from &Digit to u32
    fn deref(&self) -> &u32 {
        use self::Digit::*;
        match *self {
            I => &1,
            V => &5,
            X => &10,
            L => &50,
            C => &100,
            D => &500,
            M => &1000
        }
    }
}