json_codec_wasm/
stack.rs

1// A `Scope` value refers to an array (A) or object (O).
2// It is used together with `Stack` to track if arrays or objects are
3// currently en- or decoded. The boolean value is `false` if no elements
4// have been encountered yet and `true` otherwise.
5
6#[derive(Debug, Copy, Clone)]
7pub enum Scope {
8    A(bool), // Array
9    O(bool), // Object
10}
11
12#[derive(Debug, Clone)]
13pub struct Stack(Vec<Scope>);
14
15impl Stack {
16    pub fn new() -> Stack {
17        Stack(Vec::new())
18    }
19
20    pub fn push(&mut self, s: Scope) {
21        self.0.push(s)
22    }
23
24    pub fn pop(&mut self) -> Option<Scope> {
25        self.0.pop()
26    }
27
28    pub fn top(&self) -> Option<Scope> {
29        self.0.last().map(|x| *x)
30    }
31
32    pub fn set(&mut self) {
33        self.0.last_mut().map(|x| match *x {
34            Scope::A(_) => *x = Scope::A(true),
35            Scope::O(_) => *x = Scope::O(true),
36        });
37    }
38}