chisel_parsers/json/
events.rs

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