pcre2: small polishing

This commit is contained in:
Andrew Gallant 2023-09-25 16:53:03 -04:00
parent 96f01b92a0
commit 798f8981eb
4 changed files with 17 additions and 22 deletions

View File

@ -15,5 +15,5 @@ edition = "2018"
[dependencies] [dependencies]
grep-matcher = { version = "0.1.6", path = "../matcher" } grep-matcher = { version = "0.1.6", path = "../matcher" }
log = "0.4.19" log = "0.4.20"
pcre2 = "0.2.4" pcre2 = "0.2.4"

View File

@ -1,6 +1,3 @@
use std::error;
use std::fmt;
/// An error that can occur in this crate. /// An error that can occur in this crate.
/// ///
/// Generally, this error corresponds to problems building a regular /// Generally, this error corresponds to problems building a regular
@ -12,7 +9,7 @@ pub struct Error {
} }
impl Error { impl Error {
pub(crate) fn regex<E: error::Error>(err: E) -> Error { pub(crate) fn regex<E: std::error::Error>(err: E) -> Error {
Error { kind: ErrorKind::Regex(err.to_string()) } Error { kind: ErrorKind::Regex(err.to_string()) }
} }
@ -24,6 +21,7 @@ impl Error {
/// The kind of an error that can occur. /// The kind of an error that can occur.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ErrorKind { pub enum ErrorKind {
/// An error that occurred as a result of parsing a regular expression. /// 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 /// 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. /// The string here is the underlying error converted to a string.
Regex(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 { fn description(&self) -> &str {
match self.kind { match self.kind {
ErrorKind::Regex(_) => "regex error", ErrorKind::Regex(_) => "regex error",
ErrorKind::__Nonexhaustive => unreachable!(),
} }
} }
} }
impl fmt::Display for Error { impl std::fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.kind { match self.kind {
ErrorKind::Regex(ref s) => write!(f, "{}", s), ErrorKind::Regex(ref s) => write!(f, "{}", s),
ErrorKind::__Nonexhaustive => unreachable!(),
} }
} }
} }

View File

@ -5,9 +5,12 @@ An implementation of `grep-matcher`'s `Matcher` trait for
#![deny(missing_docs)] #![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 pcre2::{is_jit_available, version};
pub use crate::{
error::{Error, ErrorKind},
matcher::{RegexCaptures, RegexMatcher, RegexMatcherBuilder},
};
mod error; mod error;
mod matcher; mod matcher;

View File

@ -1,7 +1,9 @@
use std::collections::HashMap; use std::collections::HashMap;
use grep_matcher::{Captures, Match, Matcher}; use {
use pcre2::bytes::{CaptureLocations, Regex, RegexBuilder}; grep_matcher::{Captures, Match, Matcher},
pcre2::bytes::{CaptureLocations, Regex, RegexBuilder},
};
use crate::error::Error; use crate::error::Error;
@ -426,9 +428,10 @@ fn has_uppercase_literal(pattern: &str) -> bool {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use grep_matcher::{LineMatchKind, Matcher}; use grep_matcher::{LineMatchKind, Matcher};
use super::*;
// Test that enabling word matches does the right thing and demonstrate // Test that enabling word matches does the right thing and demonstrate
// the difference between it and surrounding the regex in `\b`. // the difference between it and surrounding the regex in `\b`.
#[test] #[test]