chisel_json/parsers/
sax_events.rs

1//! Events generated by the SAX parser
2//!
3//!
4use std::borrow::Cow;
5use std::fmt::Display;
6
7use crate::coords::Span;
8use crate::pointers::pointer::JsonPointer;
9
10/// Enumeration of the various different matches that can be produced during a parse
11#[derive(PartialEq)]
12pub enum Match<'a> {
13    /// Start of the input Emitted prior to anything else
14    StartOfInput,
15    /// End of the input Emitted after everything else
16    EndOfInput,
17    /// Emitted when the start of a new object is matched
18    StartObject,
19    /// Emitted when a new key within an object is matched
20    ObjectKey(Cow<'a, str>),
21    /// Emitted after an object has been fully parsed
22    EndObject,
23    /// Emitted when the start of an array is matched
24    StartArray,
25    /// Emitted when the end of an array is matched
26    EndArray,
27    /// Emitted when a string is matched
28    String(Cow<'a, str>),
29    /// Emitted when an integer is matched
30    Integer(i64),
31    /// Emitted when a float is matched
32    Float(f64),
33    /// Emitted when a boolean is matched
34    Boolean(bool),
35    /// Emitted when a null is matched
36    Null,
37}
38
39impl<'a> Display for Match<'a> {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            Match::StartOfInput => write!(f, "StartOfInput"),
43            Match::EndOfInput => write!(f, "EndOfInput"),
44            Match::StartObject => write!(f, "StartObject"),
45            Match::ObjectKey(_) => write!(f, "ObjectKey"),
46            Match::EndObject => write!(f, "EndObject"),
47            Match::StartArray => write!(f, "StartArray"),
48            Match::EndArray => write!(f, "EndArray"),
49            Match::String(value) => write!(f, "String({})", value),
50            Match::Integer(value) => write!(f, "Integer({})", value),
51            Match::Float(value) => write!(f, "Float({})", value),
52            Match::Boolean(b) => write!(f, "Boolean({})", b),
53            Match::Null => write!(f, "Null"),
54        }
55    }
56}
57
58/// An event produced by the parser during a parse
59pub struct Event<'a> {
60    /// The [Match] associated with the event
61    pub matched: Match<'a>,
62
63    /// The [Span] associated with the current [Match]
64    pub span: Span,
65
66    /// Optional [JsonPointer] information relating to the event
67    pub pointer: Option<&'a JsonPointer<'a>>,
68}
69
70impl<'a> Event<'a> {
71    /// Checks whether an event has a path or not
72    fn has_pointer(&self) -> bool {
73        self.pointer.is_some()
74    }
75}
76
77impl<'a> Display for Event<'a> {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        if self.pointer.is_some() {
80            write!(
81                f,
82                "Event[{}, {}, {}]",
83                self.matched,
84                self.span,
85                self.pointer.unwrap()
86            )
87        } else {
88            write!(f, "Event[{}, {}]", self.matched, self.span)
89        }
90    }
91}