regex_literal/error.rs
1//! The module defines Error struct for parsing and compiling literals
2extern crate alloc;
3
4// code based from crate regex 1.10.3 https://docs.rs/crate/regex/1.10.3/source/src/error.rs
5//latest https://github.com/rust-lang/regex/blob/master/src/error.rs
6use alloc::string::{String, ToString};//was alloc::string::{String,ToString}
7
8use regex_automata::meta;
9
10use core::str::Utf8Error;
11
12/// An error that occurred during parsing or compiling a regular expression.
13#[non_exhaustive]
14#[derive(Clone, PartialEq)]
15pub enum Error {
16 /// A syntax error.
17 Syntax(String),
18 /// The compiled program exceeded the set size
19 /// limit. The argument is the size limit imposed by
20 /// [`meta::BuildError::size_limit`]. Even
21 /// when not configured explicitly, it defaults to a reasonable limit.
22 ///
23 /// If you're getting this error, it occurred because your regex has been
24 /// compiled to an intermediate state that is too big. It is important to
25 /// note that exceeding this limit does _not_ mean the regex is too big to
26 /// _work_, but rather, the regex is big enough that it may wind up being
27 /// surprisingly slow when used in a search. In other words, this error is
28 /// meant to be a practical heuristic for avoiding a performance footgun,
29 /// and especially so for the case where the regex pattern is coming from
30 /// an untrusted source.
31 ///
32 /// There are generally two ways to move forward if you hit this error.
33 /// The first is to find some way to use a smaller regex. The second is to
34 /// increase the size limit via `meta::BuilderError::size_limit`. However, if
35 /// your regex pattern is not from a trusted source, then neither of these
36 /// approaches may be appropriate. Instead, you'll have to determine just
37 /// how big of a regex you want to allow.
38 CompiledTooBig(usize),
39}
40
41impl Error {
42 pub(crate) fn from_meta_build_error(err: meta::BuildError) -> Error {
43 if let Some(size_limit) = err.size_limit() {
44 Error::CompiledTooBig(size_limit)
45 } else if let Some(ref err) = err.syntax_error() {
46 Error::Syntax(err.to_string())
47 } else {
48 // This is a little suspect. Technically there are more ways for
49 // a meta regex to fail to build other than "exceeded size limit"
50 // and "syntax error." For example, if there are too many states
51 // or even too many patterns. But in practice this is probably
52 // good enough. The worst thing that happens is that Error::Syntax
53 // represents an error that isn't technically a syntax error, but
54 // the actual message will still be shown. So... it's not too bad.
55 //
56 // We really should have made the Error type in the regex crate
57 // completely opaque. Rookie mistake.
58 Error::Syntax(err.to_string())
59 }
60 }
61 //specific for byte string parsing in regex_literal, TODO: refactoring
62 pub(crate) fn from_utf8_error(err: Utf8Error,start_index: usize) -> Error {
63 Error::Syntax(format!("Invalid UTF-8 code after byte index {}",err.valid_up_to() + start_index))
64 }
65}
66
67
68impl std::error::Error for Error {
69 // TODO: Remove this method entirely on the next breaking semver release.
70 #[allow(deprecated)]
71 fn description(&self) -> &str {
72 match *self {
73 Error::Syntax(ref err) => err,
74 Error::CompiledTooBig(_) => "compiled program too big",
75 }
76 }
77}
78
79impl core::fmt::Display for Error {
80 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
81 match *self {
82 Error::Syntax(ref err) => err.fmt(f),
83 Error::CompiledTooBig(limit) => write!(
84 f,
85 "Compiled regex exceeds size limit of {} bytes.",
86 limit
87 ),
88 }
89 }
90}
91
92// We implement our own Debug implementation so that we show nicer syntax
93// errors when people use `Regex::new(...).unwrap()`. It's a little weird,
94// but the `Syntax` variant is already storing a `String` anyway, so we might
95// as well format it nicely.
96impl core::fmt::Debug for Error {
97 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
98 match *self {
99 Error::Syntax(ref err) => {
100 let hr: String = core::iter::repeat('~').take(79).collect();
101 writeln!(f, "Syntax(")?;
102 writeln!(f, "{}", hr)?;
103 writeln!(f, "{}", err)?;
104 writeln!(f, "{}", hr)?;
105 write!(f, ")")?;
106 Ok(())
107 }
108 Error::CompiledTooBig(limit) => {
109 f.debug_tuple("CompiledTooBig").field(&limit).finish()
110 }
111 }
112 }
113}