@@ -259,66 +259,76 @@ make_parser!(
259
259
) ;
260
260
261
261
make_parser ! (
262
- Alias ( input: char ) -> Option <Name > {
263
- optional(
264
- NameP :: new( )
265
- . skip( many:: <Vec <_>, _>( or( WhiteSpace :: new( ) , LineTerminator :: new( & true ) ) ) )
266
- . skip( char ( ':' ) )
267
- )
262
+ Alias ( input: char ) -> Name {
263
+ NameP :: new( )
264
+ . skip( many:: <Vec <_>, _>( or( WhiteSpace :: new( ) , LineTerminator :: new( & true ) ) ) )
265
+ . skip( char ( ':' ) )
268
266
. parse_stream( input)
269
267
}
270
268
) ;
271
269
272
- // pub fn operation_definition<I: U8Input>(i: I) -> SimpleResult<I,Definition>
273
- // {
274
- // parse!{i;
275
- // let op_type = operation_type();
276
- // white_space() <|> line_terminator();
277
- // let name = option(|i| name(i).map(Some), None);
278
-
279
- // ret {
280
- // let op = Operation::new(op_type, name, Vec::new(), Vec::new());
281
- // Definition::OperationDefinition(op)
282
- // }
283
- // }
284
- // }
270
+ make_parser ! (
271
+ OperationDefinition ( input: char ) -> Operation {
272
+ OperationTypeP :: new( )
273
+ . skip( many:: <Vec <_>, _>( or( WhiteSpace :: new( ) , LineTerminator :: new( & true ) ) ) )
274
+ . and( optional( NameP :: new( ) ) )
275
+ . map( |( op_type, name) | {
276
+ Operation :: new( op_type, name, Vec :: new( ) , Vec :: new( ) )
277
+ } )
278
+ . parse_stream( input)
279
+ }
280
+ ) ;
285
281
286
282
#[ cfg( test) ]
287
283
mod tests {
288
284
use super :: * ;
289
285
use combine:: { Parser , State } ;
290
286
291
- macro_rules! assert_sucessful_parse {
287
+ macro_rules! assert_successful_parse {
292
288
( $parser: ident, $input: expr, $result: expr) => {
293
289
assert_eq!( $parser:: new( ) . parse( State :: new( $input) ) . map( |x| x. 0 ) , Ok ( $result) ) ;
294
290
}
295
291
}
296
292
297
293
#[ test]
298
294
fn test_parse_comment ( ) {
299
- assert_sucessful_parse ! ( LineComment , "#hello world\r \n " , ( ) ) ;
295
+ assert_successful_parse ! ( LineComment , "#hello world\r \n " , ( ) ) ;
300
296
}
301
297
302
298
#[ test]
303
299
fn test_parse_operationtype ( ) {
304
- assert_sucessful_parse ! ( OperationTypeP , "query" , OperationType :: Query ) ;
305
- assert_sucessful_parse ! ( OperationTypeP , "mutation" , OperationType :: Mutation ) ;
300
+ assert_successful_parse ! ( OperationTypeP , "query" , OperationType :: Query ) ;
301
+ assert_successful_parse ! ( OperationTypeP , "mutation" , OperationType :: Mutation ) ;
306
302
}
307
303
308
304
#[ test]
309
305
fn test_parse_name ( ) {
310
- assert_sucessful_parse ! ( NameP , "_asd" , String :: from( "_asd" ) ) ;
311
- assert_sucessful_parse ! ( NameP , "aasd" , String :: from( "aasd" ) ) ;
312
- assert_sucessful_parse ! ( NameP , "zasd" , String :: from( "zasd" ) ) ;
313
- assert_sucessful_parse ! ( NameP , "Aasd" , String :: from( "Aasd" ) ) ;
314
- assert_sucessful_parse ! ( NameP , "Zasd" , String :: from( "Zasd" ) ) ;
306
+ assert_successful_parse ! ( NameP , "_asd" , String :: from( "_asd" ) ) ;
307
+ assert_successful_parse ! ( NameP , "aasd" , String :: from( "aasd" ) ) ;
308
+ assert_successful_parse ! ( NameP , "zasd" , String :: from( "zasd" ) ) ;
309
+ assert_successful_parse ! ( NameP , "Aasd" , String :: from( "Aasd" ) ) ;
310
+ assert_successful_parse ! ( NameP , "Zasd" , String :: from( "Zasd" ) ) ;
315
311
}
316
312
317
313
#[ test]
318
314
fn test_parse_alias ( ) {
319
- assert_sucessful_parse ! ( Alias , "asd:" , Some ( String :: from( "asd" ) ) ) ;
320
- assert_sucessful_parse ! ( Alias , "asd :" , Some ( String :: from( "asd" ) ) ) ;
321
- assert_sucessful_parse ! ( Alias , "asd \r \n :" , Some ( String :: from( "asd" ) ) ) ;
322
- // assert_sucessful_parse!(Alias, "asd", None);
315
+ assert_successful_parse ! ( Alias , "asd:" , String :: from( "asd" ) ) ;
316
+ assert_successful_parse ! ( Alias , "asd :" , String :: from( "asd" ) ) ;
317
+ assert_successful_parse ! ( Alias , "asd \r \n :" , String :: from( "asd" ) ) ;
318
+ }
319
+
320
+ #[ test]
321
+ fn test_parse_operation ( ) {
322
+ // named operation
323
+ {
324
+ let result = Operation :: new ( OperationType :: Mutation , Some ( String :: from ( "test" ) ) , Vec :: new ( ) , Vec :: new ( ) ) ;
325
+ assert_successful_parse ! ( OperationDefinition , "mutation test" , result) ;
326
+ }
327
+
328
+ // non named
329
+ {
330
+ let result = Operation :: new ( OperationType :: Mutation , None , Vec :: new ( ) , Vec :: new ( ) ) ;
331
+ assert_successful_parse ! ( OperationDefinition , "mutation" , result) ;
332
+ }
323
333
}
324
334
}
0 commit comments