kalendarium 0.1.0

A library for converting Arabic numerals into Roman numerals and modern dates into the Kalendarium Romanum
Documentation
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! # kalendarium Roman numeral implementation
//!
//! A library for converting Arabic numerals to Classical Attic numerals
//!
//! TODO: implement Apostrophus (vs Vinculum) for numbers greater than 5000
//!       https://en.wikipedia.org/wiki/Roman_numerals#Other_additive_forms
//! TODO: implement Additive (vs Subtractive) for -4 and -9
//!       e.g. XIIII and MDCCCC for 14 an 1900 respectively
//!
//! ## License
//!
//! GNU GPL 3

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
#![warn(clippy::std_instead_of_core)]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]

use crate::{OutOfRangeError, Result};
use core::fmt;

/// A Roman numeral
///
/// Values from 0 to 4,999,9999 are supported
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct RomanNumeral(pub u32);

impl RomanNumeral {
    /// Creates a ``RomanNumeral`` for any value in range.
    /// Requires ``value`` to be less than 5,000,000. 0 (ZERO) is acceptable.
    ///
    /// Example
    /// -------
    ///
    /// .. code-block:: rust
    ///
    //     let answer: RomanNumeral = RomanNumeral::new(42).unwrap();
    //     assert_eq!("XLII", answer.to_uppercase());
    ///
    pub const fn new(value: u32) -> Result<Self, OutOfRangeError> {
        if value <= 4_999_999 {
            // SAFETY: 0 <= value <= 4,999,999
            Ok(RomanNumeral(value))
        } else {
            Err(OutOfRangeError)
        }
    }

    /// Return the value of this ``RomanNumeral`` as a ``u32``.
    ///
    /// Example
    /// -------
    ///
    /// .. code-block:: rust
    ///
    ///    let answer: RomanNumeral = RomanNumeral::new(42)?;
    ///    assert_eq!(answer.as_u32(), 42_u32);
    ///
    #[must_use]
    pub const fn as_u32(self) -> u32 {
        self.0
    }

    /// Converts a ``RomanNumeral`` to an uppercase string.
    ///
    /// Example
    /// -------
    ///
    /// .. code-block:: rust
    ///
    ///    let answer: RomanNumeral = RomanNumeral::new(616)?;
    ///    assert_eq!("DCXVI", answer.to_uppercase());
    ///
    #[must_use]
    #[cfg(feature = "std")]
    pub fn to_uppercase(&self) -> String {
        let mut out = String::new();
        if self.0 == 0 {
            out.push_str(&"N".to_string());
        } else {
            out.push_str(&Self::arabic_to_roman(self.0, true));
        }
        out
    }
    ///
    /// Converts a ``RomanNumeral`` to a lowercase string.
    ///
    /// Example
    /// -------
    ///
    /// .. code-block:: rust
    ///
    ///    let answer: RomanNumeral = RomanNumeral::new(616)?;
    ///    assert_eq!("dcxvi", answer.to_lowercase());
    ///
    #[must_use]
    #[cfg(feature = "std")]
    pub fn to_lowercase(self) -> String {
        let mut out = String::new();
        if self.0 == 0 {
            out.push_str(&"N".to_string());
        } else {
            out.push_str(&Self::arabic_to_roman(self.0, false));
        }
        out
    }

    /// Helper function to repeat a character, needed for II, XX, CCC, etc.
    fn repeat(total: u8, character: &str) -> String {
        let mut count = total;
        let mut out = String::new();
        while count > 0 {
            out.push_str(character);
            count -= 1;
        }
        out
    }

    /// Helper function to convert an Arabic numeral into a Roman one
    ///
    /// This looks complicated and I know there are more concise solutions that
    /// I've seen in other libraries.
    ///
    /// I am using this one because I derived it during Covid lockdown in 2020-2021
    /// without consulting any other solutions. What's fun about it now is that I
    /// no longer am sure how exactly it works anymore, and I find beauty in knowing
    /// that at one point my brain solved this problem in this way and now, though
    /// I no longer intuit the solution, it remains a unique creation of my mind.
    ///
    /// In the original rust code, I painstakingly used Latin works for many of the terms.
    /// Here, I have rendered then in English, though I have left the name of the struct
    /// which contains all the conversion as its Latin name, NUMERI, the nominative plural
    /// of the word for number.
    fn arabic_to_roman(num: u32, uppercase: bool) -> String {
        let mut out: String = String::new();
        if num != 0 {
            for (index, basis) in NUMERI.iter().enumerate() {
                let arabic = num / basis.arabic; // Truncating division since both values are ints
                if arabic > 0 {
                    let offset = if basis.arabic.to_string().contains("5") {
                        1
                    } else {
                        0
                    };
                    if num >= NUMERI[index - 1].arabic - NUMERI[index + offset].arabic {
                        if 4000 <= num && num < 10000 {
                            if uppercase {
                                out.push_str(&"IÌ…".to_string());
                            } else {
                                out.push_str(&"iÌ…".to_string());
                            }
                        } else {
                            if uppercase {
                                out.push_str(NUMERI[index + offset].u_latin);
                            } else {
                                out.push_str(NUMERI[index + offset].l_latin);
                            }
                        }
                        out.push_str(NUMERI[index - 1].u_latin);
                        out.push_str(&Self::arabic_to_roman(
                            (num - (NUMERI[index - 1].arabic - NUMERI[index + offset].arabic))
                                % basis.arabic,
                            uppercase,
                        ));
                    } else {
                        if uppercase {
                            out.push_str(&Self::repeat(
                                u8::try_from(arabic).unwrap(),
                                basis.u_latin,
                            ));
                        } else {
                            out.push_str(&Self::repeat(
                                u8::try_from(arabic).unwrap(),
                                basis.l_latin,
                            ));
                        }
                        out.push_str(&Self::arabic_to_roman(
                            u32::try_from(
                                (isize::try_from(num).unwrap()
                                    - isize::try_from(NUMERI[index - 1].arabic).unwrap())
                                .rem_euclid(isize::try_from(basis.arabic).unwrap()),
                            )
                            .unwrap(),
                            uppercase,
                        ));
                    }
                    break;
                }
            }
        }
        out
    }
}
#[cfg(feature = "std")]
impl fmt::Display for RomanNumeral {
    /// Converts a ``RomanNumeral`` to an uppercase string.
    ///
    /// Example
    /// -------
    ///
    /// .. code-block:: rust
    ///
    ///    let answer: RomanNumeral = RomanNumeral::new(42)?;
    ///    assert_eq!("ΜΒ'", answer.to_string());
    ///
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.to_uppercase())
    }
}

