fallow_config/jsonc.rs
1//! JSONC parsing helpers shared by every surface that reads user-authored
2//! JSONC (config files, external plugin definitions, rule packs), so they all
3//! accept exactly the same dialect.
4
5use serde::de::DeserializeOwned;
6
7/// The JSONC dialect fallow accepts: comments and trailing commas on top of
8/// strict JSON. Loose extensions (unquoted keys, single quotes, hex numbers,
9/// unary plus, missing commas) stay rejected so files remain portable to other
10/// JSONC tooling.
11pub fn parse_options() -> jsonc_parser::ParseOptions {
12 jsonc_parser::ParseOptions {
13 allow_comments: true,
14 allow_loose_object_property_names: false,
15 allow_trailing_commas: true,
16 allow_missing_commas: false,
17 allow_single_quoted_strings: false,
18 allow_hexadecimal_numbers: false,
19 allow_unary_plus_numbers: false,
20 }
21}
22
23/// Parse JSONC `content` and deserialize it into `T` using [`parse_options`].
24///
25/// # Errors
26///
27/// Returns the parser's error when `content` is not valid JSONC under
28/// [`parse_options`] or does not deserialize into `T`.
29pub fn parse_to_value<T: DeserializeOwned>(
30 content: &str,
31) -> Result<T, jsonc_parser::errors::ParseError> {
32 jsonc_parser::parse_to_serde_value(content, &parse_options())
33}