wowlab_tidy/languages/toml/rules/
validity.rs1use crate::{Example, TomlCtx, Violation, violation};
2
3#[rustfmt::skip]
4const EXAMPLES: &[Example] = &[
5 Example {
6 label: "valid TOML",
7 code: "name = \"example\"\n[table]\nvalue = 1\n",
8 pass: true,
9 },
10 Example {
11 label: "syntax error",
12 code: "name =\n",
13 pass: false,
14 },
15 Example {
16 label: "duplicate key",
17 code: "name = \"first\"\nname = \"second\"\n",
18 pass: false,
19 },
20];
21
22crate::toml_rule!(
23 toml_validity,
24 "Reject TOML syntax errors and semantic conflicts such as duplicate keys.",
25 "Taplo validation catches malformed documents before language-specific consumers produce inconsistent diagnostics.",
26 High,
27);
28
29fn check_toml_validity(ctx: &TomlCtx<'_>) -> Vec<Violation> {
30 let mut violations: Vec<Violation> = ctx
31 .parse
32 .errors
33 .iter()
34 .map(|error| {
35 violation(
36 ctx.file.rel,
37 ctx.line_of_offset(usize::from(error.range.start())),
38 format!("TOML syntax error: {}", error.message),
39 )
40 })
41 .collect();
42
43 if ctx.parse.errors.is_empty()
44 && let Err(errors) = ctx.dom.validate()
45 {
46 violations.extend(
47 errors.map(|error| violation(ctx.file.rel, 1, format!("TOML semantic error: {error}"))),
48 );
49 }
50
51 violations
52}
53
54crate::tidy_toml_test!(check_toml_validity, {
55 crate::example_tests!(EXAMPLES, check_toml_validity);
56});