File: README.md

package info (click to toggle)
roman-numerals 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 236 kB
  • sloc: python: 393; sh: 6; makefile: 2
file content (84 lines) | stat: -rw-r--r-- 2,245 bytes parent folder | download
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
# roman-numerals

A library for manipulating well-formed Roman numerals.

Integers between 1 and 3,999 (inclusive) are supported.
Numbers beyond this range will return an ``OutOfRangeError``.

The classical system of roman numerals requires that
the same character may not appear more than thrice consecutively,
meaning that 'MMMCMXCIX' (3,999) is the largest well-formed Roman numeral.
The smallest is 'I' (1), as there is no symbol for zero in Roman numerals.

Both upper- and lower-case formatting of roman numerals are supported,
and likewise for parsing strings,
although the entire string must be of the same case.
Numerals that do not adhere to the classical form are rejected
with an ``InvalidRomanNumeralError``.

## Example usage

### Create a roman numeral

```rust
use roman_numerals_rs::RomanNumeral;

let num = RomanNumeral::new(16)?;
assert_eq!(num.to_string(), "XVI");

let num: RomanNumeral = "XVI".parse()?;
assert_eq!(num.as_u16(), 16);

let num: RomanNumeral = 3_999.try_into().unwrap();
println!("{}", num);  // MMMCMXCIX
```

### Convert a roman numeral to a string

```rust
use roman_numerals_rs::RomanNumeral;

let num = RomanNumeral::new(16)?;
assert_eq!(num.to_string(), "XVI");
assert_eq!(num.to_uppercase(), "XVI");
assert_eq!(num.to_lowercase(), "xvi");
assert_eq!(format!("{:X}", num), "XVI");
assert_eq!(format!("{:x}", num), "xvi");
```

### Extract the decimal value of a roman numeral

```rust
use roman_numerals_rs::RomanNumeral;

let num = RomanNumeral::new(42)?;
assert_eq!(num.as_u16(), 42);
```

### Invalid input

```rust
use core::str::FromStr;
use roman_numerals_rs::{RomanNumeral, InvalidRomanNumeralError, OutOfRangeError};

let res = RomanNumeral::from_str("Spam!");
assert!(matches!(res.unwrap_err(), InvalidRomanNumeralError));

let res = "CLL".parse::<RomanNumeral>();
assert!(matches!(res.unwrap_err(), InvalidRomanNumeralError));

let res = RomanNumeral::new(0);
assert!(matches!(res.unwrap_err(), OutOfRangeError));

let res = RomanNumeral::new(4_000);
assert!(matches!(res.unwrap_err(), OutOfRangeError));
```

## Benchmarks

Run the benchmarks with ``cargo bench``.

## Licence

This project is licenced under the terms of either the Zero-Clause BSD licence
or the CC0 1.0 Universal licence.