use crate::rules::math::parser::MathToken;
use super::math_token_rule::{MathEncodeState, MathTokenEngine, MathTokenResult, MathTokenRule};
pub fn encode_decimal_point(
tokens: &[MathToken],
i: usize,
prev_was_number: &mut bool,
result: &mut Vec<u8>,
) {
if !*prev_was_number {
let has_next_number = match tokens.get(i + 1) {
Some(MathToken::Number(_)) => true,
Some(MathToken::MathSymbol('\u{0307}')) => {
matches!(tokens.get(i + 2), Some(MathToken::Number(_)))
}
_ => false,
};
if has_next_number {
result.push(60);
*prev_was_number = true;
}
}
result.push(50);
}
pub struct DecimalPointRule;
impl MathTokenRule for DecimalPointRule {
fn name(&self) -> &'static str {
"DecimalPointRule"
}
fn priority(&self) -> u16 {
50
}
fn matches(&self, tokens: &[MathToken], index: usize, _state: &MathEncodeState) -> bool {
matches!(tokens.get(index), Some(MathToken::DecimalPoint))
}
fn apply(
&self,
tokens: &[MathToken],
index: usize,
result: &mut Vec<u8>,
state: &mut MathEncodeState,
_engine: &MathTokenEngine,
) -> Result<MathTokenResult, String> {
encode_decimal_point(tokens, index, &mut state.prev_was_number, result);
Ok(MathTokenResult::Consumed(1))
}
}