#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContextValue {
ObjectKey,
ObjectValue,
Array,
}
#[derive(Debug, Default)]
pub struct JsonContext {
stack: Vec<ContextValue>,
}
impl JsonContext {
pub fn new() -> Self {
Self { stack: Vec::new() }
}
pub fn current(&self) -> Option<ContextValue> {
self.stack.last().copied()
}
pub fn is_empty(&self) -> bool {
self.stack.is_empty()
}
pub fn contains(&self, value: ContextValue) -> bool {
self.stack.contains(&value)
}
pub fn reset(&mut self) {
self.stack.pop();
}
pub fn set(&mut self, value: ContextValue) {
self.stack.push(value);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_context_stack() {
let mut ctx = JsonContext::new();
assert!(ctx.is_empty());
assert_eq!(ctx.current(), None);
ctx.set(ContextValue::ObjectKey);
assert!(!ctx.is_empty());
assert_eq!(ctx.current(), Some(ContextValue::ObjectKey));
ctx.set(ContextValue::Array);
assert_eq!(ctx.current(), Some(ContextValue::Array));
assert!(ctx.contains(ContextValue::ObjectKey));
ctx.reset();
assert_eq!(ctx.current(), Some(ContextValue::ObjectKey));
ctx.reset();
assert!(ctx.is_empty());
}
}