use super::Error;
#[derive(Clone)]
pub struct Pos {
slot: u8,
depth: usize,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Depth(usize);
impl Pos {
#[inline]
pub fn is_key(&self) -> bool {
self.slot == Slot::KEY
}
#[inline]
pub fn is_value(&self) -> bool {
self.slot == Slot::VAL
}
#[inline]
pub fn is_elem(&self) -> bool {
self.slot == Slot::ELEM
}
#[inline]
pub fn depth(&self) -> Depth {
Depth(self.depth)
}
}
#[derive(Clone)]
pub struct Stack {
inner: [Slot; Stack::SIZE],
depth: usize,
}
impl Stack {
const SIZE: usize = 16;
const MAX_LEN: usize = Self::SIZE - 1;
}
#[derive(Clone, Copy)]
struct Slot(u8);
impl Slot {
const EMPTY: u8 = 0b0000_0000;
const DONE: u8 = 0b0000_0001;
const ROOT: u8 = 0b1000_0000;
const MAP: u8 = 0b0100_0000;
const SEQ: u8 = 0b0010_0000;
const KEY: u8 = 0b0001_0000;
const VAL: u8 = 0b0000_1000;
const ELEM: u8 = 0b0000_0100;
const MASK_EXPECT: u8 = 0b1001_1100;
const MAP_KEY: u8 = Self::MAP | Self::KEY;
const MAP_KEY_DONE: u8 = Self::MAP_KEY | Self::DONE;
const MAP_VAL: u8 = Self::MAP | Self::VAL;
const MAP_VAL_DONE: u8 = Self::MAP_VAL | Self::DONE;
const SEQ_ELEM: u8 = Self::SEQ | Self::ELEM;
const SEQ_ELEM_DONE: u8 = Self::SEQ_ELEM | Self::DONE;
#[inline]
fn root() -> Self {
Slot(Slot::ROOT)
}
#[inline]
fn pos(self, depth: usize) -> Pos {
Pos {
slot: self.0 & Slot::MASK_EXPECT,
depth,
}
}
}
impl Default for Stack {
fn default() -> Self {
Self::new()
}
}
impl Stack {
#[inline]
pub fn new() -> Self {
Stack {
inner: [Slot::root(); Self::SIZE],
depth: 0,
}
}
#[inline]
pub fn clear(&mut self) {
*self = Stack {
inner: [Slot::root(); Self::SIZE],
depth: 0,
};
}
#[inline]
fn current_mut(&mut self) -> &mut Slot {
unsafe { self.inner.get_unchecked_mut(self.depth) }
}
#[inline]
fn current(&self) -> Slot {
unsafe { *self.inner.get_unchecked(self.depth) }
}
#[inline]
pub fn begin(&mut self) -> Result<(), Error> {
if self.depth == 0 {
self.current_mut().0 = Slot::ROOT;
Ok(())
} else {
Err(Error::msg("stack is not empty"))
}
}
#[inline]
pub fn primitive(&mut self) -> Result<Pos, Error> {
let mut curr = self.current_mut();
match curr.0 & Slot::DONE {
Slot::EMPTY => {
curr.0 |= Slot::DONE;
Ok(curr.pos(self.depth))
}
_ => Err(Error::msg("invalid attempt to write primitive")),
}
}
#[inline]
pub fn map_begin(&mut self) -> Result<Pos, Error> {
if self.depth >= Self::MAX_LEN {
return Err(Error::msg("nesting limit reached"));
}
let curr = self.current();
match curr.0 {
Slot::ROOT | Slot::MAP_KEY | Slot::MAP_VAL | Slot::SEQ_ELEM => {
self.depth += 1;
self.current_mut().0 = Slot::MAP;
Ok(curr.pos(self.depth))
}
_ => Err(Error::msg("invalid attempt to begin map")),
}
}
#[inline]
pub fn map_key(&mut self) -> Result<Pos, Error> {
let mut curr = self.current_mut();
match curr.0 {
Slot::MAP | Slot::MAP_VAL_DONE => {
curr.0 = Slot::MAP_KEY;
Ok(curr.pos(self.depth))
}
_ => Err(Error::msg("invalid attempt to begin key")),
}
}
#[inline]
pub fn map_value(&mut self) -> Result<Pos, Error> {
let mut curr = self.current_mut();
match curr.0 {
Slot::MAP_KEY_DONE => {
curr.0 = Slot::MAP_VAL;
Ok(curr.pos(self.depth))
}
_ => Err(Error::msg("invalid attempt to begin value")),
}
}
#[inline]
pub fn map_end(&mut self) -> Result<Pos, Error> {
let curr = self.current();
match curr.0 {
Slot::MAP | Slot::MAP_VAL_DONE => {
self.depth -= 1;
let mut curr = self.current_mut();
curr.0 |= Slot::DONE;
Ok(curr.pos(self.depth + 1))
}
_ => Err(Error::msg("invalid attempt to end map")),
}
}
#[inline]
pub fn seq_begin(&mut self) -> Result<Pos, Error> {
if self.depth >= Self::MAX_LEN {
return Err(Error::msg("nesting limit reached"));
}
let curr = self.current();
match curr.0 {
Slot::ROOT | Slot::MAP_KEY | Slot::MAP_VAL | Slot::SEQ_ELEM => {
self.depth += 1;
self.current_mut().0 = Slot::SEQ;
Ok(curr.pos(self.depth))
}
_ => Err(Error::msg("invalid attempt to begin sequence")),
}
}
#[inline]
pub fn seq_elem(&mut self) -> Result<Pos, Error> {
let mut curr = self.current_mut();
match curr.0 {
Slot::SEQ | Slot::SEQ_ELEM_DONE => {
curr.0 = Slot::SEQ_ELEM;
Ok(curr.pos(self.depth))
}
_ => Err(Error::msg("invalid attempt to begin element")),
}
}
#[inline]
pub fn seq_end(&mut self) -> Result<Pos, Error> {
let curr = self.current();
match curr.0 {
Slot::SEQ | Slot::SEQ_ELEM_DONE => {
self.depth -= 1;
let mut curr = self.current_mut();
curr.0 |= Slot::DONE;
Ok(curr.pos(self.depth + 1))
}
_ => Err(Error::msg("invalid attempt to end sequence")),
}
}
#[inline]
pub fn can_end(&self) -> bool {
self.depth == 0
}
#[inline]
pub fn end(&mut self) -> Result<(), Error> {
if self.depth == 0 {
self.current_mut().0 |= Slot::DONE;
Ok(())
} else {
Err(Error::msg("stack is not empty"))
}
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "std")]
mod std_support {
use crate::{
std::{
mem,
vec::Vec,
},
stream::stack::*,
};
#[test]
fn stack_is_not_bigger_than_vec() {
assert!(mem::size_of::<Stack>() <= mem::size_of::<Vec<Slot>>());
}
}
}