mod dialect;
mod event;
mod fixes;
mod format;
mod messages;
mod parse;
mod path;
mod problem;
mod report;
mod rule;
mod suggest;
mod unicode;
pub use dialect::{Dialect, UnknownDialect};
pub use event::{Event, EventKind};
#[doc(hidden)]
pub use fixes::{Fix, Fixed, fix, try_fix, try_fix_at};
pub use format::{FormatError, FormatOptions, Indent, format};
pub use messages::{MESSAGES, fill};
pub use path::{PathStep, json_path, key_text};
pub use problem::{Problem, Related, RelatedKind};
pub use report::{Location, Report};
pub use rule::{Config, Rule, Severity, UnknownRule};
pub use suggest::{closest, did_you_mean};
pub use unicode::is_hidden;
pub fn check<'a>(text: &'a str, config: &Config) -> Report<'a> {
let problems = parse::check(text, config, false).0;
Report::new(text, problems, config.dialect() == Dialect::Json5)
}
#[doc(hidden)]
#[must_use]
pub fn report<'a>(text: &'a str, problems: Vec<Problem>, config: &Config) -> Report<'a> {
Report::new(text, problems, config.dialect() == Dialect::Json5)
}
#[doc(hidden)]
pub fn parse<'a>(text: &'a str, config: &Config) -> (Report<'a>, Option<Vec<Event<'a>>>) {
let (problems, events) = parse::check(text, config, true);
let syntax_error = problems.iter().any(|p| p.rule() == Rule::Syntax);
(
Report::new(text, problems, config.dialect() == Dialect::Json5),
(!syntax_error).then_some(events),
)
}
#[doc(hidden)]
pub fn parse_with<'a>(
text: &'a str,
config: &Config,
sink: &mut dyn FnMut(Event<'a>),
) -> Report<'a> {
let problems = parse::walk(text, 0..text.len(), config, Some(sink));
Report::new(text, problems, config.dialect() == Dialect::Json5)
}
#[doc(hidden)]
pub fn line_parts(text: &str, parts: usize) -> Vec<std::ops::Range<usize>> {
let mut ranges = Vec::new();
let mut start = 0;
for i in 1..parts {
let from = (text.len() / parts * i).max(start + 1) - 1;
let Some(newline) = text.as_bytes()[from..].iter().position(|&b| b == b'\n') else {
break;
};
let end = from + newline + 1;
ranges.push(start..end);
start = end;
}
if start < text.len() || ranges.is_empty() {
ranges.push(start..text.len());
}
ranges
}
#[doc(hidden)]
pub struct Lines {
problems: Vec<Problem>,
syntax_error: bool,
}
impl Lines {
pub fn syntax_error(&self) -> bool {
self.syntax_error
}
}
#[doc(hidden)]
pub fn parse_lines<'a>(
text: &'a str,
lines: std::ops::Range<usize>,
config: &Config,
sink: Option<&mut dyn FnMut(Event<'a>)>,
) -> Lines {
debug_assert_eq!(config.dialect(), Dialect::JsonLines);
let problems = parse::walk(text, lines, config, sink);
let syntax_error = problems.iter().any(|p| p.rule() == Rule::Syntax);
Lines {
problems,
syntax_error,
}
}
#[doc(hidden)]
pub fn join_lines(text: &str, parts: Vec<Lines>) -> (Report<'_>, bool) {
let mut problems = Vec::new();
for mut part in parts {
if problems
.iter()
.any(|p: &Problem| p.rule() == Rule::MixedLineEndings)
{
part.problems.retain(|p| p.rule() != Rule::MixedLineEndings);
}
problems.append(&mut part.problems);
if part.syntax_error {
return (Report::new(text, problems, false), true);
}
}
(Report::new(text, problems, false), false)
}
#[cfg(doctest)]
pub struct ApiGuards;