Skip to content

Commit a7a4fe9

Browse files
committed
jsondoclint: Tree Walk Validator
1 parent 2506aa0 commit a7a4fe9

File tree

5 files changed

+469
-7
lines changed

5 files changed

+469
-7
lines changed

Cargo.lock

+12-6
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ dependencies = [
103103

104104
[[package]]
105105
name = "anyhow"
106-
version = "1.0.60"
106+
version = "1.0.65"
107107
source = "registry+https://github.com/rust-lang/crates.io-index"
108-
checksum = "c794e162a5eff65c72ef524dfe393eb923c354e350bb78b9c7383df13f3bc142"
108+
checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602"
109109

110110
[[package]]
111111
name = "array_tool"
@@ -1362,9 +1362,9 @@ dependencies = [
13621362

13631363
[[package]]
13641364
name = "fs-err"
1365-
version = "2.5.0"
1365+
version = "2.8.1"
13661366
source = "registry+https://github.com/rust-lang/crates.io-index"
1367-
checksum = "bcd1163ae48bda72a20ae26d66a04d3094135cadab911cff418ae5e33f253431"
1367+
checksum = "64db3e262960f0662f43a6366788d5f10f7f244b8f7d7d987f560baf5ded5c50"
13681368

13691369
[[package]]
13701370
name = "fs_extra"
@@ -1894,6 +1894,12 @@ dependencies = [
18941894
[[package]]
18951895
name = "jsondoclint"
18961896
version = "0.1.0"
1897+
dependencies = [
1898+
"anyhow",
1899+
"fs-err",
1900+
"rustdoc-json-types",
1901+
"serde_json",
1902+
]
18971903

18981904
[[package]]
18991905
name = "jsonpath_lib"
@@ -4449,9 +4455,9 @@ dependencies = [
44494455

44504456
[[package]]
44514457
name = "serde_json"
4452-
version = "1.0.83"
4458+
version = "1.0.85"
44534459
source = "registry+https://github.com/rust-lang/crates.io-index"
4454-
checksum = "38dd04e3c8279e75b31ef29dbdceebfe5ad89f4d0937213c53f7d49d01b3d5a7"
4460+
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
44554461
dependencies = [
44564462
"indexmap",
44574463
"itoa",

src/tools/jsondoclint/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
anyhow = "1.0.62"
10+
fs-err = "2.8.1"
11+
rustdoc-json-types = { version = "0.1.0", path = "../../rustdoc-json-types" }
12+
serde_json = "1.0.85"
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rustdoc_json_types::ItemEnum;
2+
3+
pub(crate) fn can_appear_in_mod(kind: &ItemEnum) -> bool {
4+
match kind {
5+
ItemEnum::Module(_) => true,
6+
ItemEnum::ExternCrate { .. } => true,
7+
ItemEnum::Import(_) => true,
8+
ItemEnum::Union(_) => true,
9+
ItemEnum::Struct(_) => true,
10+
ItemEnum::StructField(_) => false, // Only in structs or variants
11+
ItemEnum::Enum(_) => true,
12+
ItemEnum::Variant(_) => false, // Only in enums
13+
ItemEnum::Function(_) => true,
14+
ItemEnum::Trait(_) => true,
15+
ItemEnum::TraitAlias(_) => true,
16+
ItemEnum::Method(_) => false, // Only in traits
17+
ItemEnum::Impl(_) => true,
18+
ItemEnum::Typedef(_) => true,
19+
ItemEnum::OpaqueTy(_) => todo!("IDK"), // On
20+
ItemEnum::Constant(_) => true,
21+
ItemEnum::Static(_) => true,
22+
ItemEnum::ForeignType => todo!("IDK"),
23+
ItemEnum::Macro(_) => true,
24+
ItemEnum::ProcMacro(_) => true,
25+
ItemEnum::PrimitiveType(_) => todo!("IDK"),
26+
ItemEnum::AssocConst { .. } => false, // Trait Only
27+
ItemEnum::AssocType { .. } => false, // Trait only
28+
}
29+
}

src/tools/jsondoclint/src/main.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
1-
fn main() {}
1+
use std::env;
2+
3+
use anyhow::{anyhow, bail, Result};
4+
use fs_err as fs;
5+
use rustdoc_json_types::{Crate, Id, FORMAT_VERSION};
6+
7+
pub(crate) mod item_kind;
8+
mod validator;
9+
10+
#[derive(Debug)]
11+
struct Error {
12+
message: String,
13+
id: Id,
14+
}
15+
16+
fn main() -> Result<()> {
17+
let path = env::args().nth(1).ok_or_else(|| anyhow!("no path given"))?;
18+
let contents = fs::read_to_string(path)?;
19+
let krate: Crate = serde_json::from_str(&contents)?;
20+
assert_eq!(krate.format_version, FORMAT_VERSION);
21+
22+
let mut validator = validator::Validator::new(&krate);
23+
validator.check_crate();
24+
25+
if !validator.errs.is_empty() {
26+
for err in validator.errs {
27+
eprintln!("`{}`: `{}`", err.id.0, err.message);
28+
}
29+
bail!("Errors validating json");
30+
}
31+
32+
Ok(())
33+
}

0 commit comments

Comments
 (0)