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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
mod ladder;

use crate::{unit::RomanUnitIterator, Error, Result};
use core::{
    fmt::{self, Display},
    num::NonZeroU16,
    str::FromStr,
};

/// A Roman numeral.

///

/// This struct stores the value of a numeral as an [`NonZeroU16`] but provides

/// for Roman-style formatting.

#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Roman(NonZeroU16);

impl Roman {
    /// Creates a `Roman` value based on a [`u16`].

    ///

    /// This function will return `None` if the value supplied is outside the

    /// acceptable range of `1..=4999`, because numbers outside that range

    /// cannot be appropriately formatted using the seven standard numerals.

    pub fn new(n: u16) -> Result<Roman> {
        match NonZeroU16::new(n) {
            Some(n) if n.get() <= 4999 => Ok(Roman(n)),
            _ => Err(Error::OutOfRange(n)),
        }
    }

    /// Formats a [`Roman`] value as an uppercase Roman numeral.

    ///

    /// ## Examples

    ///

    /// ```

    /// use xvii::Roman;

    /// assert_eq!(Roman::new(42).unwrap().to_uppercase(), "XLII");

    /// ```

    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    pub fn to_uppercase(self) -> String {
        let mut current = self.0.get();
        let mut buf = String::new();

        for entry in ladder::VALUES {
            while current >= entry.value {
                current -= entry.value;
                buf += entry.upper;
            }
        }

        buf
    }

    /// Formats a [`Roman`] value as a lowercase Roman numeral.

    ///

    /// ## Examples

    ///

    /// ```

    /// use xvii::Roman;

    /// assert_eq!(Roman::new(42).unwrap().to_lowercase(), "xlii");

    /// ```

    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    pub fn to_lowercase(self) -> String {
        let mut current = self.0.get();
        let mut buf = String::new();

        for entry in ladder::VALUES {
            while current >= entry.value {
                current -= entry.value;
                buf += entry.lower;
            }
        }

        buf
    }

    /// Returns a [`RomanFormatter`] which lazily formats a `self` value as a lowercase or uppercase Roman numeral depending of `style`.

    ///

    /// ## Examples

    ///

    /// ```

    /// use xvii::{Roman, Style};

    ///

    /// let value = Roman::new(12).unwrap();

    /// assert_eq!(format!("{}", value.format(Style::Upper)), "XII");

    /// assert_eq!(value.format(Style::Lower).to_string(), "xii"); // `format!("{}")` and `.to_string()` are the same thing

    /// ```

    pub const fn format(&self, style: Style) -> RomanFormatter {
        RomanFormatter {
            style,
            value: self.0,
        }
    }

    /// Returns value of this `Roman` numeral.

    ///

    /// ## Examples

    ///

    /// ```rust

    /// let roman = xvii::Roman::new(42).unwrap();

    /// assert_eq!(roman.value(), 42);

    /// ```

    pub const fn value(self) -> u16 {
        self.0.get()
    }

    /// Returns the inner value.

    ///

    /// ## Examples

    ///

    /// ```rust

    /// let roman = xvii::Roman::new(42).unwrap();

    /// assert_eq!(roman.into_inner(), std::num::NonZeroU16::new(42).unwrap());

    /// ```

    pub const fn into_inner(self) -> NonZeroU16 {
        self.0
    }
}

/// Style of formatting — lowercase or uppercase.

#[derive(Debug, Copy, Clone)]
pub enum Style {
    /// Lowercase formatting. E.g.: `xvii`.

    Lower,
    /// Uppercase formatting. E.g.: `XVII`.

    Upper,
}

/// Lazy roman formatter.

///

/// This struct is created by [`format`](Roman::format) method.

#[derive(Debug, Copy, Clone)]
pub struct RomanFormatter {
    style: Style,
    value: NonZeroU16,
}

impl Display for RomanFormatter {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut current = self.value.get();

        for entry in ladder::VALUES {
            while current >= entry.value {
                match self.style {
                    Style::Lower => f.write_str(entry.lower)?,
                    Style::Upper => f.write_str(entry.upper)?,
                }
                current -= entry.value;
            }
        }

        Ok(())
    }
}

impl FromStr for Roman {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let sum = RomanUnitIterator::new(s)
            .try_fold(0, |acc, r| r?.checked_add(acc).ok_or(Error::Overflow))?;
        Roman::new(sum)
    }
}

impl Display for Roman {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.format(Style::Upper).fmt(f)
    }
}

#[cfg(test)]
mod tests {
    use crate::Error;

    use super::Roman;

    #[test]
    fn mcmlxxxiv_equals_1984() {
        assert_eq!("MCMLXXXIV", Roman::new(1984).unwrap().to_string());
    }

    #[test]
    fn mmdxxix_equals_2529() {
        assert_eq!("MMDXXIX", Roman::new(2529).unwrap().to_string());
    }

    #[test]
    fn mmcmxcix_equals_2999() {
        assert_eq!("MMCMXCIX", Roman::new(2999).unwrap().to_string());
    }

    #[test]
    fn mmmcmxcix_value_equals_3999() {
        assert_eq!("MMMCMXCIX", Roman::new(3999).unwrap().to_string());
    }

    #[test]
    fn max_value_equals_4999() {
        assert_eq!("MMMMCMXCIX", Roman::new(4999).unwrap().to_string());
    }

    #[test]
    fn mmmmcmxcix_parses_as_4999() {
        let result: Roman = "MMMMCMXCIX".parse().unwrap();
        assert_eq!(4999, result.value());
    }

    #[test]
    fn overflow() {
        assert_eq!(
            Err(Error::Overflow),
            "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMCM".parse::<Roman>()
        );

        assert_eq!(
            Err(Error::Overflow),
            "CMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCM".parse::<Roman>()
        );

        assert_eq!(
            Err(Error::Overflow),
            "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCMCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD".parse::<Roman>()
        );

        assert_eq!(
            Err(Error::Overflow),
            "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM".parse::<Roman>()
        );

        assert_eq!(
            Err(Error::Overflow),
            "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM".parse::<Roman>()
        );
    }
}