quick_error/
lib.rs

1#![warn(missing_docs)]
2//! A macro which makes errors easy to write
3//!
4//! Minimum type is like this:
5//!
6//! ```rust
7//! #[macro_use] extern crate quick_error;
8//! # fn main() {}
9//!
10//! quick_error! {
11//!     #[derive(Debug)]
12//!     pub enum SomeError {
13//!         Variant1 {}
14//!     }
15//! }
16//! ```
17//! Both ``pub`` and non-public types may be declared, and all meta attributes
18//! (such as ``#[derive(Debug)]``) are forwarded as is. The `Debug` must be
19//! implemented (but you may do that yourself if you like). The documentation
20//! comments ``/// something`` (as well as other meta attrbiutes) on variants
21//! are allowed.
22//!
23//! # Allowed Syntax
24//!
25//! You may add arbitrary parameters to any struct variant:
26//!
27//! ```rust
28//! # #[macro_use] extern crate quick_error;
29//! # fn main() {}
30//! #
31//! quick_error! {
32//!     #[derive(Debug)]
33//!     pub enum SomeError {
34//!         /// IO Error
35//!         Io(err: std::io::Error) {}
36//!         /// Utf8 Error
37//!         Utf8(err: std::str::Utf8Error) {}
38//!     }
39//! }
40//! ```
41//!
42//! Note unlike in normal Enum declarations you declare names of fields (which
43//! are omitted from type). How they can be used is outlined below.
44//!
45//! Now you might have noticed trailing braces `{}`. They are used to define
46//! implementations. By default:
47//!
48//! * `Error::description()` returns variant name as static string
49//! * `Error::cause()` returns None (even if type wraps some value)
50//! * `Display` outputs `description()`
51//! * No `From` implementations are defined
52//!
53//! To define description simply add `description(value)` inside braces:
54//!
55//! ```rust
56//! # #[macro_use] extern crate quick_error;
57//! # fn main() {}
58//! #
59//! quick_error! {
60//!     #[derive(Debug)]
61//!     pub enum SomeError {
62//!         Io(err: std::io::Error) {
63//!             description(err.description())
64//!         }
65//!         Utf8(err: std::str::Utf8Error) {
66//!             description("utf8 error")
67//!         }
68//!     }
69//! }
70//! ```
71//!
72//! Normal rules for borrowing apply. So most of the time description either
73//! returns constant string or forwards description from enclosed type.
74//!
75//! To change `cause` method to return some error, add `cause(value)`, for
76//! example:
77//!
78//! ```rust
79//! # #[macro_use] extern crate quick_error;
80//! # fn main() {}
81//! #
82//! quick_error! {
83//!     #[derive(Debug)]
84//!     pub enum SomeError {
85//!         Io(err: std::io::Error) {
86//!             cause(err)
87//!             description(err.description())
88//!         }
89//!         Utf8(err: std::str::Utf8Error) {
90//!             description("utf8 error")
91//!         }
92//!         Other(err: Box<std::error::Error>) {
93//!             cause(&**err)
94//!             description(err.description())
95//!         }
96//!     }
97//! }
98//! ```
99//! Note you don't need to wrap value in `Some`, its implicit. In case you want
100//! `None` returned just omit the `cause`. You can't return `None`
101//! conditionally.
102//!
103//! To change how each clause is `Display`ed add `display(pattern,..args)`,
104//! for example:
105//!
106//! ```rust
107//! # #[macro_use] extern crate quick_error;
108//! # fn main() {}
109//! #
110//! quick_error! {
111//!     #[derive(Debug)]
112//!     pub enum SomeError {
113//!         Io(err: std::io::Error) {
114//!             display("I/O error: {}", err)
115//!         }
116//!         Utf8(err: std::str::Utf8Error) {
117//!             display("Utf8 error, valid up to {}", err.valid_up_to())
118//!         }
119//!     }
120//! }
121//! ```
122//!
123//! If you need a reference to the error when `Display`ing, you can instead use
124//! `display(x) -> (pattern, ..args)`, where `x` sets the name of the reference.
125//!
126//! ```rust
127//! # #[macro_use] extern crate quick_error;
128//! # fn main() {}
129//! #
130//! use std::error::Error; // put methods like `description()` of this trait into scope
131//!
132//! quick_error! {
133//!     #[derive(Debug)]
134//!     pub enum SomeError {
135//!         Io(err: std::io::Error) {
136//!             display(x) -> ("{}: {}", x.description(), err)
137//!         }
138//!         Utf8(err: std::str::Utf8Error) {
139//!             display(self_) -> ("{}, valid up to {}", self_.description(), err.valid_up_to())
140//!         }
141//!     }
142//! }
143//! ```
144//!
145//! To convert to the type from any other, use one of the three forms of
146//! `from` clause.
147//!
148//! For example, to convert simple wrapper use bare `from()`:
149//!
150//! ```rust
151//! # #[macro_use] extern crate quick_error;
152//! # fn main() {}
153//! #
154//! quick_error! {
155//!     #[derive(Debug)]
156//!     pub enum SomeError {
157//!         Io(err: std::io::Error) {
158//!             from()
159//!         }
160//!     }
161//! }
162//! ```
163//!
164//! This implements ``From<io::Error>``.
165//!
166//! To convert to singleton enumeration type (discarding the value), use
167//! the `from(type)` form:
168//!
169//! ```rust
170//! # #[macro_use] extern crate quick_error;
171//! # fn main() {}
172//! #
173//! quick_error! {
174//!     #[derive(Debug)]
175//!     pub enum SomeError {
176//!         FormatError {
177//!             from(std::fmt::Error)
178//!         }
179//!     }
180//! }
181//! ```
182//!
183//! And the most powerful form is `from(var: type) -> (arguments...)`. It
184//! might be used to convert to type with multiple arguments or for arbitrary
185//! value conversions:
186//!
187//! ```rust
188//! # #[macro_use] extern crate quick_error;
189//! # fn main() {}
190//! #
191//! quick_error! {
192//!     #[derive(Debug)]
193//!     pub enum SomeError {
194//!         FailedOperation(s: &'static str, errno: i32) {
195//!             from(errno: i32) -> ("os error", errno)
196//!             from(e: std::io::Error) -> ("io error", e.raw_os_error().unwrap())
197//!         }
198//!         /// Converts from both kinds of utf8 errors
199//!         Utf8(err: std::str::Utf8Error) {
200//!             from()
201//!             from(err: std::string::FromUtf8Error) -> (err.utf8_error())
202//!         }
203//!     }
204//! }
205//! ```
206//! # Context
207//!
208//! Since quick-error 1.1 we also have a `context` declaration, which is
209//! similar to (the longest form of) `from`, but allows adding some context to
210//! the error. We need a longer example to demonstrate this:
211//!
212//! ```rust
213//! # #[macro_use] extern crate quick_error;
214//! # use std::io;
215//! # use std::fs::File;
216//! # use std::path::{Path, PathBuf};
217//! #
218//! use quick_error::ResultExt;
219//!
220//! quick_error! {
221//!     #[derive(Debug)]
222//!     pub enum Error {
223//!         File(filename: PathBuf, err: io::Error) {
224//!             context(path: &'a Path, err: io::Error)
225//!                 -> (path.to_path_buf(), err)
226//!         }
227//!     }
228//! }
229//!
230//! fn openfile(path: &Path) -> Result<(), Error> {
231//!     try!(File::open(path).context(path));
232//!
233//!     // If we didn't have context, the line above would be written as;
234//!     //
235//!     // try!(File::open(path)
236//!     //     .map_err(|err| Error::File(path.to_path_buf(), err)));
237//!
238//!     Ok(())
239//! }
240//!
241//! # fn main() {
242//! #     openfile(Path::new("/etc/somefile")).ok();
243//! # }
244//! ```
245//!
246//! Each `context(a: A, b: B)` clause implements
247//! `From<Context<A, B>> for Error`. Which means multiple `context` clauses
248//! are a subject to the normal coherence rules. Unfortunately, we can't
249//! provide full support of generics for the context, but you may either use a
250//! lifetime `'a` for references or `AsRef<Type>` (the latter means `A:
251//! AsRef<Type>`, and `Type` must be concrete). It's also occasionally useful
252//! to use a tuple as a type of the first argument.
253//!
254//! You also need to `use quick_error::ResultExt` extension trait to get
255//! working `.context()` method.
256//!
257//! More info on context in [this article](http://bit.ly/1PsuxDt).
258//!
259//! All forms of `from`, `display`, `description`, `cause`, and `context`
260//! clauses can be combined and put in arbitrary order. Only `from` and
261//! `context` can be used multiple times in single variant of enumeration.
262//! Docstrings are also okay.  Empty braces can be omitted as of quick_error
263//! 0.1.3.
264//!
265//! # Private Enums
266//!
267//! Since quick-error 1.2.0 we  have a way to make a private enum that is
268//! wrapped by public structure:
269//!
270//! ```rust
271//! #[macro_use] extern crate quick_error;
272//! # fn main() {}
273//!
274//! quick_error! {
275//!     #[derive(Debug)]
276//!     pub enum PubError wraps ErrorEnum {
277//!         Variant1 {}
278//!     }
279//! }
280//! ```
281//!
282//! This generates data structures like this
283//!
284//! ```rust
285//!
286//! pub struct PubError(ErrorEnum);
287//!
288//! enum ErrorEnum {
289//!     Variant1,
290//! }
291//!
292//! ```
293//!
294//! Which in turn allows you to export just `PubError` in your crate and keep
295//! actual enumeration private to the crate. This is useful to keep backwards
296//! compatibility for error types. Currently there is no shorcuts to define
297//! error constructors for the inner type, but we consider adding some in
298//! future versions.
299//!
300//! It's possible to declare internal enum as public too.
301//!
302//!
303
304
305/// Main macro that does all the work
306#[macro_export]
307macro_rules! quick_error {
308
309    (   $(#[$meta:meta])*
310        pub enum $name:ident { $($chunks:tt)* }
311    ) => {
312        quick_error!(SORT [pub enum $name $(#[$meta])* ]
313            items [] buf []
314            queue [ $($chunks)* ]);
315    };
316    (   $(#[$meta:meta])*
317        enum $name:ident { $($chunks:tt)* }
318    ) => {
319        quick_error!(SORT [enum $name $(#[$meta])* ]
320            items [] buf []
321            queue [ $($chunks)* ]);
322    };
323
324    (   $(#[$meta:meta])*
325        pub enum $name:ident wraps $enum_name:ident { $($chunks:tt)* }
326    ) => {
327        quick_error!(WRAPPER $enum_name [ pub struct ] $name $(#[$meta])*);
328        quick_error!(SORT [enum $enum_name $(#[$meta])* ]
329            items [] buf []
330            queue [ $($chunks)* ]);
331    };
332
333    (   $(#[$meta:meta])*
334        pub enum $name:ident wraps pub $enum_name:ident { $($chunks:tt)* }
335    ) => {
336        quick_error!(WRAPPER $enum_name [ pub struct ] $name $(#[$meta])*);
337        quick_error!(SORT [pub enum $enum_name $(#[$meta])* ]
338            items [] buf []
339            queue [ $($chunks)* ]);
340    };
341    (   $(#[$meta:meta])*
342        enum $name:ident wraps $enum_name:ident { $($chunks:tt)* }
343    ) => {
344        quick_error!(WRAPPER $enum_name [ struct ] $name $(#[$meta])*);
345        quick_error!(SORT [enum $enum_name $(#[$meta])* ]
346            items [] buf []
347            queue [ $($chunks)* ]);
348    };
349
350    (   $(#[$meta:meta])*
351        enum $name:ident wraps pub $enum_name:ident { $($chunks:tt)* }
352    ) => {
353        quick_error!(WRAPPER $enum_name [ struct ] $name $(#[$meta])*);
354        quick_error!(SORT [pub enum $enum_name $(#[$meta])* ]
355            items [] buf []
356            queue [ $($chunks)* ]);
357    };
358
359
360    (
361        WRAPPER $internal:ident [ $($strdef:tt)* ] $strname:ident
362        $(#[$meta:meta])*
363    ) => {
364        $(#[$meta])*
365        $($strdef)* $strname ( $internal );
366
367        impl ::std::fmt::Display for $strname {
368            fn fmt(&self, f: &mut ::std::fmt::Formatter)
369                -> ::std::fmt::Result
370            {
371                ::std::fmt::Display::fmt(&self.0, f)
372            }
373        }
374
375        impl From<$internal> for $strname {
376            fn from(err: $internal) -> Self {
377                $strname(err)
378            }
379        }
380
381        impl ::std::error::Error for $strname {
382            fn description(&self) -> &str {
383                self.0.description()
384            }
385            fn cause(&self) -> Option<&::std::error::Error> {
386                self.0.cause()
387            }
388        }
389    };
390
391    // Queue is empty, can do the work
392    (SORT [enum $name:ident $( #[$meta:meta] )*]
393        items [$($( #[$imeta:meta] )*
394                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
395                                {$( $ifuncs:tt )*} )* ]
396        buf [ ]
397        queue [ ]
398    ) => {
399        quick_error!(ENUM_DEFINITION [enum $name $( #[$meta] )*]
400            body []
401            queue [$($( #[$imeta] )*
402                      => $iitem: $imode [$( $ivar: $ityp ),*] )*]
403        );
404        quick_error!(IMPLEMENTATIONS $name {$(
405           $iitem: $imode [$(#[$imeta])*] [$( $ivar: $ityp ),*] {$( $ifuncs )*}
406           )*});
407        $(
408            quick_error!(ERROR_CHECK $imode $($ifuncs)*);
409        )*
410    };
411    (SORT [pub enum $name:ident $( #[$meta:meta] )*]
412        items [$($( #[$imeta:meta] )*
413                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
414                                {$( $ifuncs:tt )*} )* ]
415        buf [ ]
416        queue [ ]
417    ) => {
418        quick_error!(ENUM_DEFINITION [pub enum $name $( #[$meta] )*]
419            body []
420            queue [$($( #[$imeta] )*
421                      => $iitem: $imode [$( $ivar: $ityp ),*] )*]
422        );
423        quick_error!(IMPLEMENTATIONS $name {$(
424           $iitem: $imode [$(#[$imeta])*] [$( $ivar: $ityp ),*] {$( $ifuncs )*}
425           )*});
426        $(
427            quick_error!(ERROR_CHECK $imode $($ifuncs)*);
428        )*
429    };
430    // Add meta to buffer
431    (SORT [$( $def:tt )*]
432        items [$($( #[$imeta:meta] )*
433                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
434                                {$( $ifuncs:tt )*} )* ]
435        buf [$( #[$bmeta:meta] )*]
436        queue [ #[$qmeta:meta] $( $tail:tt )*]
437    ) => {
438        quick_error!(SORT [$( $def )*]
439            items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*]
440            buf [$( #[$bmeta] )* #[$qmeta] ]
441            queue [$( $tail )*]);
442    };
443    // Add ident to buffer
444    (SORT [$( $def:tt )*]
445        items [$($( #[$imeta:meta] )*
446                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
447                                {$( $ifuncs:tt )*} )* ]
448        buf [$( #[$bmeta:meta] )*]
449        queue [ $qitem:ident $( $tail:tt )*]
450    ) => {
451        quick_error!(SORT [$( $def )*]
452            items [$( $(#[$imeta])*
453                      => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*]
454            buf [$(#[$bmeta])* => $qitem : UNIT [ ] ]
455            queue [$( $tail )*]);
456    };
457    // Flush buffer on meta after ident
458    (SORT [$( $def:tt )*]
459        items [$($( #[$imeta:meta] )*
460                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
461                                {$( $ifuncs:tt )*} )* ]
462        buf [$( #[$bmeta:meta] )*
463            => $bitem:ident: $bmode:tt [$( $bvar:ident: $btyp:ty ),*] ]
464        queue [ #[$qmeta:meta] $( $tail:tt )*]
465    ) => {
466        quick_error!(SORT [$( $def )*]
467            enum [$( $(#[$emeta])* => $eitem $(( $($etyp),* ))* )*
468                     $(#[$bmeta])* => $bitem: $bmode $(( $($btyp),* ))*]
469            items [$($( #[$imeta:meta] )*
470                      => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*
471                     $bitem: $bmode [$( $bvar:$btyp ),*] {} ]
472            buf [ #[$qmeta] ]
473            queue [$( $tail )*]);
474    };
475    // Add tuple enum-variant
476    (SORT [$( $def:tt )*]
477        items [$($( #[$imeta:meta] )*
478                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
479                                {$( $ifuncs:tt )*} )* ]
480        buf [$( #[$bmeta:meta] )* => $bitem:ident: UNIT [ ] ]
481        queue [($( $qvar:ident: $qtyp:ty ),+) $( $tail:tt )*]
482    ) => {
483        quick_error!(SORT [$( $def )*]
484            items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*]
485            buf [$( #[$bmeta] )* => $bitem: TUPLE [$( $qvar:$qtyp ),*] ]
486            queue [$( $tail )*]
487        );
488    };
489    // Add struct enum-variant - e.g. { descr: &'static str }
490    (SORT [$( $def:tt )*]
491        items [$($( #[$imeta:meta] )*
492                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
493                                {$( $ifuncs:tt )*} )* ]
494        buf [$( #[$bmeta:meta] )* => $bitem:ident: UNIT [ ] ]
495        queue [{ $( $qvar:ident: $qtyp:ty ),+} $( $tail:tt )*]
496    ) => {
497        quick_error!(SORT [$( $def )*]
498            items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*]
499            buf [$( #[$bmeta] )* => $bitem: STRUCT [$( $qvar:$qtyp ),*] ]
500            queue [$( $tail )*]);
501    };
502    // Add struct enum-variant, with excess comma - e.g. { descr: &'static str, }
503    (SORT [$( $def:tt )*]
504        items [$($( #[$imeta:meta] )*
505                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
506                                {$( $ifuncs:tt )*} )* ]
507        buf [$( #[$bmeta:meta] )* => $bitem:ident: UNIT [ ] ]
508        queue [{$( $qvar:ident: $qtyp:ty ),+ ,} $( $tail:tt )*]
509    ) => {
510        quick_error!(SORT [$( $def )*]
511            items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*]
512            buf [$( #[$bmeta] )* => $bitem: STRUCT [$( $qvar:$qtyp ),*] ]
513            queue [$( $tail )*]);
514    };
515    // Add braces and flush always on braces
516    (SORT [$( $def:tt )*]
517        items [$($( #[$imeta:meta] )*
518                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
519                                {$( $ifuncs:tt )*} )* ]
520        buf [$( #[$bmeta:meta] )*
521                 => $bitem:ident: $bmode:tt [$( $bvar:ident: $btyp:ty ),*] ]
522        queue [ {$( $qfuncs:tt )*} $( $tail:tt )*]
523    ) => {
524        quick_error!(SORT [$( $def )*]
525            items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*
526                      $(#[$bmeta])* => $bitem: $bmode [$( $bvar:$btyp ),*] {$( $qfuncs )*} ]
527            buf [ ]
528            queue [$( $tail )*]);
529    };
530    // Flush buffer on double ident
531    (SORT [$( $def:tt )*]
532        items [$($( #[$imeta:meta] )*
533                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
534                                {$( $ifuncs:tt )*} )* ]
535        buf [$( #[$bmeta:meta] )*
536                 => $bitem:ident: $bmode:tt [$( $bvar:ident: $btyp:ty ),*] ]
537        queue [ $qitem:ident $( $tail:tt )*]
538    ) => {
539        quick_error!(SORT [$( $def )*]
540            items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*
541                     $(#[$bmeta])* => $bitem: $bmode [$( $bvar:$btyp ),*] {} ]
542            buf [ => $qitem : UNIT [ ] ]
543            queue [$( $tail )*]);
544    };
545    // Flush buffer on end
546    (SORT [$( $def:tt )*]
547        items [$($( #[$imeta:meta] )*
548                  => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
549                                {$( $ifuncs:tt )*} )* ]
550        buf [$( #[$bmeta:meta] )*
551            => $bitem:ident: $bmode:tt [$( $bvar:ident: $btyp:ty ),*] ]
552        queue [ ]
553    ) => {
554        quick_error!(SORT [$( $def )*]
555            items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*
556                     $(#[$bmeta])* => $bitem: $bmode [$( $bvar:$btyp ),*] {} ]
557            buf [ ]
558            queue [ ]);
559    };
560    // Public enum (Queue Empty)
561    (ENUM_DEFINITION [pub enum $name:ident $( #[$meta:meta] )*]
562        body [$($( #[$imeta:meta] )*
563            => $iitem:ident ($(($( $ttyp:ty ),+))*) {$({$( $svar:ident: $styp:ty ),*})*} )* ]
564        queue [ ]
565    ) => {
566        $(#[$meta])*
567        pub enum $name {
568            $(
569                $(#[$imeta])*
570                $iitem $(($( $ttyp ),*))* $({$( $svar: $styp ),*})*,
571            )*
572        }
573    };
574    // Private enum (Queue Empty)
575    (ENUM_DEFINITION [enum $name:ident $( #[$meta:meta] )*]
576        body [$($( #[$imeta:meta] )*
577            => $iitem:ident ($(($( $ttyp:ty ),+))*) {$({$( $svar:ident: $styp:ty ),*})*} )* ]
578        queue [ ]
579    ) => {
580        $(#[$meta])*
581        enum $name {
582            $(
583                $(#[$imeta])*
584                $iitem $(($( $ttyp ),*))* $({$( $svar: $styp ),*})*,
585            )*
586        }
587    };
588    // Unit variant
589    (ENUM_DEFINITION [$( $def:tt )*]
590        body [$($( #[$imeta:meta] )*
591            => $iitem:ident ($(($( $ttyp:ty ),+))*) {$({$( $svar:ident: $styp:ty ),*})*} )* ]
592        queue [$( #[$qmeta:meta] )*
593            => $qitem:ident: UNIT [ ] $( $queue:tt )*]
594    ) => {
595        quick_error!(ENUM_DEFINITION [ $($def)* ]
596            body [$($( #[$imeta] )* => $iitem ($(($( $ttyp ),+))*) {$({$( $svar: $styp ),*})*} )*
597                    $( #[$qmeta] )* => $qitem () {} ]
598            queue [ $($queue)* ]
599        );
600    };
601    // Tuple variant
602    (ENUM_DEFINITION [$( $def:tt )*]
603        body [$($( #[$imeta:meta] )*
604            => $iitem:ident ($(($( $ttyp:ty ),+))*) {$({$( $svar:ident: $styp:ty ),*})*} )* ]
605        queue [$( #[$qmeta:meta] )*
606            => $qitem:ident: TUPLE [$( $qvar:ident: $qtyp:ty ),+] $( $queue:tt )*]
607    ) => {
608        quick_error!(ENUM_DEFINITION [ $($def)* ]
609            body [$($( #[$imeta] )* => $iitem ($(($( $ttyp ),+))*) {$({$( $svar: $styp ),*})*} )*
610                    $( #[$qmeta] )* => $qitem (($( $qtyp ),*)) {} ]
611            queue [ $($queue)* ]
612        );
613    };
614    // Struct variant
615    (ENUM_DEFINITION [$( $def:tt )*]
616        body [$($( #[$imeta:meta] )*
617            => $iitem:ident ($(($( $ttyp:ty ),+))*) {$({$( $svar:ident: $styp:ty ),*})*} )* ]
618        queue [$( #[$qmeta:meta] )*
619            => $qitem:ident: STRUCT [$( $qvar:ident: $qtyp:ty ),*] $( $queue:tt )*]
620    ) => {
621        quick_error!(ENUM_DEFINITION [ $($def)* ]
622            body [$($( #[$imeta] )* => $iitem ($(($( $ttyp ),+))*) {$({$( $svar: $styp ),*})*} )*
623                    $( #[$qmeta] )* => $qitem () {{$( $qvar: $qtyp ),*}} ]
624            queue [ $($queue)* ]
625        );
626    };
627    (IMPLEMENTATIONS
628        $name:ident {$(
629            $item:ident: $imode:tt [$(#[$imeta:meta])*] [$( $var:ident: $typ:ty ),*] {$( $funcs:tt )*}
630        )*}
631    ) => {
632        #[allow(unused)]
633        impl ::std::fmt::Display for $name {
634            fn fmt(&self, fmt: &mut ::std::fmt::Formatter)
635                -> ::std::fmt::Result
636            {
637                match *self {
638                    $(
639                        $(#[$imeta])*
640                        quick_error!(ITEM_PATTERN
641                            $name $item: $imode [$( ref $var ),*]
642                        ) => {
643                            let display_fn = quick_error!(FIND_DISPLAY_IMPL
644                                $name $item: $imode
645                                {$( $funcs )*});
646
647                            display_fn(self, fmt)
648                        }
649                    )*
650                }
651            }
652        }
653        #[allow(unused)]
654        impl ::std::error::Error for $name {
655            fn description(&self) -> &str {
656                match *self {
657                    $(
658                        $(#[$imeta])*
659                        quick_error!(ITEM_PATTERN
660                            $name $item: $imode [$( ref $var ),*]
661                        ) => {
662                            quick_error!(FIND_DESCRIPTION_IMPL
663                                $item: $imode self fmt [$( $var ),*]
664                                {$( $funcs )*})
665                        }
666                    )*
667                }
668            }
669            fn cause(&self) -> Option<&::std::error::Error> {
670                match *self {
671                    $(
672                        $(#[$imeta])*
673                        quick_error!(ITEM_PATTERN
674                            $name $item: $imode [$( ref $var ),*]
675                        ) => {
676                            quick_error!(FIND_CAUSE_IMPL
677                                $item: $imode [$( $var ),*]
678                                {$( $funcs )*})
679                        }
680                    )*
681                }
682            }
683        }
684        $(
685            quick_error!(FIND_FROM_IMPL
686                $name $item: $imode [$( $var:$typ ),*]
687                {$( $funcs )*});
688        )*
689        $(
690            quick_error!(FIND_CONTEXT_IMPL
691                $name $item: $imode [$( $var:$typ ),*]
692                {$( $funcs )*});
693        )*
694    };
695    (FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
696        { display($self_:tt) -> ($( $exprs:tt )*) $( $tail:tt )*}
697    ) => {
698        |quick_error!(IDENT $self_): &$name, f: &mut ::std::fmt::Formatter| { write!(f, $( $exprs )*) }
699    };
700    (FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
701        { display($pattern:expr) $( $tail:tt )*}
702    ) => {
703        |_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern) }
704    };
705    (FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
706        { display($pattern:expr, $( $exprs:tt )*) $( $tail:tt )*}
707    ) => {
708        |_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern, $( $exprs )*) }
709    };
710    (FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
711        { $t:tt $( $tail:tt )*}
712    ) => {
713        quick_error!(FIND_DISPLAY_IMPL
714            $name $item: $imode
715            {$( $tail )*})
716    };
717    (FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
718        { }
719    ) => {
720        |self_: &$name, f: &mut ::std::fmt::Formatter| {
721            write!(f, "{}", ::std::error::Error::description(self_))
722        }
723    };
724    (FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident
725        [$( $var:ident ),*]
726        { description($expr:expr) $( $tail:tt )*}
727    ) => {
728        $expr
729    };
730    (FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident
731        [$( $var:ident ),*]
732        { $t:tt $( $tail:tt )*}
733    ) => {
734        quick_error!(FIND_DESCRIPTION_IMPL
735            $item: $imode $me $fmt [$( $var ),*]
736            {$( $tail )*})
737    };
738    (FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident
739        [$( $var:ident ),*]
740        { }
741    ) => {
742        stringify!($item)
743    };
744    (FIND_CAUSE_IMPL $item:ident: $imode:tt
745        [$( $var:ident ),*]
746        { cause($expr:expr) $( $tail:tt )*}
747    ) => {
748        Some($expr)
749    };
750    (FIND_CAUSE_IMPL $item:ident: $imode:tt
751        [$( $var:ident ),*]
752        { $t:tt $( $tail:tt )*}
753    ) => {
754        quick_error!(FIND_CAUSE_IMPL
755            $item: $imode [$( $var ),*]
756            { $($tail)* })
757    };
758    (FIND_CAUSE_IMPL $item:ident: $imode:tt
759        [$( $var:ident ),*]
760        { }
761    ) => {
762        None
763    };
764    // ----------------------------- FROM IMPL --------------------------
765    (FIND_FROM_IMPL $name:ident $item:ident: $imode:tt
766        [$( $var:ident: $typ:ty ),*]
767        { from() $( $tail:tt )*}
768    ) => {
769        $(
770            impl From<$typ> for $name {
771                fn from($var: $typ) -> $name {
772                    $name::$item($var)
773                }
774            }
775        )*
776        quick_error!(FIND_FROM_IMPL
777            $name $item: $imode [$( $var:$typ ),*]
778            {$( $tail )*});
779    };
780    (FIND_FROM_IMPL $name:ident $item:ident: UNIT
781        [ ]
782        { from($ftyp:ty) $( $tail:tt )*}
783    ) => {
784        impl From<$ftyp> for $name {
785            fn from(_discarded_error: $ftyp) -> $name {
786                $name::$item
787            }
788        }
789        quick_error!(FIND_FROM_IMPL
790            $name $item: UNIT [  ]
791            {$( $tail )*});
792    };
793    (FIND_FROM_IMPL $name:ident $item:ident: TUPLE
794        [$( $var:ident: $typ:ty ),*]
795        { from($fvar:ident: $ftyp:ty) -> ($( $texpr:expr ),*) $( $tail:tt )*}
796    ) => {
797        impl From<$ftyp> for $name {
798            fn from($fvar: $ftyp) -> $name {
799                $name::$item($( $texpr ),*)
800            }
801        }
802        quick_error!(FIND_FROM_IMPL
803            $name $item: TUPLE [$( $var:$typ ),*]
804            { $($tail)* });
805    };
806    (FIND_FROM_IMPL $name:ident $item:ident: STRUCT
807        [$( $var:ident: $typ:ty ),*]
808        { from($fvar:ident: $ftyp:ty) -> {$( $tvar:ident: $texpr:expr ),*} $( $tail:tt )*}
809    ) => {
810        impl From<$ftyp> for $name {
811            fn from($fvar: $ftyp) -> $name {
812                $name::$item {
813                    $( $tvar: $texpr ),*
814                }
815            }
816        }
817        quick_error!(FIND_FROM_IMPL
818            $name $item: STRUCT [$( $var:$typ ),*]
819            { $($tail)* });
820    };
821    (FIND_FROM_IMPL $name:ident $item:ident: $imode:tt
822        [$( $var:ident: $typ:ty ),*]
823        { $t:tt $( $tail:tt )*}
824    ) => {
825        quick_error!(FIND_FROM_IMPL
826            $name $item: $imode [$( $var:$typ ),*]
827            {$( $tail )*}
828        );
829    };
830    (FIND_FROM_IMPL $name:ident $item:ident: $imode:tt
831        [$( $var:ident: $typ:ty ),*]
832        { }
833    ) => {
834    };
835    // ----------------------------- CONTEXT IMPL --------------------------
836    (FIND_CONTEXT_IMPL $name:ident $item:ident: TUPLE
837        [$( $var:ident: $typ:ty ),*]
838        { context($cvar:ident: AsRef<$ctyp:ty>, $fvar:ident: $ftyp:ty)
839            -> ($( $texpr:expr ),*) $( $tail:tt )* }
840    ) => {
841        impl<T: AsRef<$ctyp>> From<$crate::Context<T, $ftyp>> for $name {
842            fn from(
843                $crate::Context($cvar, $fvar): $crate::Context<T, $ftyp>)
844                -> $name
845            {
846                $name::$item($( $texpr ),*)
847            }
848        }
849        quick_error!(FIND_CONTEXT_IMPL
850            $name $item: TUPLE [$( $var:$typ ),*]
851            { $($tail)* });
852    };
853    (FIND_CONTEXT_IMPL $name:ident $item:ident: TUPLE
854        [$( $var:ident: $typ:ty ),*]
855        { context($cvar:ident: $ctyp:ty, $fvar:ident: $ftyp:ty)
856            -> ($( $texpr:expr ),*) $( $tail:tt )* }
857    ) => {
858        impl<'a> From<$crate::Context<$ctyp, $ftyp>> for $name {
859            fn from(
860                $crate::Context($cvar, $fvar): $crate::Context<$ctyp, $ftyp>)
861                -> $name
862            {
863                $name::$item($( $texpr ),*)
864            }
865        }
866        quick_error!(FIND_CONTEXT_IMPL
867            $name $item: TUPLE [$( $var:$typ ),*]
868            { $($tail)* });
869    };
870    (FIND_CONTEXT_IMPL $name:ident $item:ident: STRUCT
871        [$( $var:ident: $typ:ty ),*]
872        { context($cvar:ident: AsRef<$ctyp:ty>, $fvar:ident: $ftyp:ty)
873            -> {$( $tvar:ident: $texpr:expr ),*} $( $tail:tt )* }
874    ) => {
875        impl<T: AsRef<$ctyp>> From<$crate::Context<T, $ftyp>> for $name {
876            fn from(
877                $crate::Context($cvar, $fvar): $crate::Context<$ctyp, $ftyp>)
878                -> $name
879            {
880                $name::$item {
881                    $( $tvar: $texpr ),*
882                }
883            }
884        }
885        quick_error!(FIND_CONTEXT_IMPL
886            $name $item: STRUCT [$( $var:$typ ),*]
887            { $($tail)* });
888    };
889    (FIND_CONTEXT_IMPL $name:ident $item:ident: STRUCT
890        [$( $var:ident: $typ:ty ),*]
891        { context($cvar:ident: $ctyp:ty, $fvar:ident: $ftyp:ty)
892            -> {$( $tvar:ident: $texpr:expr ),*} $( $tail:tt )* }
893    ) => {
894        impl<'a> From<$crate::Context<$ctyp, $ftyp>> for $name {
895            fn from(
896                $crate::Context($cvar, $fvar): $crate::Context<$ctyp, $ftyp>)
897                -> $name
898            {
899                $name::$item {
900                    $( $tvar: $texpr ),*
901                }
902            }
903        }
904        quick_error!(FIND_CONTEXT_IMPL
905            $name $item: STRUCT [$( $var:$typ ),*]
906            { $($tail)* });
907    };
908    (FIND_CONTEXT_IMPL $name:ident $item:ident: $imode:tt
909        [$( $var:ident: $typ:ty ),*]
910        { $t:tt $( $tail:tt )*}
911    ) => {
912        quick_error!(FIND_CONTEXT_IMPL
913            $name $item: $imode [$( $var:$typ ),*]
914            {$( $tail )*}
915        );
916    };
917    (FIND_CONTEXT_IMPL $name:ident $item:ident: $imode:tt
918        [$( $var:ident: $typ:ty ),*]
919        { }
920    ) => {
921    };
922    // ----------------------------- ITEM IMPL --------------------------
923    (ITEM_BODY $(#[$imeta:meta])* $item:ident: UNIT
924    ) => { };
925    (ITEM_BODY $(#[$imeta:meta])* $item:ident: TUPLE
926        [$( $typ:ty ),*]
927    ) => {
928        ($( $typ ),*)
929    };
930    (ITEM_BODY $(#[$imeta:meta])* $item:ident: STRUCT
931        [$( $var:ident: $typ:ty ),*]
932    ) => {
933        {$( $var:$typ ),*}
934    };
935    (ITEM_PATTERN $name:ident $item:ident: UNIT []
936    ) => {
937        $name::$item
938    };
939    (ITEM_PATTERN $name:ident $item:ident: TUPLE
940        [$( ref $var:ident ),*]
941    ) => {
942        $name::$item ($( ref $var ),*)
943    };
944    (ITEM_PATTERN $name:ident $item:ident: STRUCT
945        [$( ref $var:ident ),*]
946    ) => {
947        $name::$item {$( ref $var ),*}
948    };
949    // This one should match all allowed sequences in "funcs" but not match
950    // anything else.
951    // This is to contrast FIND_* clauses which just find stuff they need and
952    // skip everything else completely
953    (ERROR_CHECK $imode:tt display($self_:tt) -> ($( $exprs:tt )*) $( $tail:tt )*)
954    => { quick_error!(ERROR_CHECK $imode $($tail)*); };
955    (ERROR_CHECK $imode:tt display($pattern: expr) $( $tail:tt )*)
956    => { quick_error!(ERROR_CHECK $imode $($tail)*); };
957    (ERROR_CHECK $imode:tt display($pattern: expr, $( $exprs:tt )*) $( $tail:tt )*)
958    => { quick_error!(ERROR_CHECK $imode $($tail)*); };
959    (ERROR_CHECK $imode:tt description($expr:expr) $( $tail:tt )*)
960    => { quick_error!(ERROR_CHECK $imode $($tail)*); };
961    (ERROR_CHECK $imode:tt cause($expr:expr) $($tail:tt)*)
962    => { quick_error!(ERROR_CHECK $imode $($tail)*); };
963    (ERROR_CHECK $imode:tt from() $($tail:tt)*)
964    => { quick_error!(ERROR_CHECK $imode $($tail)*); };
965    (ERROR_CHECK $imode:tt from($ftyp:ty) $($tail:tt)*)
966    => { quick_error!(ERROR_CHECK $imode $($tail)*); };
967    (ERROR_CHECK TUPLE from($fvar:ident: $ftyp:ty) -> ($( $e:expr ),*) $( $tail:tt )*)
968    => { quick_error!(ERROR_CHECK TUPLE $($tail)*); };
969    (ERROR_CHECK STRUCT from($fvar:ident: $ftyp:ty) -> {$( $v:ident: $e:expr ),*} $( $tail:tt )*)
970    => { quick_error!(ERROR_CHECK STRUCT $($tail)*); };
971
972    (ERROR_CHECK TUPLE context($cvar:ident: $ctyp:ty, $fvar:ident: $ftyp:ty)
973        -> ($( $e:expr ),*) $( $tail:tt )*)
974    => { quick_error!(ERROR_CHECK TUPLE $($tail)*); };
975    (ERROR_CHECK STRUCT context($cvar:ident: $ctyp:ty, $fvar:ident: $ftyp:ty)
976        -> {$( $v:ident: $e:expr ),*} $( $tail:tt )*)
977    => { quick_error!(ERROR_CHECK STRUCT $($tail)*); };
978
979    (ERROR_CHECK $imode:tt ) => {};
980    // Utility functions
981    (IDENT $ident:ident) => { $ident }
982}
983
984
985/// Generic context type
986///
987/// Used mostly as a transport for `ResultExt::context` method
988#[derive(Debug)]
989pub struct Context<X, E>(pub X, pub E);
990
991/// Result extension trait adding a `context` method
992pub trait ResultExt<T, E> {
993    /// The method is use to add context information to current operation
994    ///
995    /// The context data is then used in error constructor to store additional
996    /// information within error. For example, you may add a filename as a
997    /// context for file operation. See crate documentation for the actual
998    /// example.
999    fn context<X>(self, x: X) -> Result<T, Context<X, E>>;
1000}
1001
1002impl<T, E> ResultExt<T, E> for Result<T, E> {
1003    fn context<X>(self, x: X) -> Result<T, Context<X, E>> {
1004        self.map_err(|e| Context(x, e))
1005    }
1006}
1007
1008
1009
1010#[cfg(test)]
1011mod test {
1012    use std::num::{ParseFloatError, ParseIntError};
1013    use std::str::Utf8Error;
1014    use std::string::FromUtf8Error;
1015    use std::error::Error;
1016    use std::path::{Path, PathBuf};
1017
1018    use super::ResultExt;
1019
1020    quick_error! {
1021        #[derive(Debug)]
1022        pub enum Bare {
1023            One
1024            Two
1025        }
1026    }
1027
1028    #[test]
1029    fn bare_item_direct() {
1030        assert_eq!(format!("{}", Bare::One), "One".to_string());
1031        assert_eq!(format!("{:?}", Bare::One), "One".to_string());
1032        assert_eq!(Bare::One.description(), "One".to_string());
1033        assert!(Bare::One.cause().is_none());
1034    }
1035    #[test]
1036    fn bare_item_trait() {
1037        let err: &Error = &Bare::Two;
1038        assert_eq!(format!("{}", err), "Two".to_string());
1039        assert_eq!(format!("{:?}", err), "Two".to_string());
1040        assert_eq!(err.description(), "Two".to_string());
1041        assert!(err.cause().is_none());
1042    }
1043
1044    quick_error! {
1045        #[derive(Debug)]
1046        pub enum Wrapper wraps Wrapped {
1047            One
1048            Two(s: String) {
1049                display("two: {}", s)
1050                from()
1051            }
1052        }
1053    }
1054
1055    #[test]
1056    fn wrapper() {
1057        assert_eq!(format!("{}", Wrapper::from(Wrapped::One)),
1058            "One".to_string());
1059        assert_eq!(format!("{}",
1060            Wrapper::from(Wrapped::from(String::from("hello")))),
1061            "two: hello".to_string());
1062        assert_eq!(format!("{:?}", Wrapper::from(Wrapped::One)),
1063            "Wrapper(One)".to_string());
1064        assert_eq!(Wrapper::from(Wrapped::One).description(),
1065            "One".to_string());
1066    }
1067
1068    quick_error! {
1069        #[derive(Debug, PartialEq)]
1070        pub enum TupleWrapper {
1071            /// ParseFloat Error
1072            ParseFloatError(err: ParseFloatError) {
1073                from()
1074                description(err.description())
1075                display("parse float error: {err}", err=err)
1076                cause(err)
1077            }
1078            Other(descr: &'static str) {
1079                description(descr)
1080                display("Error: {}", descr)
1081            }
1082            /// FromUtf8 Error
1083            FromUtf8Error(err: Utf8Error, source: Vec<u8>) {
1084                cause(err)
1085                display(me) -> ("{desc} at index {pos}: {err}", desc=me.description(), pos=err.valid_up_to(), err=err)
1086                description("utf8 error")
1087                from(err: FromUtf8Error) -> (err.utf8_error().clone(), err.into_bytes())
1088            }
1089            Discard {
1090                from(&'static str)
1091            }
1092            Singleton {
1093                display("Just a string")
1094            }
1095        }
1096    }
1097
1098    #[test]
1099    fn tuple_wrapper_err() {
1100        let cause = "one and a half times pi".parse::<f32>().unwrap_err();
1101        let err = TupleWrapper::ParseFloatError(cause.clone());
1102        assert_eq!(format!("{}", err), format!("parse float error: {}", cause));
1103        assert_eq!(format!("{:?}", err), format!("ParseFloatError({:?})", cause));
1104        assert_eq!(err.description(), cause.description());
1105        assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause));
1106    }
1107
1108    #[test]
1109    fn tuple_wrapper_trait_str() {
1110        let desc = "hello";
1111        let err: &Error = &TupleWrapper::Other(desc);
1112        assert_eq!(format!("{}", err), format!("Error: {}", desc));
1113        assert_eq!(format!("{:?}", err), format!("Other({:?})", desc));
1114        assert_eq!(err.description(), desc);
1115        assert!(err.cause().is_none());
1116    }
1117
1118    #[test]
1119    fn tuple_wrapper_trait_two_fields() {
1120        let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
1121        let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error();
1122        let err: &Error = &TupleWrapper::FromUtf8Error(cause.clone(), invalid_utf8.clone());
1123        assert_eq!(format!("{}", err), format!("{desc} at index {pos}: {cause}", desc=err.description(), pos=cause.valid_up_to(), cause=cause));
1124        assert_eq!(format!("{:?}", err), format!("FromUtf8Error({:?}, {:?})", cause, invalid_utf8));
1125        assert_eq!(err.description(), "utf8 error");
1126        assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause));
1127    }
1128
1129    #[test]
1130    fn tuple_wrapper_from() {
1131        let cause = "one and a half times pi".parse::<f32>().unwrap_err();
1132        let err = TupleWrapper::ParseFloatError(cause.clone());
1133        let err_from: TupleWrapper = From::from(cause);
1134        assert_eq!(err_from, err);
1135    }
1136
1137    #[test]
1138    fn tuple_wrapper_custom_from() {
1139        let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
1140        let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err();
1141        let err = TupleWrapper::FromUtf8Error(cause.utf8_error().clone(), invalid_utf8);
1142        let err_from: TupleWrapper = From::from(cause);
1143        assert_eq!(err_from, err);
1144    }
1145
1146    #[test]
1147    fn tuple_wrapper_discard() {
1148        let err: TupleWrapper = From::from("hello");
1149        assert_eq!(format!("{}", err), format!("Discard"));
1150        assert_eq!(format!("{:?}", err), format!("Discard"));
1151        assert_eq!(err.description(), "Discard");
1152        assert!(err.cause().is_none());
1153    }
1154
1155    #[test]
1156    fn tuple_wrapper_singleton() {
1157        let err: TupleWrapper = TupleWrapper::Singleton;
1158        assert_eq!(format!("{}", err), format!("Just a string"));
1159        assert_eq!(format!("{:?}", err), format!("Singleton"));
1160        assert_eq!(err.description(), "Singleton");
1161        assert!(err.cause().is_none());
1162    }
1163
1164    quick_error! {
1165        #[derive(Debug, PartialEq)]
1166        pub enum StructWrapper {
1167            // Utf8 Error
1168            Utf8Error{ err: Utf8Error, hint: Option<&'static str> } {
1169                cause(err)
1170                display(me) -> ("{desc} at index {pos}: {err}", desc=me.description(), pos=err.valid_up_to(), err=err)
1171                description("utf8 error")
1172                from(err: Utf8Error) -> { err: err, hint: None }
1173            }
1174            // Utf8 Error
1175            ExcessComma { descr: &'static str, } {
1176                description(descr)
1177                display("Error: {}", descr)
1178            }
1179        }
1180    }
1181
1182    #[test]
1183    fn struct_wrapper_err() {
1184        let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
1185        let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error();
1186        let err: &Error = &StructWrapper::Utf8Error{ err: cause.clone(), hint: Some("nonsense") };
1187        assert_eq!(format!("{}", err), format!("{desc} at index {pos}: {cause}", desc=err.description(), pos=cause.valid_up_to(), cause=cause));
1188        assert_eq!(format!("{:?}", err), format!("Utf8Error {{ err: {:?}, hint: {:?} }}", cause, Some("nonsense")));
1189        assert_eq!(err.description(), "utf8 error");
1190        assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause));
1191    }
1192
1193    #[test]
1194    fn struct_wrapper_struct_from() {
1195        let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
1196        let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error();
1197        let err = StructWrapper::Utf8Error{ err: cause.clone(), hint: None };
1198        let err_from: StructWrapper = From::from(cause);
1199        assert_eq!(err_from, err);
1200    }
1201
1202    #[test]
1203    fn struct_wrapper_excess_comma() {
1204        let descr = "hello";
1205        let err = StructWrapper::ExcessComma { descr: descr };
1206        assert_eq!(format!("{}", err), format!("Error: {}", descr));
1207        assert_eq!(format!("{:?}", err), format!("ExcessComma {{ descr: {:?} }}", descr));
1208        assert_eq!(err.description(), descr);
1209        assert!(err.cause().is_none());
1210    }
1211
1212    quick_error! {
1213        #[derive(Debug)]
1214        pub enum ContextErr {
1215            Float(src: String, err: ParseFloatError) {
1216                context(s: &'a str, e: ParseFloatError) -> (s.to_string(), e)
1217                display("Float error {:?}: {}", src, err)
1218            }
1219            Int { src: String, err: ParseIntError } {
1220                context(s: &'a str, e: ParseIntError)
1221                    -> {src: s.to_string(), err: e}
1222                display("Int error {:?}: {}", src, err)
1223            }
1224            Utf8(path: PathBuf, err: Utf8Error) {
1225                context(p: AsRef<Path>, e: Utf8Error)
1226                    -> (p.as_ref().to_path_buf(), e)
1227                display("Path error at {:?}: {}", path, err)
1228            }
1229            Utf8Str(s: String, err: ::std::io::Error) {
1230                context(s: AsRef<str>, e: ::std::io::Error)
1231                    -> (s.as_ref().to_string(), e)
1232                display("Str error {:?}: {}", s, err)
1233            }
1234        }
1235    }
1236
1237    #[test]
1238    fn parse_float_error() {
1239        fn parse_float(s: &str) -> Result<f32, ContextErr> {
1240            Ok(try!(s.parse().context(s)))
1241        }
1242        assert_eq!(format!("{}", parse_float("12ab").unwrap_err()),
1243            r#"Float error "12ab": invalid float literal"#);
1244    }
1245
1246    #[test]
1247    fn parse_int_error() {
1248        fn parse_int(s: &str) -> Result<i32, ContextErr> {
1249            Ok(try!(s.parse().context(s)))
1250        }
1251        assert_eq!(format!("{}", parse_int("12.5").unwrap_err()),
1252            r#"Int error "12.5": invalid digit found in string"#);
1253    }
1254
1255    #[test]
1256    fn debug_context() {
1257        fn parse_int(s: &str) -> i32 {
1258            s.parse().context(s).unwrap()
1259        }
1260        assert_eq!(parse_int("12"), 12);
1261        assert_eq!(format!("{:?}", "x".parse::<i32>().context("x")),
1262            r#"Err(Context("x", ParseIntError { kind: InvalidDigit }))"#);
1263    }
1264
1265    #[test]
1266    fn path_context() {
1267        fn parse_utf<P: AsRef<Path>>(s: &[u8], p: P)
1268            -> Result<(), ContextErr>
1269        {
1270            try!(::std::str::from_utf8(s).context(p));
1271            Ok(())
1272        }
1273        let etext = parse_utf(b"a\x80\x80", "/etc").unwrap_err().to_string();
1274        assert!(etext.starts_with(
1275            "Path error at \"/etc\": invalid utf-8"));
1276        let etext = parse_utf(b"\x80\x80", PathBuf::from("/tmp")).unwrap_err()
1277            .to_string();
1278        assert!(etext.starts_with(
1279            "Path error at \"/tmp\": invalid utf-8"));
1280    }
1281
1282    #[test]
1283    fn conditional_compilation() {
1284        quick_error! {
1285            #[allow(dead_code)]
1286            #[derive(Debug)]
1287            pub enum Test {
1288                #[cfg(feature = "foo")]
1289                Variant
1290            }
1291        }
1292    }
1293}