Skip to content

Commit cd5f603

Browse files
committed
Implement @set
1 parent a00eb7e commit cd5f603

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/test/rustdoc-json/nested.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
// @is nested.json "$.index[*][?(@.name=='l1')].kind" \"module\"
88
// @is - "$.index[*][?(@.name=='l1')].inner.is_crate" false
99
// @count - "$.index[*][?(@.name=='l1')].inner.items[*]" 2
10+
// @set l1_id = - "$.index[*][?(@.name=='l1')].id"
1011
pub mod l1 {
1112

1213
// @is nested.json "$.index[*][?(@.name=='l3')].kind" \"module\"
1314
// @is - "$.index[*][?(@.name=='l3')].inner.is_crate" false
1415
// @count - "$.index[*][?(@.name=='l3')].inner.items[*]" 1
16+
// @set l3_id = - "$.index[*][?(@.name=='l3')].id"
1517
pub mod l3 {
1618

1719
// @is nested.json "$.index[*][?(@.name=='L4')].kind" \"struct\"
1820
// @is - "$.index[*][?(@.name=='L4')].inner.struct_type" \"unit\"
21+
// @set l4_id = - "$.index[*][?(@.name=='L4')].id"
1922
pub struct L4;
2023
}
2124
// @is nested.json "$.index[*][?(@.inner.span=='l3::L4')].kind" \"import\"

src/tools/jsondocck/src/cache.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct Cache {
99
root: PathBuf,
1010
files: HashMap<PathBuf, String>,
1111
values: HashMap<PathBuf, Value>,
12+
pub variables: HashMap<String, Value>,
1213
last_path: Option<PathBuf>,
1314
}
1415

@@ -19,6 +20,7 @@ impl Cache {
1920
root: Path::new(doc_dir).to_owned(),
2021
files: HashMap::new(),
2122
values: HashMap::new(),
23+
variables: HashMap::new(),
2224
last_path: None,
2325
}
2426
}

src/tools/jsondocck/src/main.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ pub enum CommandKind {
4949
Has,
5050
Count,
5151
Is,
52+
Set,
5253
}
5354

5455
impl CommandKind {
5556
fn validate(&self, args: &[String], command_num: usize, lineno: usize) -> bool {
5657
let count = match self {
5758
CommandKind::Has => (1..=3).contains(&args.len()),
5859
CommandKind::Count | CommandKind::Is => 3 == args.len(),
60+
CommandKind::Set => 4 == args.len(),
5961
};
6062

6163
if !count {
@@ -85,6 +87,7 @@ impl fmt::Display for CommandKind {
8587
CommandKind::Has => "has",
8688
CommandKind::Count => "count",
8789
CommandKind::Is => "is",
90+
CommandKind::Set => "set",
8891
};
8992
write!(f, "{}", text)
9093
}
@@ -130,6 +133,7 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
130133
"has" => CommandKind::Has,
131134
"count" => CommandKind::Count,
132135
"is" => CommandKind::Is,
136+
"set" => CommandKind::Set,
133137
_ => {
134138
print_err(&format!("Unrecognized command name `@{}`", cmd), lineno);
135139
errors = true;
@@ -236,6 +240,23 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
236240
Err(_) => false,
237241
}
238242
}
243+
// FIXME, Figure out semantics for @!set
244+
CommandKind::Set => {
245+
// @set <name> = <path> <jsonpath>
246+
assert_eq!(command.args.len(), 4);
247+
assert_eq!(command.args[1], "=", "Expected an `=`");
248+
let val = cache.get_value(&command.args[2])?;
249+
250+
match select(&val, &command.args[3]) {
251+
Ok(results) => {
252+
assert_eq!(results.len(), 1);
253+
let r = cache.variables.insert(command.args[0].clone(), results[0].clone());
254+
assert!(r.is_none(), "Name collision");
255+
true
256+
}
257+
Err(_) => false,
258+
}
259+
}
239260
};
240261

241262
if result == command.negated {

0 commit comments

Comments
 (0)