Crates.io | roman_numeral |
lib.rs | roman_numeral |
version | 0.1.0 |
source | src |
created_at | 2020-04-02 19:38:19.927478 |
updated_at | 2020-04-02 19:38:19.927478 |
description | Rust library for roman numerals. Encode/decode roman numerals with ease! |
homepage | https://github.com/jammymalina/roman_numeral/ |
repository | https://github.com/jammymalina/roman_numeral/ |
max_upload_size | |
id | 225639 |
size | 15,478 |
Rust library for roman numerals. Encode/decode roman numerals with ease!
Add this to your Cargo.toml
:
[dependencies]
roman_numeral = "0.1"
Example
use roman_numeral::{RomanNumeral, RomanNumeralError};
// Create roman numeral
let roman = RomanNumeral::from_string("I").unwrap();
assert_eq!(roman.get_u32(), 1);
let roman = RomanNumeral::from_u32(49).unwrap();
assert_eq!(roman.get(), String::from("XLIX"));
// Get roman numeral (string)
let roman = RomanNumeral::from_u32(3999).unwrap();
let numeral = roman.get(); // MMMCMXCIX
// Get roman numeral decimal value
let roman = RomanNumeral::from_string("CXXV").unwrap();
let decimal_value = roman.get_u32(); // 125
// Get maximum possible value
let max_value = RomanNumeral::max_value();
assert_eq!(max_value, 3999);
// Invalid input
let error = RomanNumeral::from_string("IIII").unwrap_err();
assert_eq!(error, RomanNumeralError::InvalidStr);
let error = RomanNumeral::from_u32(0).unwrap_err();
assert_eq!(error, RomanNumeralError::NumberOutOfRange);
let error = RomanNumeral::from_u32(4000).unwrap_err();
assert_eq!(error, RomanNumeralError::NumberOutOfRange);