Skip to content

Commit 728ffc8

Browse files
committed
Address review v2
1 parent a3df483 commit 728ffc8

File tree

12 files changed

+60
-68
lines changed

12 files changed

+60
-68
lines changed

src/bootstrap/builder.rs

-5
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,6 @@ impl<'a> Builder<'a> {
751751
cmd
752752
}
753753

754-
/// Gets a path to the jsondocck tool
755-
pub fn jsondocck(&self, compiler: Compiler, target: TargetSelection) -> PathBuf {
756-
self.ensure(tool::JsonDocCk { compiler, target })
757-
}
758-
759754
/// Return the path to `llvm-config` for the target, if it exists.
760755
///
761756
/// Note that this returns `None` if LLVM is disabled, or if we're in a

src/bootstrap/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ note: if you're sure you want to do this, please open an issue as to why. In the
10721072

10731073
cmd.arg("--docck-python").arg(builder.python());
10741074

1075-
cmd.arg("--jsondocck-path").arg(builder.jsondocck(compiler, target));
1075+
cmd.arg("--jsondocck-path").arg(builder.ensure(tool::JsonDocCk { compiler, target }));
10761076

10771077
if builder.config.build.ends_with("apple-darwin") {
10781078
// Force /usr/bin/python3 on macOS for LLDB tests because we're loading the

src/test/rustdoc-json/nested.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
// edition:2018
22

3-
// @has nested.json "$.index.['0:0'].kind" \"module\"
4-
// @has - "$.index.['0:0'].inner.is_crate" true
5-
// @has - "$.index.['0:0'].inner.items[*]" \"0:3\"
3+
// @has nested.json "$.index[*][?(@.name=='nested')].kind" \"module\"
4+
// @has - "$.index[*][?(@.name=='nested')].inner.is_crate" true
5+
// @count - "$.index[*][?(@.name=='nested')].inner.items[*]" 1
66

7-
// @has nested.json "$.index.['0:3'].kind" \"module\"
8-
// @has - "$.index.['0:3'].inner.is_crate" false
9-
// @has - "$.index.['0:3'].inner.items[*]" \"0:4\"
10-
// @has - "$.index.['0:3'].inner.items[*]" \"0:7\"
7+
// @has nested.json "$.index[*][?(@.name=='l1')].kind" \"module\"
8+
// @has - "$.index[*][?(@.name=='l1')].inner.is_crate" false
9+
// @count - "$.index[*][?(@.name=='l1')].inner.items[*]" 2
1110
pub mod l1 {
1211

13-
// @has nested.json "$.index.['0:4'].kind" \"module\"
14-
// @has - "$.index.['0:4'].inner.is_crate" false
15-
// @has - "$.index.['0:4'].inner.items[*]" \"0:5\"
12+
// @has nested.json "$.index[*][?(@.name=='l3')].kind" \"module\"
13+
// @has - "$.index[*][?(@.name=='l3')].inner.is_crate" false
14+
// @count - "$.index[*][?(@.name=='l3')].inner.items[*]" 1
1615
pub mod l3 {
1716

18-
// @has nested.json "$.index.['0:5'].kind" \"struct\"
19-
// @has - "$.index.['0:5'].inner.struct_type" \"unit\"
17+
// @has nested.json "$.index[*][?(@.name=='L4')].kind" \"struct\"
18+
// @has - "$.index[*][?(@.name=='L4')].inner.struct_type" \"unit\"
2019
pub struct L4;
2120
}
22-
// @has nested.json "$.index.['0:7'].kind" \"import\"
23-
// @has - "$.index.['0:7'].inner.glob" false
21+
// @has nested.json "$.index[*][?(@.inner.span=='l3::L4')].kind" \"import\"
22+
// @has - "$.index[*][?(@.inner.span=='l3::L4')].inner.glob" false
2423
pub use l3::L4;
2524
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// @has plain_empty.json "$.index.['0:3'].name" \"PlainEmpty\"
2-
// @has - "$.index.['0:3'].visibility" \"public\"
3-
// @has - "$.index.['0:3'].kind" \"struct\"
4-
// @has - "$.index.['0:3'].inner.struct_type" \"plain\"
5-
// @has - "$.index.['0:3'].inner.fields_stripped" false
6-
// @has - "$.index.['0:3'].inner.fields" []
1+
// @has plain_empty.json "$.index[*][?(@.name=='PlainEmpty')].visibility" \"public\"
2+
// @has - "$.index[*][?(@.name=='PlainEmpty')].kind" \"struct\"
3+
// @has - "$.index[*][?(@.name=='PlainEmpty')].inner.struct_type" \"plain\"
4+
// @has - "$.index[*][?(@.name=='PlainEmpty')].inner.fields_stripped" false
5+
// @has - "$.index[*][?(@.name=='PlainEmpty')].inner.fields" []
76
pub struct PlainEmpty {}
+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// @has tuple.json "$.index.['0:3'].name" \"Tuple\"
2-
// @has - "$.index.['0:3'].visibility" \"public\"
3-
// @has - "$.index.['0:3'].kind" \"struct\"
4-
// @has - "$.index.['0:3'].inner.struct_type" \"tuple\"
5-
// @has - "$.index.['0:3'].inner.fields_stripped" true
1+
// @has tuple.json "$.index[*][?(@.name=='Tuple')].visibility" \"public\"
2+
// @has - "$.index[*][?(@.name=='Tuple')].kind" \"struct\"
3+
// @has - "$.index[*][?(@.name=='Tuple')].inner.struct_type" \"tuple\"
4+
// @has - "$.index[*][?(@.name=='Tuple')].inner.fields_stripped" true
65
pub struct Tuple(u32, String);

src/test/rustdoc-json/structs/unit.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// @has unit.json "$.index.['0:3'].name" \"Unit\"
2-
// @has - "$.index.['0:3'].visibility" \"public\"
3-
// @has - "$.index.['0:3'].kind" \"struct\"
4-
// @has - "$.index.['0:3'].inner.struct_type" \"unit\"
5-
// @has - "$.index.['0:3'].inner.fields" []
1+
// @has unit.json "$.index[*][?(@.name=='Unit')].visibility" \"public\"
2+
// @has - "$.index[*][?(@.name=='Unit')].kind" \"struct\"
3+
// @has - "$.index[*][?(@.name=='Unit')].inner.struct_type" \"unit\"
4+
// @has - "$.index[*][?(@.name=='Unit')].inner.fields" []
65
pub struct Unit;

src/test/rustdoc-json/structs/with_generics.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::collections::HashMap;
22

3-
// @has with_generics.json "$.index.['0:4'].name" \"WithGenerics\"
4-
// @has - "$.index.['0:4'].visibility" \"public\"
5-
// @has - "$.index.['0:4'].kind" \"struct\"
6-
// @has - "$.index.['0:4'].inner.generics.params[0].name" \"T\"
7-
// @has - "$.index.['0:4'].inner.generics.params[0].kind.type"
8-
// @has - "$.index.['0:4'].inner.generics.params[1].name" \"U\"
9-
// @has - "$.index.['0:4'].inner.generics.params[1].kind.type"
10-
// @has - "$.index.['0:4'].inner.struct_type" \"plain\"
11-
// @has - "$.index.['0:4'].inner.fields_stripped" true
3+
// @has with_generics.json "$.index[*][?(@.name=='WithGenerics')].visibility" \"public\"
4+
// @has - "$.index[*][?(@.name=='WithGenerics')].kind" \"struct\"
5+
// @has - "$.index[*][?(@.name=='WithGenerics')].inner.generics.params[0].name" \"T\"
6+
// @has - "$.index[*][?(@.name=='WithGenerics')].inner.generics.params[0].kind.type"
7+
// @has - "$.index[*][?(@.name=='WithGenerics')].inner.generics.params[1].name" \"U\"
8+
// @has - "$.index[*][?(@.name=='WithGenerics')].inner.generics.params[1].kind.type"
9+
// @has - "$.index[*][?(@.name=='WithGenerics')].inner.struct_type" \"plain\"
10+
// @has - "$.index[*][?(@.name=='WithGenerics')].inner.fields_stripped" true
1211
pub struct WithGenerics<T, U> {
1312
stuff: Vec<T>,
1413
things: HashMap<U, U>,

src/test/rustdoc-json/structs/with_primitives.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// @has with_primitives.json "$.index.['0:3'].name" \"WithPrimitives\"
2-
// @has - "$.index.['0:3'].visibility" \"public\"
3-
// @has - "$.index.['0:3'].kind" \"struct\"
4-
// @has - "$.index.['0:3'].inner.generics.params[0].name" \"\'a\"
5-
// @has - "$.index.['0:3'].inner.generics.params[0].kind" \"lifetime\"
6-
// @has - "$.index.['0:3'].inner.struct_type" \"plain\"
7-
// @has - "$.index.['0:3'].inner.fields_stripped" true
1+
// @has with_primitives.json "$.index[*][?(@.name=='WithPrimitives')].visibility" \"public\"
2+
// @has - "$.index[*][?(@.name=='WithPrimitives')].kind" \"struct\"
3+
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.generics.params[0].name" \"\'a\"
4+
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.generics.params[0].kind" \"lifetime\"
5+
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.struct_type" \"plain\"
6+
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.fields_stripped" true
87
pub struct WithPrimitives<'a> {
98
num: u32,
109
s: &'a str,

src/tools/compiletest/src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ pub fn parse_config(args: Vec<String>) -> Config {
197197
let has_tidy = Command::new("tidy")
198198
.arg("--version")
199199
.stdout(Stdio::null())
200-
.stderr(Stdio::null())
201200
.status()
202201
.map_or(false, |status| status.success());
203202
Config {

src/tools/jsondocck/src/cache.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct Cache {
1313
}
1414

1515
impl Cache {
16+
/// Create a new cache, used to read files only once and otherwise store their contents.
1617
pub fn new(doc_dir: &str) -> Cache {
1718
Cache {
1819
root: Path::new(doc_dir).to_owned(),
@@ -32,9 +33,7 @@ impl Cache {
3233
}
3334
}
3435

35-
pub fn get_file(&mut self, path: &String) -> Result<String, io::Error> {
36-
let path = self.resolve_path(path);
37-
36+
fn read_file(&mut self, path: PathBuf) -> Result<String, io::Error> {
3837
if let Some(f) = self.files.get(&path) {
3938
return Ok(f.clone());
4039
}
@@ -46,16 +45,22 @@ impl Cache {
4645
Ok(file)
4746
}
4847

48+
/// Get the text from a file. If called multiple times, the file will only be read once
49+
pub fn get_file(&mut self, path: &String) -> Result<String, io::Error> {
50+
let path = self.resolve_path(path);
51+
self.read_file(path)
52+
}
53+
54+
/// Parse the JSON from a file. If called multiple times, the file will only be read once.
4955
pub fn get_value(&mut self, path: &String) -> Result<Value, CkError> {
5056
let path = self.resolve_path(path);
5157

5258
if let Some(v) = self.values.get(&path) {
5359
return Ok(v.clone());
5460
}
5561

56-
let file = fs::File::open(&path)?;
57-
58-
let val = serde_json::from_reader::<_, Value>(file)?;
62+
let content = self.read_file(path.clone())?;
63+
let val = serde_json::from_str::<Value>(&content)?;
5964

6065
self.values.insert(path, val.clone());
6166

src/tools/jsondocck/src/config.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ pub struct Config {
88
pub template: String,
99
}
1010

11+
/// Create a Config from a vector of command-line arguments
1112
pub fn parse_config(args: Vec<String>) -> Config {
1213
let mut opts = Options::new();
1314
opts.reqopt("", "doc-dir", "Path to the documentation directory", "PATH")
1415
.reqopt("", "template", "Path to the template file", "PATH")
1516
.optflag("h", "help", "show this message");
1617

1718
let (argv0, args_) = args.split_first().unwrap();
18-
if args.len() == 1 || args[1] == "-h" || args[1] == "--help" {
19+
if args.len() == 1 {
1920
let message = format!("Usage: {} <doc-dir> <template>", argv0);
2021
println!("{}", opts.usage(&message));
2122
std::process::exit(1);

src/tools/jsondocck/src/main.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ fn print_err(msg: &str, lineno: usize) {
106106
eprintln!("Invalid command: {} on line {}", msg, lineno)
107107
}
108108

109+
/// Get a list of commands from a file. Does the work of ensuring the commands
110+
/// are syntactically valid.
109111
fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
110112
let mut commands = Vec::new();
111113
let mut errors = false;
@@ -147,10 +149,7 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
147149
}
148150
}
149151

150-
let args = match cap.name("args") {
151-
Some(m) => shlex::split(m.as_str()).unwrap(),
152-
None => vec![],
153-
};
152+
let args = cap.name("args").map_or(vec![], |m| shlex::split(m.as_str()).unwrap());
154153

155154
if !cmd.validate(&args, commands.len(), lineno) {
156155
errors = true;
@@ -163,15 +162,14 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
163162
if !errors { Ok(commands) } else { Err(()) }
164163
}
165164

165+
/// Performs the actual work of ensuring a command passes. Generally assumes the command
166+
/// is syntactically valid.
166167
fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
167168
let result = match command.kind {
168169
CommandKind::Has => {
169170
match command.args.len() {
170171
// @has <path> = file existence
171-
1 => match cache.get_file(&command.args[0]) {
172-
Ok(_) => true,
173-
Err(_) => false,
174-
},
172+
1 => cache.get_file(&command.args[0]).is_ok(),
175173
// @has <path> <jsonpath> = check path exists
176174
2 => {
177175
let val = cache.get_value(&command.args[0])?;

0 commit comments

Comments
 (0)