1c67d6573Sopenharmony_ciuse std::fmt; 2c67d6573Sopenharmony_ciuse std::iter::repeat; 3c67d6573Sopenharmony_ci 4c67d6573Sopenharmony_ci/// An error that occurred during parsing or compiling a regular expression. 5c67d6573Sopenharmony_ci#[derive(Clone, PartialEq)] 6c67d6573Sopenharmony_cipub enum Error { 7c67d6573Sopenharmony_ci /// A syntax error. 8c67d6573Sopenharmony_ci Syntax(String), 9c67d6573Sopenharmony_ci /// The compiled program exceeded the set size limit. 10c67d6573Sopenharmony_ci /// The argument is the size limit imposed. 11c67d6573Sopenharmony_ci CompiledTooBig(usize), 12c67d6573Sopenharmony_ci /// Hints that destructuring should not be exhaustive. 13c67d6573Sopenharmony_ci /// 14c67d6573Sopenharmony_ci /// This enum may grow additional variants, so this makes sure clients 15c67d6573Sopenharmony_ci /// don't count on exhaustive matching. (Otherwise, adding a new variant 16c67d6573Sopenharmony_ci /// could break existing code.) 17c67d6573Sopenharmony_ci #[doc(hidden)] 18c67d6573Sopenharmony_ci __Nonexhaustive, 19c67d6573Sopenharmony_ci} 20c67d6573Sopenharmony_ci 21c67d6573Sopenharmony_ciimpl ::std::error::Error for Error { 22c67d6573Sopenharmony_ci // TODO: Remove this method entirely on the next breaking semver release. 23c67d6573Sopenharmony_ci #[allow(deprecated)] 24c67d6573Sopenharmony_ci fn description(&self) -> &str { 25c67d6573Sopenharmony_ci match *self { 26c67d6573Sopenharmony_ci Error::Syntax(ref err) => err, 27c67d6573Sopenharmony_ci Error::CompiledTooBig(_) => "compiled program too big", 28c67d6573Sopenharmony_ci Error::__Nonexhaustive => unreachable!(), 29c67d6573Sopenharmony_ci } 30c67d6573Sopenharmony_ci } 31c67d6573Sopenharmony_ci} 32c67d6573Sopenharmony_ci 33c67d6573Sopenharmony_ciimpl fmt::Display for Error { 34c67d6573Sopenharmony_ci fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 35c67d6573Sopenharmony_ci match *self { 36c67d6573Sopenharmony_ci Error::Syntax(ref err) => err.fmt(f), 37c67d6573Sopenharmony_ci Error::CompiledTooBig(limit) => write!( 38c67d6573Sopenharmony_ci f, 39c67d6573Sopenharmony_ci "Compiled regex exceeds size limit of {} bytes.", 40c67d6573Sopenharmony_ci limit 41c67d6573Sopenharmony_ci ), 42c67d6573Sopenharmony_ci Error::__Nonexhaustive => unreachable!(), 43c67d6573Sopenharmony_ci } 44c67d6573Sopenharmony_ci } 45c67d6573Sopenharmony_ci} 46c67d6573Sopenharmony_ci 47c67d6573Sopenharmony_ci// We implement our own Debug implementation so that we show nicer syntax 48c67d6573Sopenharmony_ci// errors when people use `Regex::new(...).unwrap()`. It's a little weird, 49c67d6573Sopenharmony_ci// but the `Syntax` variant is already storing a `String` anyway, so we might 50c67d6573Sopenharmony_ci// as well format it nicely. 51c67d6573Sopenharmony_ciimpl fmt::Debug for Error { 52c67d6573Sopenharmony_ci fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 53c67d6573Sopenharmony_ci match *self { 54c67d6573Sopenharmony_ci Error::Syntax(ref err) => { 55c67d6573Sopenharmony_ci let hr: String = repeat('~').take(79).collect(); 56c67d6573Sopenharmony_ci writeln!(f, "Syntax(")?; 57c67d6573Sopenharmony_ci writeln!(f, "{}", hr)?; 58c67d6573Sopenharmony_ci writeln!(f, "{}", err)?; 59c67d6573Sopenharmony_ci writeln!(f, "{}", hr)?; 60c67d6573Sopenharmony_ci write!(f, ")")?; 61c67d6573Sopenharmony_ci Ok(()) 62c67d6573Sopenharmony_ci } 63c67d6573Sopenharmony_ci Error::CompiledTooBig(limit) => { 64c67d6573Sopenharmony_ci f.debug_tuple("CompiledTooBig").field(&limit).finish() 65c67d6573Sopenharmony_ci } 66c67d6573Sopenharmony_ci Error::__Nonexhaustive => { 67c67d6573Sopenharmony_ci f.debug_tuple("__Nonexhaustive").finish() 68c67d6573Sopenharmony_ci } 69c67d6573Sopenharmony_ci } 70c67d6573Sopenharmony_ci } 71c67d6573Sopenharmony_ci} 72