lintel_validate/parsers/
jsonc.rs1use miette::NamedSource;
2use serde_json::Value;
3
4use crate::diagnostics::ParseDiagnostic;
5
6use super::Parser;
7
8pub struct JsoncParser;
9
10impl Parser for JsoncParser {
11 fn parse(&self, content: &str, file_name: &str) -> Result<Value, ParseDiagnostic> {
12 let opts = jsonc_parser::ParseOptions {
13 allow_comments: true,
14 allow_loose_object_property_names: false,
15 allow_trailing_commas: true,
16 allow_single_quoted_strings: false,
17 allow_hexadecimal_numbers: false,
18 allow_missing_commas: false,
19 allow_unary_plus_numbers: false,
20 };
21 jsonc_parser::parse_to_serde_value(content, &opts)
22 .map_err(|e| {
23 let range = e.range();
24 ParseDiagnostic {
25 src: NamedSource::new(file_name, content.to_string()),
26 span: (range.start, range.end - range.start).into(),
27 message: e.to_string(),
28 }
29 })?
30 .ok_or_else(|| ParseDiagnostic {
31 src: NamedSource::new(file_name, content.to_string()),
32 span: 0.into(),
33 message: "empty JSONC document".to_string(),
34 })
35 }
36
37 fn annotate(&self, content: &str, schema_url: &str) -> Option<String> {
38 Some(super::annotate_json_content(content, schema_url))
39 }
40
41 fn strip_annotation(&self, content: &str) -> String {
42 super::strip_json_schema_property(content)
43 }
44}