Skip to content

Commit 0974796

Browse files
committed
Added PatterErrorKind enum + thiserror dependency
1 parent e0b33b0 commit 0974796

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

Cargo.toml

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

src/lib.rs

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

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

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

512521
impl fmt::Display for PatternError {
@@ -596,11 +605,6 @@ enum MatchResult {
596605
EntirePatternDoesntMatch,
597606
}
598607

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-
604608
impl Pattern {
605609
/// This function compiles Unix shell style patterns.
606610
///
@@ -634,7 +638,7 @@ impl Pattern {
634638
Ordering::Greater => {
635639
return Err(PatternError {
636640
pos: old + 2,
637-
msg: ERROR_WILDCARDS,
641+
msg: PatternErrorKind::InvalidWildcards,
638642
})
639643
}
640644
Ordering::Equal => {
@@ -654,14 +658,14 @@ impl Pattern {
654658
} else {
655659
return Err(PatternError {
656660
pos: i,
657-
msg: ERROR_RECURSIVE_WILDCARDS,
661+
msg: PatternErrorKind::InvalidRecursiveWildcards,
658662
});
659663
}
660664
// `**` begins with non-separator
661665
} else {
662666
return Err(PatternError {
663667
pos: old - 1,
664-
msg: ERROR_RECURSIVE_WILDCARDS,
668+
msg: PatternErrorKind::InvalidRecursiveWildcards,
665669
});
666670
};
667671

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

0 commit comments

Comments
 (0)