litelighter/src/tokenizer/state.rs
2022-11-22 00:24:23 +01:00

40 lines
843 B
Rust

use crate::tokenizer::types::Error;
/// Should never be modified directly. Instead use the `push_id` and `pop_id` methods.
pub struct State {
depth: u8,
id_stack: Vec<u8>
}
impl State {
pub fn new() -> Self {
State {
depth: 0,
id_stack: Vec::new()
}
}
pub fn get_id(&self) -> u8 {
self.id_stack[self.depth as usize]
}
pub fn push_id(&mut self, id: u8) -> Result<(), Error> {
self.depth += 1;
self.id_stack[self.depth as usize] = id;
Ok(())
}
pub fn pop_id(&mut self) -> Result<u8, Error> {
if self.depth.checked_sub(1) == None {
return Err(Error::ExceededDepthRange);
}
let id = self.get_id();
self.id_stack[self.depth as usize] = 0;
self.depth -= 1;
Ok(id)
}
}