@@ -495,30 +495,54 @@ impl Iterator for Paths {
495495/// A pattern parsing error.
496496#[ derive( Debug ) ]
497497#[ allow( missing_copy_implementations) ]
498+ #[ non_exhaustive]
498499pub struct PatternError {
499500 /// The approximate character index of where the error occurred.
500501 pub pos : usize ,
501502
502- /// A message describing the error.
503- pub msg : & ' static str ,
503+ /// Specific kind of pattern error.
504+ pub kind : PatternErrorKind ,
504505}
505506
506- impl Error for PatternError {
507- fn description ( & self ) -> & str {
508- self . msg
509- }
510- }
507+ impl Error for PatternError { }
511508
512509impl fmt:: Display for PatternError {
513510 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
514511 write ! (
515512 f,
516513 "Pattern syntax error near position {}: {}" ,
517- self . pos, self . msg
514+ self . pos, self . kind
518515 )
519516 }
520517}
521518
519+ /// Define kinds of Error that can happen during parsing Pattern
520+ #[ derive( Debug , PartialEq , Clone ) ]
521+ #[ non_exhaustive]
522+ pub enum PatternErrorKind {
523+ /// Wildcard should be only `*` or `**`
524+ InvalidWildcards ,
525+ /// Recursive wildcard should be in `**/` | `a/**/b` | `a/**` structure
526+ InvalidRecursiveWildcards ,
527+ /// Range pattern should be enclosed by `[]`
528+ InvalidRange ,
529+ }
530+
531+ impl fmt:: Display for PatternErrorKind {
532+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
533+ let msg = match self {
534+ PatternErrorKind :: InvalidWildcards => {
535+ "Wildcards are either regular `*` or recursive `**`"
536+ }
537+ PatternErrorKind :: InvalidRecursiveWildcards => {
538+ "Recursive wildcards must form a single path component"
539+ }
540+ PatternErrorKind :: InvalidRange => "Invalid range pattern" ,
541+ } ;
542+ write ! ( f, "{}" , msg)
543+ }
544+ }
545+
522546/// A compiled Unix shell style pattern.
523547///
524548/// - `?` matches any single character.
@@ -596,11 +620,6 @@ enum MatchResult {
596620 EntirePatternDoesntMatch ,
597621}
598622
599- const ERROR_WILDCARDS : & str = "wildcards are either regular `*` or recursive `**`" ;
600- const ERROR_RECURSIVE_WILDCARDS : & str = "recursive wildcards must form a single path \
601- component";
602- const ERROR_INVALID_RANGE : & str = "invalid range pattern" ;
603-
604623impl Pattern {
605624 /// This function compiles Unix shell style patterns.
606625 ///
@@ -634,7 +653,7 @@ impl Pattern {
634653 Ordering :: Greater => {
635654 return Err ( PatternError {
636655 pos : old + 2 ,
637- msg : ERROR_WILDCARDS ,
656+ kind : PatternErrorKind :: InvalidWildcards ,
638657 } )
639658 }
640659 Ordering :: Equal => {
@@ -654,14 +673,14 @@ impl Pattern {
654673 } else {
655674 return Err ( PatternError {
656675 pos : i,
657- msg : ERROR_RECURSIVE_WILDCARDS ,
676+ kind : PatternErrorKind :: InvalidRecursiveWildcards ,
658677 } ) ;
659678 }
660679 // `**` begins with non-separator
661680 } else {
662681 return Err ( PatternError {
663682 pos : old - 1 ,
664- msg : ERROR_RECURSIVE_WILDCARDS ,
683+ kind : PatternErrorKind :: InvalidRecursiveWildcards ,
665684 } ) ;
666685 } ;
667686
@@ -711,7 +730,7 @@ impl Pattern {
711730 // if we get here then this is not a valid range pattern
712731 return Err ( PatternError {
713732 pos : i,
714- msg : ERROR_INVALID_RANGE ,
733+ kind : PatternErrorKind :: InvalidRange ,
715734 } ) ;
716735 }
717736 c => {
0 commit comments