Skip to content

Commit 5ce218d

Browse files
committed
Added PatterErrorKind enum + thiserror dependency
1 parent 4d1967f commit 5ce218d

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ rust-version = "1.63.0"
1515
[dev-dependencies]
1616
tempfile = "3"
1717
doc-comment = "0.3"
18+
19+
[dependencies]
20+
thiserror = "2.0.17"

src/lib.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -500,14 +500,23 @@ pub struct PatternError {
500500
/// The approximate character index of where the error occurred.
501501
pub pos: usize,
502502

503-
/// A message describing the error.
504-
pub msg: &'static str,
503+
/// Specific kind of pattern error.
504+
pub msg: PatternErrorKind,
505505
}
506506

507-
impl Error for PatternError {
508-
fn description(&self) -> &str {
509-
self.msg
510-
}
507+
/// Define kinds of Error that can happen during parsing Pattern
508+
#[derive(Debug, PartialEq, Eq, Clone, Copy, thiserror::Error)]
509+
#[non_exhaustive]
510+
pub enum PatternErrorKind {
511+
/// Wildcard should be only `*` or `**`
512+
#[error("wildcards are either regular `*` or recursive `**`")]
513+
InvalidWildcards,
514+
/// Recursive wildcard should be in `**/` | `a/**/b` | `a/**` structure
515+
#[error("recursive wildcards must form a single path component")]
516+
InvalidRecursiveWildcards,
517+
/// Range pattern should be enclosed by `[]`
518+
#[error("invalid range pattern")]
519+
InvalidRange
511520
}
512521

513522
impl fmt::Display for PatternError {
@@ -597,11 +606,6 @@ enum MatchResult {
597606
EntirePatternDoesntMatch,
598607
}
599608

600-
const ERROR_WILDCARDS: &str = "wildcards are either regular `*` or recursive `**`";
601-
const ERROR_RECURSIVE_WILDCARDS: &str = "recursive wildcards must form a single path \
602-
component";
603-
const ERROR_INVALID_RANGE: &str = "invalid range pattern";
604-
605609
impl Pattern {
606610
/// This function compiles Unix shell style patterns.
607611
///
@@ -635,7 +639,7 @@ impl Pattern {
635639
Ordering::Greater => {
636640
return Err(PatternError {
637641
pos: old + 2,
638-
msg: ERROR_WILDCARDS,
642+
msg: PatternErrorKind::InvalidWildcards,
639643
})
640644
}
641645
Ordering::Equal => {
@@ -655,14 +659,14 @@ impl Pattern {
655659
} else {
656660
return Err(PatternError {
657661
pos: i,
658-
msg: ERROR_RECURSIVE_WILDCARDS,
662+
msg: PatternErrorKind::InvalidRecursiveWildcards,
659663
});
660664
}
661665
// `**` begins with non-separator
662666
} else {
663667
return Err(PatternError {
664668
pos: old - 1,
665-
msg: ERROR_RECURSIVE_WILDCARDS,
669+
msg: PatternErrorKind::InvalidRecursiveWildcards,
666670
});
667671
};
668672

@@ -712,7 +716,7 @@ impl Pattern {
712716
// if we get here then this is not a valid range pattern
713717
return Err(PatternError {
714718
pos: i,
715-
msg: ERROR_INVALID_RANGE,
719+
msg: PatternErrorKind::InvalidRange,
716720
});
717721
}
718722
c => {

0 commit comments

Comments
 (0)