Skip to main content

error_kind_macros/
lib.rs

1#![doc = include_str!("README.md")]
2
3// =============================================================================
4// MODULES
5// =============================================================================
6
7// -----------------------------------------------------------------------------
8mod attribute;
9mod common;
10mod error;
11mod error_domain;
12mod error_kind;
13
14// =============================================================================
15// USES
16// =============================================================================
17
18// -----------------------------------------------------------------------------
19use crate::common::ErrorFmt;
20use proc_macro::TokenStream;
21use syn::{DeriveInput, Error, parse_macro_input};
22
23// =============================================================================
24// PROC MACRO ENTRY FUNCTIONS
25// =============================================================================
26
27/// Derive macro available if error-kind is built with `features = ["macros"]`
28#[proc_macro_derive(ErrorDomain, attributes(error))]
29pub fn derive_error_domain(input: TokenStream) -> TokenStream {
30    let input = parse_macro_input!(input as DeriveInput);
31    error_domain::expand_derive_macro(&input)
32        .unwrap_or_else(Error::into_compile_error)
33        .into()
34}
35
36/// Derive macro available if error-kind is built with `features = ["macros"]`
37#[proc_macro_derive(ErrorKind, attributes(error))]
38pub fn derive_error_kind(input: TokenStream) -> TokenStream {
39    let input = parse_macro_input!(input as DeriveInput);
40    error_kind::expand_derive_macro(&input)
41        .unwrap_or_else(Error::into_compile_error)
42        .into()
43}
44
45/// Derive macro available if error-kind is built with `features = ["macros"]`
46#[proc_macro_derive(Error, attributes(error, error_kind))]
47pub fn derive_error(input: TokenStream) -> TokenStream {
48    let input = parse_macro_input!(input as DeriveInput);
49    error::expand_derive_macro(&input)
50        .unwrap_or_else(Error::into_compile_error)
51        .into()
52}
53
54/// Attribute macro available if error-kind is built with `features = ["macros"]`
55#[proc_macro_attribute]
56pub fn error(args: TokenStream, item: TokenStream) -> TokenStream {
57    let args = parse_macro_input!(args as ErrorFmt);
58    attribute::expand_attribute_macro(args, item)
59        .unwrap_or_else(Error::into_compile_error)
60        .into()
61}