]> git.proxmox.com Git - rustc.git/blob - src/vendor/regex-0.2.11/src/error.rs
New upstream version 1.31.0+dfsg1
[rustc.git] / src / vendor / regex-0.2.11 / src / error.rs
1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::fmt;
12 use std::iter::repeat;
13
14 use syntax;
15
16 /// An error that occurred during parsing or compiling a regular expression.
17 #[derive(Clone, PartialEq)]
18 pub enum Error {
19 /// A syntax error.
20 Syntax(String),
21 /// The compiled program exceeded the set size limit.
22 /// The argument is the size limit imposed.
23 CompiledTooBig(usize),
24 /// Hints that destructuring should not be exhaustive.
25 ///
26 /// This enum may grow additional variants, so this makes sure clients
27 /// don't count on exhaustive matching. (Otherwise, adding a new variant
28 /// could break existing code.)
29 #[doc(hidden)]
30 __Nonexhaustive,
31 }
32
33 impl ::std::error::Error for Error {
34 fn description(&self) -> &str {
35 match *self {
36 Error::Syntax(ref err) => err,
37 Error::CompiledTooBig(_) => "compiled program too big",
38 Error::__Nonexhaustive => unreachable!(),
39 }
40 }
41
42 fn cause(&self) -> Option<&::std::error::Error> {
43 None
44 }
45 }
46
47 impl fmt::Display for Error {
48 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49 match *self {
50 Error::Syntax(ref err) => err.fmt(f),
51 Error::CompiledTooBig(limit) => {
52 write!(f, "Compiled regex exceeds size limit of {} bytes.",
53 limit)
54 }
55 Error::__Nonexhaustive => unreachable!(),
56 }
57 }
58 }
59
60 // We implement our own Debug implementation so that we show nicer syntax
61 // errors when people use `Regex::new(...).unwrap()`. It's a little weird,
62 // but the `Syntax` variant is already storing a `String` anyway, so we might
63 // as well format it nicely.
64 impl fmt::Debug for Error {
65 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 match *self {
67 Error::Syntax(ref err) => {
68 let hr: String = repeat('~').take(79).collect();
69 try!(writeln!(f, "Syntax("));
70 try!(writeln!(f, "{}", hr));
71 try!(writeln!(f, "{}", err));
72 try!(writeln!(f, "{}", hr));
73 try!(write!(f, ")"));
74 Ok(())
75 }
76 Error::CompiledTooBig(limit) => {
77 f.debug_tuple("CompiledTooBig")
78 .field(&limit)
79 .finish()
80 }
81 Error::__Nonexhaustive => {
82 f.debug_tuple("__Nonexhaustive").finish()
83 }
84 }
85 }
86 }
87
88 impl From<syntax::Error> for Error {
89 fn from(err: syntax::Error) -> Error {
90 Error::Syntax(err.to_string())
91 }
92 }