tosserror_derive/lib.rs
1#![allow(
2 clippy::blocks_in_if_conditions,
3 clippy::cast_lossless,
4 clippy::cast_possible_truncation,
5 clippy::manual_find,
6 clippy::manual_let_else,
7 clippy::manual_map,
8 clippy::map_unwrap_or,
9 clippy::module_name_repetitions,
10 clippy::needless_pass_by_value,
11 clippy::option_if_let_else,
12 clippy::range_plus_one,
13 clippy::single_match_else,
14 clippy::struct_field_names,
15 clippy::too_many_lines,
16 clippy::wrong_self_convention
17)]
18
19extern crate proc_macro;
20
21mod ast;
22mod attr;
23mod expand;
24
25use proc_macro::TokenStream;
26use syn::{parse_macro_input, DeriveInput};
27
28/// Generates helper traits for enum variants.
29///
30/// ### Attributes:
31///
32/// `#[visibility]`
33///
34/// sets visibility of the generated traits.
35///
36/// Example:
37///
38/// ```ignore
39/// use thiserror::Error;
40/// use tosserror::Toss;
41///
42/// #[derive(Error, Toss, Debug)]
43/// #[visibility(pub(crate))] // sets visibility of the generated traits to pub(crate)
44/// pub enum Error {
45/// ...
46/// }
47/// ```
48///
49/// <br>
50///
51/// `#[prefix]`
52///
53/// sets custom prefix for the generated traits.
54///
55/// Example:
56///
57/// ```ignore
58/// use thiserror::Error;
59/// use tosserror::Toss;
60///
61/// #[derive(Error, Toss, Debug)]
62/// #[prefix(invalid)] // sets custom prefix `invalid` for the generated traits
63/// pub enum Error {
64/// Io { ... } // `.toss_io()` becomes `.toss_invalid_io()`
65/// }
66/// ```
67///
68/// <br>
69///
70/// `#[backtrace]`, `#[source]`, `#[from]`
71///
72/// these are not custom attributes for tosserror. They are used to detect source fields for `thiserror::Error`.
73#[proc_macro_derive(Toss, attributes(backtrace, source, from, visibility, prefix))]
74pub fn derive_error(input: TokenStream) -> TokenStream {
75 let input = parse_macro_input!(input as DeriveInput);
76 expand::derive(&input)
77 .unwrap_or_else(|err| err.to_compile_error())
78 .into()
79}