Skip to content

Commit 759ed3a

Browse files
committed
Added PatterErrorKind enum
1 parent 335da33 commit 759ed3a

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

src/lib.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -502,30 +502,54 @@ impl Iterator for Paths {
502502
/// A pattern parsing error.
503503
#[derive(Debug)]
504504
#[allow(missing_copy_implementations)]
505+
#[non_exhaustive]
505506
pub struct PatternError {
506507
/// The approximate character index of where the error occurred.
507508
pub pos: usize,
508509

509-
/// A message describing the error.
510-
pub msg: &'static str,
510+
/// Specific kind of pattern error.
511+
pub kind: PatternErrorKind,
511512
}
512513

513-
impl Error for PatternError {
514-
fn description(&self) -> &str {
515-
self.msg
516-
}
517-
}
514+
impl Error for PatternError {}
518515

519516
impl fmt::Display for PatternError {
520517
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
521518
write!(
522519
f,
523520
"Pattern syntax error near position {}: {}",
524-
self.pos, self.msg
521+
self.pos, self.kind
525522
)
526523
}
527524
}
528525

526+
/// Define kinds of Error that can happen during parsing Pattern
527+
#[derive(Debug, PartialEq, Clone)]
528+
#[non_exhaustive]
529+
pub enum PatternErrorKind {
530+
/// Wildcard should be only `*` or `**`
531+
InvalidWildcards,
532+
/// Recursive wildcard should be in `**/` | `a/**/b` | `a/**` structure
533+
InvalidRecursiveWildcards,
534+
/// Range pattern should be enclosed by `[]`
535+
InvalidRange,
536+
}
537+
538+
impl fmt::Display for PatternErrorKind {
539+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
540+
let msg = match self {
541+
PatternErrorKind::InvalidWildcards => {
542+
"wildcards are either regular `*` or recursive `**`"
543+
}
544+
PatternErrorKind::InvalidRecursiveWildcards => {
545+
"recursive wildcards must form a single path component"
546+
}
547+
PatternErrorKind::InvalidRange => "invalid range pattern",
548+
};
549+
write!(f, "{}", msg)
550+
}
551+
}
552+
529553
/// A compiled Unix shell style pattern.
530554
///
531555
/// - `?` matches any single character.
@@ -603,11 +627,6 @@ enum MatchResult {
603627
EntirePatternDoesntMatch,
604628
}
605629

606-
const ERROR_WILDCARDS: &str = "wildcards are either regular `*` or recursive `**`";
607-
const ERROR_RECURSIVE_WILDCARDS: &str = "recursive wildcards must form a single path \
608-
component";
609-
const ERROR_INVALID_RANGE: &str = "invalid range pattern";
610-
611630
impl Pattern {
612631
/// This function compiles Unix shell style patterns.
613632
///
@@ -641,7 +660,7 @@ impl Pattern {
641660
Ordering::Greater => {
642661
return Err(PatternError {
643662
pos: old + 2,
644-
msg: ERROR_WILDCARDS,
663+
kind: PatternErrorKind::InvalidWildcards,
645664
})
646665
}
647666
Ordering::Equal => {
@@ -661,14 +680,14 @@ impl Pattern {
661680
} else {
662681
return Err(PatternError {
663682
pos: i,
664-
msg: ERROR_RECURSIVE_WILDCARDS,
683+
kind: PatternErrorKind::InvalidRecursiveWildcards,
665684
});
666685
}
667686
// `**` begins with non-separator
668687
} else {
669688
return Err(PatternError {
670689
pos: old - 1,
671-
msg: ERROR_RECURSIVE_WILDCARDS,
690+
kind: PatternErrorKind::InvalidRecursiveWildcards,
672691
});
673692
};
674693

@@ -718,7 +737,7 @@ impl Pattern {
718737
// if we get here then this is not a valid range pattern
719738
return Err(PatternError {
720739
pos: i,
721-
msg: ERROR_INVALID_RANGE,
740+
kind: PatternErrorKind::InvalidRange,
722741
});
723742
}
724743
c => {

0 commit comments

Comments
 (0)