use alloc::string::String;
use alloc::vec::Vec;
use crate::{EncodeError, Encoding};
pub fn decode(bytes: &[u8]) -> String {
Encoding::Roman.decode(bytes)
}
pub fn encode(text: &str) -> Result<Vec<u8>, EncodeError> {
Encoding::Roman.encode(text)
}
#[cfg(test)]
mod tests {
use alloc::vec;
use super::*;
#[test]
fn ascii_round_trips() {
assert_eq!(decode(b"CODE"), "CODE");
assert_eq!(encode("CODE").unwrap(), b"CODE");
}
#[test]
fn pi_is_0xb9_not_a_utf8_construction() {
assert_eq!(decode(&[0xB9]), "π");
assert_eq!(encode("π").unwrap(), vec![0xB9]);
}
#[test]
fn bullet_is_0xa5_and_asterisk_is_0x2a() {
assert_eq!(decode(&[0xA5]), "•");
assert_eq!(decode(&[0x2A]), "*");
}
#[test]
fn every_high_byte_decodes_and_re_encodes() {
for byte in 0x80u8..=0xFF {
let decoded = decode(&[byte]);
assert_eq!(
encode(&decoded),
Ok(vec![byte]),
"byte {byte:#04x} did not round-trip"
);
}
}
#[test]
fn every_byte_is_defined() {
assert!(Encoding::Roman.defines_every_byte());
assert!(Encoding::Roman
.decode_strict(&(0..=255).collect::<Vec<u8>>())
.is_ok());
}
#[test]
fn apple_logo_is_private_use() {
assert_eq!(decode(&[0xF0]), "\u{F8FF}");
assert_eq!(encode("\u{F8FF}").unwrap(), vec![0xF0]);
}
#[test]
fn byte_0xdb_is_the_euro_not_the_currency_sign() {
assert_eq!(decode(&[0xDB]), "€");
assert_eq!(
encode("¤"),
Err(EncodeError {
encoding: Encoding::Roman,
code_point: '¤',
index: 0,
})
);
}
#[test]
fn encode_error_locates_the_character() {
let err = encode("πx→").unwrap_err();
assert_eq!(err.code_point, '→');
assert_eq!(err.index, 3);
}
}