use std::str::from_utf8_unchecked;
use crate::engine::Engine;
use crate::error::Result;
#[derive(PartialEq)]
pub (crate) enum CodeType {
OneByteWonder(usize),
TwoByteCommon(bool, usize),
ThreeByteUncommon(bool, usize),
UnicodeChar(char),
Number(u128),
Unprintable(usize),
Repetitions(u32, usize),
Custom(bool, usize),
}
impl std::fmt::Debug for CodeType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CodeType::OneByteWonder(i) => {
write!(f, "OneByteWonder({:?})", crate::map::OneByteMap::get_index(*i))
}
CodeType::TwoByteCommon(space, i) => {
write!(f, "TwoByteCommon(\"{}{}\")", if *space { " " } else { "" }, crate::map::TwoByteMap::get_index(*i))
}
CodeType::ThreeByteUncommon(space, i) => {
write!(f, "ThreeByteUncommon(\"{}{}\")", if *space { " " } else { "" }, crate::map::ThreeByteMap::get_index(*i))
}
CodeType::Number(num) => {
write!(f, "Number({})", *num)
}
CodeType::UnicodeChar(ch) => {
write!(f, "UnicodeChar({:?})", *ch)
}
CodeType::Unprintable(i) => {
write!(f, "Unprintable({:?})", crate::map::Controls::get_index(*i))
}
CodeType::Repetitions(count, index) => {
write!(f, "Repetition(\"")?;
for _ in 0..*count {
write!(f, "{}", crate::map::Repetitions::get_index(*index))?;
}
write!(f, "\")")
}
CodeType::Custom(space, ind) => {
write!(f, "Custom({}{})", if *space { " " } else { "" }, *ind)
}
}
}
}
impl CodeType {
pub fn add_to_string(&self, string: & mut String, engine: & Engine) -> Result<()> {
use std::fmt::Write;
match self {
CodeType::OneByteWonder(index) => {
write!(string, "{}", crate::map::OneByteMap::get_index(*index))?;
}
CodeType::TwoByteCommon(space, index) => {
write!(string, "{}{}", if *space { " " } else { "" }, crate::map::TwoByteMap::get_index(*index) )?;
}
CodeType::ThreeByteUncommon(space, index) => {
write!(string, "{}{}", if *space { " " } else { "" }, crate::map::ThreeByteMap::get_index(*index))?;
}
CodeType::UnicodeChar(ch) => {
write!(string, "{}", *ch)?;
}
CodeType::Number(num) => {
write!(string, "{}", *num)?;
}
CodeType::Unprintable(index) => {
write!(string, "{}", crate::map::Controls::get_index(*index))?;
}
CodeType::Repetitions(count, index) => {
for _ in 0..*count {
write!(string, "{}", crate::map::Repetitions::get_index(*index))?;
}
}
CodeType::Custom(space, index) => {
write!(string, "{}{}", if *space { " " } else { "" }, unsafe { from_utf8_unchecked( engine.custom_map.get_by_right(index).unwrap() )} )?; }
}
Ok(())
}
}