Skip to main content

ephemeris_documents/
json.rs

1//! JSON and JSON5 support: the dialects, the syntax tree, and the backend.
2//!
3//! JSON5 can express every annotation except dotted tables: bare identifier
4//! keys, `//` comments, and single-quoted literal strings. JSON can express
5//! none of them. [`Format::supports`] tells which, so rendering cannot fail.
6
7mod backend;
8mod cst;
9mod lexer;
10mod node;
11mod parser;
12
13pub(crate) use self::backend::Json;
14use self::cst::{Comma, Position};
15use crate::format::Format;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub(crate) enum Dialect {
19    Json,
20    Json5,
21}
22
23impl Dialect {
24    /// The format this dialect writes.
25    pub(crate) const fn format(self) -> Format {
26        match self {
27            Self::Json => Format::Json,
28            Self::Json5 => Format::Json5,
29        }
30    }
31
32    /// A comma follows every member but the last. The last member keeps its
33    /// comma only in JSON5.
34    const fn comma(self, position: Position, comma: Comma) -> bool {
35        match position {
36            Position::Inner => true,
37            Position::Last => matches!(comma, Comma::Present) && matches!(self, Self::Json5),
38        }
39    }
40}
41
42#[cfg(test)]
43mod tests;