use crate::char_struct::{CharType, KoreanChar};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EncodingMode {
Korean,
English,
Math,
MiddleKorean,
}
#[derive(Debug, Clone)]
pub struct EncoderState {
pub mode_stack: Vec<EncodingMode>,
pub is_english: bool,
pub english_indicator: bool,
pub triple_big_english: bool,
pub has_processed_word: bool,
pub needs_english_continuation: bool,
pub parenthesis_stack: Vec<bool>,
pub is_number: bool,
pub is_big_english: bool,
}
impl EncoderState {
pub fn new(english_indicator: bool) -> Self {
Self {
mode_stack: vec![EncodingMode::Korean],
english_indicator,
is_english: false,
triple_big_english: false,
has_processed_word: false,
needs_english_continuation: false,
parenthesis_stack: Vec::new(),
is_number: false,
is_big_english: false,
}
}
pub fn current_mode(&self) -> EncodingMode {
self.mode_stack
.last()
.copied()
.unwrap_or(EncodingMode::Korean)
}
pub fn push_mode(&mut self, mode: EncodingMode) {
self.mode_stack.push(mode);
}
pub fn pop_mode(&mut self) -> Option<EncodingMode> {
if self.mode_stack.len() > 1 {
self.mode_stack.pop()
} else {
None
}
}
}
pub struct RuleContext<'a> {
pub word_chars: &'a [char],
pub index: usize,
pub char_type: &'a CharType,
pub prev_word: &'a str,
pub remaining_words: &'a [&'a str],
pub has_korean_char: bool,
pub is_all_uppercase: bool,
pub ascii_starts_at_beginning: bool,
pub skip_count: &'a mut usize,
pub state: &'a mut EncoderState,
pub result: &'a mut Vec<u8>,
}
impl<'a> RuleContext<'a> {
pub fn current_char(&self) -> char {
self.word_chars[self.index]
}
pub fn next_char(&self) -> Option<char> {
self.word_chars.get(self.index + 1).copied()
}
pub fn prev_char(&self) -> Option<char> {
if self.index > 0 {
Some(self.word_chars[self.index - 1])
} else {
None
}
}
pub fn word_len(&self) -> usize {
self.word_chars.len()
}
pub fn as_korean(&self) -> Option<&KoreanChar> {
if let CharType::Korean(k) = self.char_type {
Some(k)
} else {
None
}
}
pub fn emit(&mut self, byte: u8) {
self.result.push(byte);
}
pub fn emit_slice(&mut self, bytes: &[u8]) {
self.result.extend_from_slice(bytes);
}
}