Skip to content

Commit 7db8619

Browse files
committed
Added PatterErrorKind enum
1 parent e0b33b0 commit 7db8619

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ rust-version = "1.63.0"
1414

1515
[dev-dependencies]
1616
tempfile = "3"
17-
doc-comment = "0.3"
17+
doc-comment = "0.3"

src/lib.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -495,30 +495,54 @@ impl Iterator for Paths {
495495
/// A pattern parsing error.
496496
#[derive(Debug)]
497497
#[allow(missing_copy_implementations)]
498+
#[non_exhaustive]
498499
pub 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

512509
impl 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-
604623
impl 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

Comments
 (0)