From 798f8981eb4fbbf8d06cbb2ad4722a1acf0ee841 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Mon, 25 Sep 2023 16:53:03 -0400 Subject: [PATCH] pcre2: small polishing --- crates/pcre2/Cargo.toml | 2 +- crates/pcre2/src/error.rs | 21 +++++---------------- crates/pcre2/src/lib.rs | 7 +++++-- crates/pcre2/src/matcher.rs | 9 ++++++--- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/crates/pcre2/Cargo.toml b/crates/pcre2/Cargo.toml index 2725ba7d..64450df0 100644 --- a/crates/pcre2/Cargo.toml +++ b/crates/pcre2/Cargo.toml @@ -15,5 +15,5 @@ edition = "2018" [dependencies] grep-matcher = { version = "0.1.6", path = "../matcher" } -log = "0.4.19" +log = "0.4.20" pcre2 = "0.2.4" diff --git a/crates/pcre2/src/error.rs b/crates/pcre2/src/error.rs index 921179cd..302e12ce 100644 --- a/crates/pcre2/src/error.rs +++ b/crates/pcre2/src/error.rs @@ -1,6 +1,3 @@ -use std::error; -use std::fmt; - /// An error that can occur in this crate. /// /// Generally, this error corresponds to problems building a regular @@ -12,7 +9,7 @@ pub struct Error { } impl Error { - pub(crate) fn regex(err: E) -> Error { + pub(crate) fn regex(err: E) -> Error { Error { kind: ErrorKind::Regex(err.to_string()) } } @@ -24,6 +21,7 @@ impl Error { /// The kind of an error that can occur. #[derive(Clone, Debug)] +#[non_exhaustive] pub enum ErrorKind { /// An error that occurred as a result of parsing a regular expression. /// This can be a syntax error or an error that results from attempting to @@ -31,29 +29,20 @@ pub enum ErrorKind { /// /// The string here is the underlying error converted to a string. Regex(String), - /// Hints that destructuring should not be exhaustive. - /// - /// This enum may grow additional variants, so this makes sure clients - /// don't count on exhaustive matching. (Otherwise, adding a new variant - /// could break existing code.) - #[doc(hidden)] - __Nonexhaustive, } -impl error::Error for Error { +impl std::error::Error for Error { fn description(&self) -> &str { match self.kind { ErrorKind::Regex(_) => "regex error", - ErrorKind::__Nonexhaustive => unreachable!(), } } } -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self.kind { ErrorKind::Regex(ref s) => write!(f, "{}", s), - ErrorKind::__Nonexhaustive => unreachable!(), } } } diff --git a/crates/pcre2/src/lib.rs b/crates/pcre2/src/lib.rs index b2e50a7b..f5951812 100644 --- a/crates/pcre2/src/lib.rs +++ b/crates/pcre2/src/lib.rs @@ -5,9 +5,12 @@ An implementation of `grep-matcher`'s `Matcher` trait for #![deny(missing_docs)] -pub use crate::error::{Error, ErrorKind}; -pub use crate::matcher::{RegexCaptures, RegexMatcher, RegexMatcherBuilder}; pub use pcre2::{is_jit_available, version}; +pub use crate::{ + error::{Error, ErrorKind}, + matcher::{RegexCaptures, RegexMatcher, RegexMatcherBuilder}, +}; + mod error; mod matcher; diff --git a/crates/pcre2/src/matcher.rs b/crates/pcre2/src/matcher.rs index 66c2b74a..ecc7c16f 100644 --- a/crates/pcre2/src/matcher.rs +++ b/crates/pcre2/src/matcher.rs @@ -1,7 +1,9 @@ use std::collections::HashMap; -use grep_matcher::{Captures, Match, Matcher}; -use pcre2::bytes::{CaptureLocations, Regex, RegexBuilder}; +use { + grep_matcher::{Captures, Match, Matcher}, + pcre2::bytes::{CaptureLocations, Regex, RegexBuilder}, +}; use crate::error::Error; @@ -426,9 +428,10 @@ fn has_uppercase_literal(pattern: &str) -> bool { #[cfg(test)] mod tests { - use super::*; use grep_matcher::{LineMatchKind, Matcher}; + use super::*; + // Test that enabling word matches does the right thing and demonstrate // the difference between it and surrounding the regex in `\b`. #[test]