A macro which makes errors easy to write
Minimum type is like this:
extern crate quick_error;
quick_error!
Both pub
and non-public types may be declared, and all meta attributes
(such as #[derive(Debug)]
) are forwarded as is. The Debug
must be
implemented (but you may do that youself if you like).
The documentation comments /// something
on variants are allowed, but
you must either put them on all variants or on none of them (to the best
of my understanding it's limitation of rust macro system)
You may add arbitrary parameters to any struct variant:
quick_error!
Note unlike in normal Enum decarations you declare names of fields (which are omitted from type). How they can be used is outlined below.
Now you might have noticed trailing braces {}
. They are used to define
implementations. By default:
Error::description()
returns variant name as static stringError::cause()
returns None (even if type wraps some value)Display
outputsdescription()
- No
From
implementations are defined
To define description simply add description(value)
inside braces:
quick_error!
Normal rules for borrowing apply. So most of the time description either returns constant string or forwards description from enclosed type.
To change cause
method to return some error, add cause(value)
, for
example:
quick_error!
Note you don't need to wrap value in Some
, its implicit. In case you want
None
returned just omit the cause
. You can't return None
conditionally.
To change how each clause is Display
ed add display(pattern,..args)
,
for example:
quick_error!
To convert to the type from any other, use one of the three forms of
from
clause.
For example, to convert simple wrapper use bare from()
:
quick_error!
This implements From<io::Error>
.
To convert to singleton enumeration type (discarding the value), use
the from(type)
form:
quick_error!
And the most powerful form is from(var: type) -> (arguments...)
. It
might be used to convert to type with multiple arguments or for arbitrary
value conversions:
quick_error!
All forms of from
, display
, description
, cause
clauses can be
combined and put in arbitrary order. Only from
may be used multiple times
in single variant of enumeration. Docstrings are also okay.
Empty braces can't be omitted.