error_chain_utils/
lib.rs

1//! Utilities for `error-chain`
2//!
3//! This crate contains macros for use with the `error-chain` crate, and by
4//! extension depends on the version of the crate these macros are designed for
5//!
6//! So far, these utilities are available:
7//!
8//! - `error_chain_quick`: Extension for convenience to the `error-chain` crate
9//!
10
11use proc_macro;
12use error_chain_utils_lib::quick::main as ecq_main;
13
14
15/// Utility that expands to `error_chain!{...}`
16///
17/// This macro can be used to replace `error_chain!` blocks, with the exact same
18/// syntax, except that within one can use the `quick!` macro inside the 
19/// `errors` block, to make writing error chains much faster.
20///
21/// // Example of input
22/// ```
23/// #[allow(unused_imports)]
24/// use error_chain_utils::error_chain_quick;
25/// error_chain_quick!{
26///     types {
27///         CustomError, CustomErrorEnum, CustomErrorTrait, CustomErrorResult;
28///     }
29/// 
30///     errors {
31///         quick!(ErrWithoutArgs, "Error Without Arguments")
32///         quick!(ErrWithArgs,     "Error With Arguments",  (arg1,arg2))
33///     }
34/// };
35/// ```
36/// 
37/// // Which would be processed into the following
38/// ```
39/// use error_chain::error_chain;
40/// error_chain!{
41///     types {
42///         CustomError, CustomErrorEnum, CustomErrorTrait, CustomErrorResult;
43///     }
44/// 
45///     errors {
46///         ErrWithoutArgs {
47///             description("Error Without Arguments")
48///             display("Error Without Arguments")
49///         }
50///         ErrWithArgs (arg1: String, arg2: String){
51///             description("Error With Arguments")
52///             display("Error With Arguments: {}, {}", arg1, arg2)
53///         }
54///     }
55/// }
56/// ```
57/// 
58/// Trailing commas are supported inside of the `quick!` macro, and wherever else
59/// `error_chain!` supports them.
60/// 
61/// Normal errors and `quick!` macro errors are supported in the same `errors` block.
62/// 
63/// Probably due to the double-expansion needed to make this happen, Rust considers
64/// this macro unused, even when it actually is being used. To bypass the diagnostics
65/// stemming from this, add #[allow(unused_imports)] before the import statement, as shown above.
66
67#[proc_macro]
68pub fn error_chain_quick(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
69    match ecq_main(input.into()) {
70        Ok(val) => val,
71        Err(e) => e.into_compile_error()
72    }.into()
73}