use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Format {
Positional(u32),
Char,
B64,
B64Url,
B32,
B32Hex,
}
impl Format {
pub const BIN: Format = Format::Positional(2);
pub const OCT: Format = Format::Positional(8);
pub const DEC: Format = Format::Positional(10);
pub const HEX: Format = Format::Positional(16);
pub fn radix(self) -> Option<u32> {
match self {
Format::Positional(r) => Some(r),
_ => None,
}
}
pub fn is_positional(self) -> bool {
matches!(self, Format::Positional(_))
}
pub fn bits_per_digit(self) -> Option<u32> {
let r = self.radix()?;
if r.is_power_of_two() {
Some(r.trailing_zeros())
} else {
None
}
}
pub fn byte_aligned(self) -> bool {
matches!(self.bits_per_digit(), Some(b) if 8 % b == 0)
}
pub fn is_byteish(self) -> bool {
!self.is_positional()
}
pub fn is_encoding(self) -> bool {
matches!(
self,
Format::B64 | Format::B64Url | Format::B32 | Format::B32Hex
)
}
pub fn parse(s: &str) -> Option<Format> {
let lower = s.to_ascii_lowercase();
let f = match lower.as_str() {
"bin" | "b" | "binary" => Format::BIN,
"oct" | "o" | "octal" => Format::OCT,
"dec" | "d" | "decimal" | "int" => Format::DEC,
"hex" | "h" | "hexadecimal" => Format::HEX,
"char" | "chr" | "ascii" | "text" | "str" | "raw" | "utf8" => Format::Char,
"b64" | "base64" => Format::B64,
"b64url" | "base64url" | "b64u" => Format::B64Url,
"b32" | "base32" => Format::B32,
"b32hex" | "base32hex" => Format::B32Hex,
_ => {
let r: u32 = lower.parse().ok()?;
if (2..=36).contains(&r) {
Format::Positional(r)
} else {
return None;
}
}
};
Some(f)
}
pub const SPELLINGS: [&'static str; 29] = [
"bin", "b", "binary", "oct", "o", "octal", "dec", "d", "decimal", "int", "hex", "h",
"hexadecimal", "char", "chr", "ascii", "text", "str", "raw", "utf8", "b64", "base64",
"b64url", "base64url", "b64u", "b32", "base32", "b32hex", "base32hex",
];
pub fn name(self) -> &'static str {
match self {
Format::Positional(2) => "bin",
Format::Positional(8) => "oct",
Format::Positional(10) => "dec",
Format::Positional(16) => "hex",
Format::Positional(_) => "base",
Format::Char => "char",
Format::B64 => "b64",
Format::B64Url => "b64url",
Format::B32 => "b32",
Format::B32Hex => "b32hex",
}
}
}
impl fmt::Display for Format {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Format::Positional(r) if self.name() == "base" => write!(f, "base{r}"),
other => write!(f, "{}", other.name()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_words_aliases_and_radices() {
assert_eq!(Format::parse("hex"), Some(Format::HEX));
assert_eq!(Format::parse("HEX"), Some(Format::HEX));
assert_eq!(Format::parse("16"), Some(Format::HEX));
assert_eq!(Format::parse("ascii"), Some(Format::Char));
assert_eq!(Format::parse("base64"), Some(Format::B64));
assert_eq!(Format::parse("36"), Some(Format::Positional(36)));
}
#[test]
fn rejects_out_of_range_radices() {
assert_eq!(Format::parse("1"), None);
assert_eq!(Format::parse("37"), None);
assert_eq!(Format::parse("0"), None);
assert_eq!(Format::parse("nonsense"), None);
}
#[test]
fn bits_per_digit_only_for_powers_of_two() {
assert_eq!(Format::BIN.bits_per_digit(), Some(1));
assert_eq!(Format::OCT.bits_per_digit(), Some(3));
assert_eq!(Format::HEX.bits_per_digit(), Some(4));
assert_eq!(Format::Positional(32).bits_per_digit(), Some(5));
assert_eq!(Format::DEC.bits_per_digit(), None);
assert_eq!(Format::Positional(36).bits_per_digit(), None);
assert_eq!(Format::Char.bits_per_digit(), None);
}
#[test]
fn byte_alignment_follows_the_bit_width() {
assert!(Format::BIN.byte_aligned());
assert!(Format::Positional(4).byte_aligned());
assert!(Format::HEX.byte_aligned());
assert!(!Format::OCT.byte_aligned());
assert!(!Format::Positional(32).byte_aligned());
assert!(!Format::DEC.byte_aligned());
assert!(!Format::Char.byte_aligned());
}
#[test]
fn every_spelling_parses() {
for s in Format::SPELLINGS {
assert!(Format::parse(s).is_some(), "`{s}` is listed but does not parse");
}
}
#[test]
fn display_names_arbitrary_bases() {
assert_eq!(Format::HEX.to_string(), "hex");
assert_eq!(Format::Positional(36).to_string(), "base36");
}
}