11use std:: borrow:: Cow ;
2+
23use serde_json:: Value ;
4+
35use 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+ }
0 commit comments