@@ -239,7 +239,9 @@ impl<'a> Tree<'a> {
239239 /// Parses a tree from a string
240240 #[ allow( clippy:: should_implement_trait) ] // Cannot use std::str::FromStr because of lifetimes.
241241 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 )
243245 }
244246
245247 fn from_str_inner ( s : & ' a str ) -> Result < Self , ParseTreeError > {
@@ -387,6 +389,7 @@ where
387389#[ cfg( test) ]
388390mod tests {
389391 use super :: * ;
392+ use crate :: ParseError ;
390393
391394 /// Test functions to manually build trees
392395 fn leaf ( name : & str ) -> Tree {
@@ -429,75 +432,81 @@ mod tests {
429432
430433 assert ! ( matches!(
431434 Tree :: from_str( "thresh," ) . unwrap_err( ) ,
432- Error :: ParseTree ( ParseTreeError :: TrailingCharacter { ch: ',' , pos: 6 } ) ,
435+ Error :: Parse ( ParseError :: Tree ( ParseTreeError :: TrailingCharacter { ch: ',' , pos: 6 } ) ) ,
433436 ) ) ;
434437
435438 assert ! ( matches!(
436439 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 } ) ) ,
438441 ) ) ;
439442
440443 assert ! ( matches!(
441444 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 } ) ) ,
443446 ) ) ;
444447
445448 assert_eq ! ( Tree :: from_str( "thresh()" ) . unwrap( ) , paren_node( "thresh" , vec![ leaf( "" ) ] ) ) ;
446449
447450 assert ! ( matches!(
448451 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+ } ) ) ) ,
450456 ) ) ;
451457
452458 assert ! ( matches!(
453459 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+ } ) ) ) ,
455464 ) ) ;
456465 }
457466
458467 #[ test]
459468 fn parse_tree_parens ( ) {
460469 assert ! ( matches!(
461470 Tree :: from_str( "a(" ) . unwrap_err( ) ,
462- Error :: ParseTree ( ParseTreeError :: UnmatchedOpenParen { ch: '(' , pos: 1 } ) ,
471+ Error :: Parse ( ParseError :: Tree ( ParseTreeError :: UnmatchedOpenParen { ch: '(' , pos: 1 } ) ) ,
463472 ) ) ;
464473
465474 assert ! ( matches!(
466475 Tree :: from_str( ")" ) . unwrap_err( ) ,
467- Error :: ParseTree ( ParseTreeError :: UnmatchedCloseParen { ch: ')' , pos: 0 } ) ,
476+ Error :: Parse ( ParseError :: Tree ( ParseTreeError :: UnmatchedCloseParen { ch: ')' , pos: 0 } ) ) ,
468477 ) ) ;
469478
470479 assert ! ( matches!(
471480 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 } ) ) ,
473482 ) ) ;
474483
475484 /* Will be enabled in a later PR which unifies TR and non-TR parsing.
476485 assert!(matches!(
477486 Tree::from_str("a{").unwrap_err(),
478- Error::ParseTree( ParseTreeError::UnmatchedOpenParen { ch: '{', pos: 1 }),
487+ Error::Parse(ParseError::Tree( ParseTreeError::UnmatchedOpenParen { ch: '{', pos: 1 }) ),
479488 ));
480489
481490 assert!(matches!(
482491 Tree::from_str("}").unwrap_err(),
483- Error::ParseTree( ParseTreeError::UnmatchedCloseParen { ch: '}', pos: 0 }),
492+ Error::Parse(ParseError::Tree( ParseTreeError::UnmatchedCloseParen { ch: '}', pos: 0 }) ),
484493 ));
485494 */
486495
487496 assert ! ( matches!(
488497 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 } ) ) ,
490499 ) ) ;
491500
492501 /* Will be enabled in a later PR which unifies TR and non-TR parsing.
493502 assert!(matches!(
494503 Tree::from_str("x{y)").unwrap_err(),
495- Error::ParseTree (ParseTreeError::MismatchedParens {
504+ Error::Parse(ParseError::Tree (ParseTreeError::MismatchedParens {
496505 open_ch: '{',
497506 open_pos: 1,
498507 close_ch: ')',
499508 close_pos: 3,
500- }),
509+ }),)
501510 ));
502511 */
503512 }
0 commit comments