/// Struct for holding the conversion values
struct Arabic2RomanStruct<'a> {
    arabic: u32,
    u_latin: &'a str,
    l_latin: &'a str,
}

/// Array of conversions for arabic numerals to Latin numerals
static NUMERI: [Arabic2RomanStruct; 13] = [
    Arabic2RomanStruct {
        arabic: 1000000,
        u_latin: "MÌ…",
        l_latin: "mÌ…",
    },
    Arabic2RomanStruct {
        arabic: 500000,
        u_latin: "DÌ…",
        l_latin: "dÌ…",
    },
    Arabic2RomanStruct {
        arabic: 100000,
        u_latin: "CÌ…",
        l_latin: "cÌ…",
    },
    Arabic2RomanStruct {
        arabic: 50000,
        u_latin: "LÌ…",
        l_latin: "lÌ…",
    },
    Arabic2RomanStruct {
        arabic: 10000,
        u_latin: "XÌ…",
        l_latin: "xÌ…",
    },
    Arabic2RomanStruct {
        arabic: 5000,
        u_latin: "VÌ…",
        l_latin: "uÌ…",
    },
    Arabic2RomanStruct {
        arabic: 1000,
        u_latin: "M",
        l_latin: "m",
    },
    Arabic2RomanStruct {
        arabic: 500,
        u_latin: "D",
        l_latin: "d",
    },
    Arabic2RomanStruct {
        arabic: 100,
        u_latin: "C",
        l_latin: "c",
    },
    Arabic2RomanStruct {
        arabic: 50,
        u_latin: "L",
        l_latin: "l",
    },
    Arabic2RomanStruct {
        arabic: 10,
        u_latin: "X",
        l_latin: "x",
    },
    Arabic2RomanStruct {
        arabic: 5,
        u_latin: "V",
        l_latin: "u",
    },
    Arabic2RomanStruct {
        arabic: 1,
        u_latin: "I",
        l_latin: "i",
    },
];

impl TryFrom<u8> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``u8``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: u8) -> Result<Self, OutOfRangeError> {
        Self::new(u32::from(value))
    }
}

impl TryFrom<u16> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``u16``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: u16) -> Result<Self, OutOfRangeError> {
        Self::new(u32::from(value))
    }
}

impl TryFrom<u32> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``u32``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: u32) -> Result<Self, OutOfRangeError> {
        Self::new(value)
    }
}

impl TryFrom<u64> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``u64``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: u64) -> Result<Self, OutOfRangeError> {
        u32::try_from(value).map_or(Err(OutOfRangeError), Self::new)
    }
}

impl TryFrom<u128> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``u128``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: u128) -> Result<Self, OutOfRangeError> {
        u32::try_from(value).map_or(Err(OutOfRangeError), Self::new)
    }
}

impl TryFrom<usize> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``usize``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: usize) -> Result<Self, OutOfRangeError> {
        u32::try_from(value).map_or(Err(OutOfRangeError), Self::new)
    }
}

impl TryFrom<i8> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``i8``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: i8) -> Result<Self, OutOfRangeError> {
        u32::try_from(value).map_or(Err(OutOfRangeError), Self::new)
    }
}

impl TryFrom<i16> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``i16``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: i16) -> Result<Self, OutOfRangeError> {
        u32::try_from(value).map_or(Err(OutOfRangeError), Self::new)
    }
}

impl TryFrom<i32> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``i32``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: i32) -> Result<Self, OutOfRangeError> {
        u32::try_from(value).map_or(Err(OutOfRangeError), Self::new)
    }
}

impl TryFrom<i64> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``i64``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: i64) -> Result<Self, OutOfRangeError> {
        u32::try_from(value).map_or(Err(OutOfRangeError), Self::new)
    }
}

impl TryFrom<i128> for RomanNumeral {
    type Error = OutOfRangeError;

    /// Creates a ``RomanNumeral`` from an ``i128``.
    ///
    /// Returns ``RomanNumeral`` or ``OutOfRangeError``.
    fn try_from(value: i128) -> Result<Self, OutOfRangeError> {
        u32::try_from(value).map_or(Err(OutOfRangeError), Self::new)
    }
}