pub fn encode_unicode(text: u8) -> char {
if text == 255 {
return '\n';
}
char::from_u32(text as u32 + 0x2800).unwrap()
}
pub const fn decode_unicode(text: char) -> u8 {
if (text as u32) < 0x2800 {
panic!("Invalid unicode character");
}
(text as u32 - 0x2800) as u8
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn encode_unicode_maps_byte_to_braille_offset() {
for b in 0u8..64 {
let expected = char::from_u32(u32::from(b) + 0x2800).unwrap();
assert_eq!(
encode_unicode(b),
expected,
"byte {b} should map to {expected:?}"
);
}
}
#[test]
fn encode_unicode_255_returns_newline() {
assert_eq!(encode_unicode(255), '\n');
}
#[test]
#[should_panic(expected = "Invalid unicode character")]
fn decode_unicode_panics_for_non_braille_char() {
let _ = decode_unicode('A');
}
}