1
1
use std:: borrow:: Cow ;
2
+
2
3
use serde_json:: Value ;
4
+
3
5
use crate :: cache:: Cache ;
4
6
5
7
#[ derive( Debug ) ]
6
- pub struct Command {
7
- pub kind : CommandKind ,
8
+ pub struct Directive {
9
+ pub kind : DirectiveKind ,
8
10
pub path : String ,
9
11
pub lineno : usize ,
10
12
}
11
13
12
14
#[ derive( Debug ) ]
13
- pub enum CommandKind {
15
+ pub enum DirectiveKind {
14
16
/// `//@ has <path>`
15
17
///
16
18
/// Checks the path exists.
@@ -55,16 +57,16 @@ pub enum CommandKind {
55
57
Set { variable : String } ,
56
58
}
57
59
58
- impl CommandKind {
60
+ impl DirectiveKind {
59
61
/// Returns both the kind and the path.
60
62
///
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).
62
64
pub fn parse < ' a > (
63
- command_name : & str ,
65
+ directive_name : & str ,
64
66
negated : bool ,
65
67
args : & ' a [ String ] ,
66
68
) -> Option < ( Self , & ' a str ) > {
67
- let kind = match ( command_name , negated) {
69
+ let kind = match ( directive_name , negated) {
68
70
( "count" , false ) => {
69
71
assert_eq ! ( args. len( ) , 2 ) ;
70
72
let expected = args[ 1 ] . parse ( ) . expect ( "invalid number for `count`" ) ;
@@ -104,42 +106,42 @@ impl CommandKind {
104
106
_ => panic ! ( "`//@ !has` must have 2 or 3 arguments, but got {args:?}" ) ,
105
107
} ,
106
108
107
- ( _, false ) if KNOWN_DIRECTIVE_NAMES . contains ( & command_name ) => {
109
+ ( _, false ) if KNOWN_DIRECTIVE_NAMES . contains ( & directive_name ) => {
108
110
return None ;
109
111
}
110
112
_ => {
111
- panic ! ( "Invalid command `//@ {}{command_name }`" , if negated { "!" } else { "" } )
113
+ panic ! ( "Invalid directive `//@ {}{directive_name }`" , if negated { "!" } else { "" } )
112
114
}
113
115
} ;
114
116
115
117
Some ( ( kind, & args[ 0 ] ) )
116
118
}
117
119
}
118
120
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.
121
123
pub fn check ( & self , cache : & mut Cache ) -> Result < ( ) , String > {
122
124
let matches = cache. select ( & self . path ) ;
123
125
match & self . kind {
124
- CommandKind :: HasPath => {
126
+ DirectiveKind :: HasPath => {
125
127
if matches. is_empty ( ) {
126
128
return Err ( "matched to no values" . to_owned ( ) ) ;
127
129
}
128
130
}
129
- CommandKind :: HasNotPath => {
131
+ DirectiveKind :: HasNotPath => {
130
132
if !matches. is_empty ( ) {
131
133
return Err ( format ! ( "matched to {matches:?}, but wanted no matches" ) ) ;
132
134
}
133
135
}
134
- CommandKind :: HasValue { value } => {
136
+ DirectiveKind :: HasValue { value } => {
135
137
let want_value = string_to_value ( value, cache) ;
136
138
if !matches. contains ( & want_value. as_ref ( ) ) {
137
139
return Err ( format ! (
138
140
"matched to {matches:?}, which didn't contain {want_value:?}"
139
141
) ) ;
140
142
}
141
143
}
142
- CommandKind :: HasNotValue { value } => {
144
+ DirectiveKind :: HasNotValue { value } => {
143
145
let wantnt_value = string_to_value ( value, cache) ;
144
146
if matches. contains ( & wantnt_value. as_ref ( ) ) {
145
147
return Err ( format ! (
@@ -152,22 +154,22 @@ impl Command {
152
154
}
153
155
}
154
156
155
- CommandKind :: Is { value } => {
157
+ DirectiveKind :: Is { value } => {
156
158
let want_value = string_to_value ( value, cache) ;
157
159
let matched = get_one ( & matches) ?;
158
160
if matched != want_value. as_ref ( ) {
159
161
return Err ( format ! ( "matched to {matched:?} but want {want_value:?}" ) ) ;
160
162
}
161
163
}
162
- CommandKind :: IsNot { value } => {
164
+ DirectiveKind :: IsNot { value } => {
163
165
let wantnt_value = string_to_value ( value, cache) ;
164
166
let matched = get_one ( & matches) ?;
165
167
if matched == wantnt_value. as_ref ( ) {
166
168
return Err ( format ! ( "got value {wantnt_value:?}, but want anything else" ) ) ;
167
169
}
168
170
}
169
171
170
- CommandKind :: IsMany { values } => {
172
+ DirectiveKind :: IsMany { values } => {
171
173
// Serde json doesn't implement Ord or Hash for Value, so we must
172
174
// use a Vec here. While in theory that makes setwize equality
173
175
// O(n^2), in practice n will never be large enough to matter.
@@ -187,15 +189,15 @@ impl Command {
187
189
}
188
190
}
189
191
}
190
- CommandKind :: CountIs { expected } => {
192
+ DirectiveKind :: CountIs { expected } => {
191
193
if * expected != matches. len ( ) {
192
194
return Err ( format ! (
193
195
"matched to `{matches:?}` with length {}, but expected length {expected}" ,
194
196
matches. len( ) ,
195
197
) ) ;
196
198
}
197
199
}
198
- CommandKind :: Set { variable } => {
200
+ DirectiveKind :: Set { variable } => {
199
201
let value = get_one ( & matches) ?;
200
202
let r = cache. variables . insert ( variable. to_owned ( ) , value. clone ( ) ) ;
201
203
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> {
227
229
} else {
228
230
Cow :: Owned ( serde_json:: from_str ( s) . expect ( & format ! ( "Cannot convert `{}` to json" , s) ) )
229
231
}
230
- }
232
+ }
0 commit comments