@@ -239,7 +239,9 @@ impl<'a> Tree<'a> {
239
239
/// Parses a tree from a string
240
240
#[ allow( clippy:: should_implement_trait) ] // Cannot use std::str::FromStr because of lifetimes.
241
241
pub fn from_str ( s : & ' a str ) -> Result < Self , Error > {
242
- Self :: from_str_inner ( s) . map_err ( Error :: ParseTree )
242
+ Self :: from_str_inner ( s)
243
+ . map_err ( From :: from)
244
+ . map_err ( Error :: Parse )
243
245
}
244
246
245
247
fn from_str_inner ( s : & ' a str ) -> Result < Self , ParseTreeError > {
@@ -387,6 +389,7 @@ where
387
389
#[ cfg( test) ]
388
390
mod tests {
389
391
use super :: * ;
392
+ use crate :: ParseError ;
390
393
391
394
/// Test functions to manually build trees
392
395
fn leaf ( name : & str ) -> Tree {
@@ -429,75 +432,81 @@ mod tests {
429
432
430
433
assert ! ( matches!(
431
434
Tree :: from_str( "thresh," ) . unwrap_err( ) ,
432
- Error :: ParseTree ( ParseTreeError :: TrailingCharacter { ch: ',' , pos: 6 } ) ,
435
+ Error :: Parse ( ParseError :: Tree ( ParseTreeError :: TrailingCharacter { ch: ',' , pos: 6 } ) ) ,
433
436
) ) ;
434
437
435
438
assert ! ( matches!(
436
439
Tree :: from_str( "thresh,thresh" ) . unwrap_err( ) ,
437
- Error :: ParseTree ( ParseTreeError :: TrailingCharacter { ch: ',' , pos: 6 } ) ,
440
+ Error :: Parse ( ParseError :: Tree ( ParseTreeError :: TrailingCharacter { ch: ',' , pos: 6 } ) ) ,
438
441
) ) ;
439
442
440
443
assert ! ( matches!(
441
444
Tree :: from_str( "thresh()thresh()" ) . unwrap_err( ) ,
442
- Error :: ParseTree ( ParseTreeError :: TrailingCharacter { ch: 't' , pos: 8 } ) ,
445
+ Error :: Parse ( ParseError :: Tree ( ParseTreeError :: TrailingCharacter { ch: 't' , pos: 8 } ) ) ,
443
446
) ) ;
444
447
445
448
assert_eq ! ( Tree :: from_str( "thresh()" ) . unwrap( ) , paren_node( "thresh" , vec![ leaf( "" ) ] ) ) ;
446
449
447
450
assert ! ( matches!(
448
451
Tree :: from_str( "thresh(a()b)" ) ,
449
- Err ( Error :: ParseTree ( ParseTreeError :: ExpectedParenOrComma { ch: 'b' , pos: 10 } ) ) ,
452
+ Err ( Error :: Parse ( ParseError :: Tree ( ParseTreeError :: ExpectedParenOrComma {
453
+ ch: 'b' ,
454
+ pos: 10
455
+ } ) ) ) ,
450
456
) ) ;
451
457
452
458
assert ! ( matches!(
453
459
Tree :: from_str( "thresh()xyz" ) ,
454
- Err ( Error :: ParseTree ( ParseTreeError :: TrailingCharacter { ch: 'x' , pos: 8 } ) ) ,
460
+ Err ( Error :: Parse ( ParseError :: Tree ( ParseTreeError :: TrailingCharacter {
461
+ ch: 'x' ,
462
+ pos: 8
463
+ } ) ) ) ,
455
464
) ) ;
456
465
}
457
466
458
467
#[ test]
459
468
fn parse_tree_parens ( ) {
460
469
assert ! ( matches!(
461
470
Tree :: from_str( "a(" ) . unwrap_err( ) ,
462
- Error :: ParseTree ( ParseTreeError :: UnmatchedOpenParen { ch: '(' , pos: 1 } ) ,
471
+ Error :: Parse ( ParseError :: Tree ( ParseTreeError :: UnmatchedOpenParen { ch: '(' , pos: 1 } ) ) ,
463
472
) ) ;
464
473
465
474
assert ! ( matches!(
466
475
Tree :: from_str( ")" ) . unwrap_err( ) ,
467
- Error :: ParseTree ( ParseTreeError :: UnmatchedCloseParen { ch: ')' , pos: 0 } ) ,
476
+ Error :: Parse ( ParseError :: Tree ( ParseTreeError :: UnmatchedCloseParen { ch: ')' , pos: 0 } ) ) ,
468
477
) ) ;
469
478
470
479
assert ! ( matches!(
471
480
Tree :: from_str( "x(y))" ) . unwrap_err( ) ,
472
- Error :: ParseTree ( ParseTreeError :: TrailingCharacter { ch: ')' , pos: 4 } ) ,
481
+ Error :: Parse ( ParseError :: Tree ( ParseTreeError :: TrailingCharacter { ch: ')' , pos: 4 } ) ) ,
473
482
) ) ;
474
483
475
484
/* Will be enabled in a later PR which unifies TR and non-TR parsing.
476
485
assert!(matches!(
477
486
Tree::from_str("a{").unwrap_err(),
478
- Error::ParseTree( ParseTreeError::UnmatchedOpenParen { ch: '{', pos: 1 }),
487
+ Error::Parse(ParseError::Tree( ParseTreeError::UnmatchedOpenParen { ch: '{', pos: 1 }) ),
479
488
));
480
489
481
490
assert!(matches!(
482
491
Tree::from_str("}").unwrap_err(),
483
- Error::ParseTree( ParseTreeError::UnmatchedCloseParen { ch: '}', pos: 0 }),
492
+ Error::Parse(ParseError::Tree( ParseTreeError::UnmatchedCloseParen { ch: '}', pos: 0 }) ),
484
493
));
485
494
*/
486
495
487
496
assert ! ( matches!(
488
497
Tree :: from_str( "x(y)}" ) . unwrap_err( ) ,
489
- Error :: ParseTree ( ParseTreeError :: TrailingCharacter { ch: '}' , pos: 4 } ) ,
498
+ Error :: Parse ( ParseError :: Tree ( ParseTreeError :: TrailingCharacter { ch: '}' , pos: 4 } ) ) ,
490
499
) ) ;
491
500
492
501
/* Will be enabled in a later PR which unifies TR and non-TR parsing.
493
502
assert!(matches!(
494
503
Tree::from_str("x{y)").unwrap_err(),
495
- Error::ParseTree (ParseTreeError::MismatchedParens {
504
+ Error::Parse(ParseError::Tree (ParseTreeError::MismatchedParens {
496
505
open_ch: '{',
497
506
open_pos: 1,
498
507
close_ch: ')',
499
508
close_pos: 3,
500
- }),
509
+ }),)
501
510
));
502
511
*/
503
512
}
0 commit comments