Skip to content

Commit 7f54d30

Browse files
committed
jsondocck: command -> directive
1 parent 2c9fb22 commit 7f54d30

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

src/tools/jsondocck/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use getopts::Options;
44
pub struct Config {
55
/// The directory documentation output was generated in
66
pub doc_dir: String,
7-
/// The file documentation was generated for, with docck commands to check
7+
/// The file documentation was generated for, with docck directives to check
88
pub template: String,
99
}
1010

src/tools/jsondocck/src/directive.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
use std::borrow::Cow;
2+
23
use serde_json::Value;
4+
35
use crate::cache::Cache;
46

57
#[derive(Debug)]
6-
pub struct Command {
7-
pub kind: CommandKind,
8+
pub struct Directive {
9+
pub kind: DirectiveKind,
810
pub path: String,
911
pub lineno: usize,
1012
}
1113

1214
#[derive(Debug)]
13-
pub enum CommandKind {
15+
pub enum DirectiveKind {
1416
/// `//@ has <path>`
1517
///
1618
/// Checks the path exists.
@@ -55,16 +57,16 @@ pub enum CommandKind {
5557
Set { variable: String },
5658
}
5759

58-
impl CommandKind {
60+
impl DirectiveKind {
5961
/// Returns both the kind and the path.
6062
///
61-
/// Returns `None` if the command isn't from jsondocck (e.g. from compiletest).
63+
/// Returns `None` if the directive isn't from jsondocck (e.g. from compiletest).
6264
pub fn parse<'a>(
63-
command_name: &str,
65+
directive_name: &str,
6466
negated: bool,
6567
args: &'a [String],
6668
) -> Option<(Self, &'a str)> {
67-
let kind = match (command_name, negated) {
69+
let kind = match (directive_name, negated) {
6870
("count", false) => {
6971
assert_eq!(args.len(), 2);
7072
let expected = args[1].parse().expect("invalid number for `count`");
@@ -104,42 +106,42 @@ impl CommandKind {
104106
_ => panic!("`//@ !has` must have 2 or 3 arguments, but got {args:?}"),
105107
},
106108

107-
(_, false) if KNOWN_DIRECTIVE_NAMES.contains(&command_name) => {
109+
(_, false) if KNOWN_DIRECTIVE_NAMES.contains(&directive_name) => {
108110
return None;
109111
}
110112
_ => {
111-
panic!("Invalid command `//@ {}{command_name}`", if negated { "!" } else { "" })
113+
panic!("Invalid directive `//@ {}{directive_name}`", if negated { "!" } else { "" })
112114
}
113115
};
114116

115117
Some((kind, &args[0]))
116118
}
117119
}
118120

119-
impl Command {
120-
/// Performs the actual work of ensuring a command passes.
121+
impl Directive {
122+
/// Performs the actual work of ensuring a directive passes.
121123
pub fn check(&self, cache: &mut Cache) -> Result<(), String> {
122124
let matches = cache.select(&self.path);
123125
match &self.kind {
124-
CommandKind::HasPath => {
126+
DirectiveKind::HasPath => {
125127
if matches.is_empty() {
126128
return Err("matched to no values".to_owned());
127129
}
128130
}
129-
CommandKind::HasNotPath => {
131+
DirectiveKind::HasNotPath => {
130132
if !matches.is_empty() {
131133
return Err(format!("matched to {matches:?}, but wanted no matches"));
132134
}
133135
}
134-
CommandKind::HasValue { value } => {
136+
DirectiveKind::HasValue { value } => {
135137
let want_value = string_to_value(value, cache);
136138
if !matches.contains(&want_value.as_ref()) {
137139
return Err(format!(
138140
"matched to {matches:?}, which didn't contain {want_value:?}"
139141
));
140142
}
141143
}
142-
CommandKind::HasNotValue { value } => {
144+
DirectiveKind::HasNotValue { value } => {
143145
let wantnt_value = string_to_value(value, cache);
144146
if matches.contains(&wantnt_value.as_ref()) {
145147
return Err(format!(
@@ -152,22 +154,22 @@ impl Command {
152154
}
153155
}
154156

155-
CommandKind::Is { value } => {
157+
DirectiveKind::Is { value } => {
156158
let want_value = string_to_value(value, cache);
157159
let matched = get_one(&matches)?;
158160
if matched != want_value.as_ref() {
159161
return Err(format!("matched to {matched:?} but want {want_value:?}"));
160162
}
161163
}
162-
CommandKind::IsNot { value } => {
164+
DirectiveKind::IsNot { value } => {
163165
let wantnt_value = string_to_value(value, cache);
164166
let matched = get_one(&matches)?;
165167
if matched == wantnt_value.as_ref() {
166168
return Err(format!("got value {wantnt_value:?}, but want anything else"));
167169
}
168170
}
169171

170-
CommandKind::IsMany { values } => {
172+
DirectiveKind::IsMany { values } => {
171173
// Serde json doesn't implement Ord or Hash for Value, so we must
172174
// use a Vec here. While in theory that makes setwize equality
173175
// O(n^2), in practice n will never be large enough to matter.
@@ -187,15 +189,15 @@ impl Command {
187189
}
188190
}
189191
}
190-
CommandKind::CountIs { expected } => {
192+
DirectiveKind::CountIs { expected } => {
191193
if *expected != matches.len() {
192194
return Err(format!(
193195
"matched to `{matches:?}` with length {}, but expected length {expected}",
194196
matches.len(),
195197
));
196198
}
197199
}
198-
CommandKind::Set { variable } => {
200+
DirectiveKind::Set { variable } => {
199201
let value = get_one(&matches)?;
200202
let r = cache.variables.insert(variable.to_owned(), value.clone());
201203
assert!(r.is_none(), "name collision: {variable:?} is duplicated");
@@ -227,4 +229,4 @@ fn string_to_value<'a>(s: &str, cache: &'a Cache) -> Cow<'a, Value> {
227229
} else {
228230
Cow::Owned(serde_json::from_str(s).expect(&format!("Cannot convert `{}` to json", s)))
229231
}
230-
}
232+
}

src/tools/jsondocck/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::Command;
1+
use crate::Directive;
22

33
#[derive(Debug)]
44
pub struct CkError {
55
pub message: String,
6-
pub command: Command,
6+
pub directive: Directive,
77
}

src/tools/jsondocck/src/main.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,30 @@ mod error;
1111

1212
use cache::Cache;
1313
use config::parse_config;
14-
use directive::{Command, CommandKind};
14+
use directive::{Directive, DirectiveKind};
1515
use error::CkError;
1616

1717
fn main() -> ExitCode {
1818
let config = parse_config(env::args().collect());
1919

2020
let mut failed = Vec::new();
2121
let mut cache = Cache::new(&config);
22-
let Ok(commands) = get_commands(&config.template) else {
22+
let Ok(directives) = get_directives(&config.template) else {
2323
eprintln!("Jsondocck failed for {}", &config.template);
2424
return ExitCode::FAILURE;
2525
};
2626

27-
for command in commands {
28-
if let Err(message) = command.check( &mut cache) {
29-
failed.push(CkError { command, message });
27+
for directive in directives {
28+
if let Err(message) = directive.check(&mut cache) {
29+
failed.push(CkError { directive, message });
3030
}
3131
}
3232

3333
if failed.is_empty() {
3434
ExitCode::SUCCESS
3535
} else {
3636
for i in failed {
37-
eprintln!("{}:{}, command failed", config.template, i.command.lineno);
37+
eprintln!("{}:{}, directive failed", config.template, i.directive.lineno);
3838
eprintln!("{}", i.message)
3939
}
4040
ExitCode::FAILURE
@@ -70,20 +70,20 @@ static DEPRECATED_LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
7070
});
7171

7272
fn print_err(msg: &str, lineno: usize) {
73-
eprintln!("Invalid command: {} on line {}", msg, lineno)
73+
eprintln!("Invalid directive: {} on line {}", msg, lineno)
7474
}
7575

76-
/// Get a list of commands from a file.
77-
fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
78-
let mut commands = Vec::new();
76+
/// Get a list of directives from a file.
77+
fn get_directives(template: &str) -> Result<Vec<Directive>, ()> {
78+
let mut directives = Vec::new();
7979
let mut errors = false;
8080
let file = fs::read_to_string(template).unwrap();
8181

8282
for (lineno, line) in file.split('\n').enumerate() {
8383
let lineno = lineno + 1;
8484

8585
if DEPRECATED_LINE_PATTERN.is_match(line) {
86-
print_err("Deprecated command syntax, replace `// @` with `//@ `", lineno);
86+
print_err("Deprecated directive syntax, replace `// @` with `//@ `", lineno);
8787
errors = true;
8888
continue;
8989
}
@@ -101,10 +101,10 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
101101
continue;
102102
};
103103

104-
if let Some((kind, path)) = CommandKind::parse(&cap["cmd"], negated, &args) {
105-
commands.push(Command { kind, lineno, path: path.to_owned() })
104+
if let Some((kind, path)) = DirectiveKind::parse(&cap["cmd"], negated, &args) {
105+
directives.push(Directive { kind, lineno, path: path.to_owned() })
106106
}
107107
}
108108

109-
if !errors { Ok(commands) } else { Err(()) }
109+
if !errors { Ok(directives) } else { Err(()) }
110110
}

0 commit comments

Comments
 (0)