fancy_default_derive/
lib.rs

1//! This crate is an internal implementation of the `fancy-default` library.
2//! 
3//! See the [`fancy-default`] library's documentation for instructions on how to use it.
4
5use proc_macro::TokenStream;
6
7mod default;
8mod variant_default;
9
10/// Derive the [`core::default::Default`] trait.
11/// 
12/// This derive macro actually implements
13/// [`fancy_default::traits::Default`] to prevent naming pollution.
14#[proc_macro_derive(Default, attributes(default))]
15pub fn derive_default(input: TokenStream) -> TokenStream {
16    match default::process_default_derive(input.into()) {
17        Ok(ts) => ts.into(),
18        Err(e) => e.to_compile_error().into(),
19    }
20}
21
22/// Derive the [`fancy_default::traits::ConstDefault`] trait.
23#[proc_macro_derive(ConstDefault, attributes(default))]
24pub fn derive_const_default(input: TokenStream) -> TokenStream {
25    match default::process_const_default_derive(input.into()) {
26        Ok(ts) => ts.into(),
27        Err(e) => e.to_compile_error().into(),
28    }
29}
30
31/// Derive no traits, but implement functions/associated constants for the type.
32#[proc_macro_derive(VariantDefault, attributes(variant, default))]
33pub fn derive_variant_default(input: TokenStream) -> TokenStream {
34    match variant_default::process_variant_default(input.into()) {
35        Ok(ts) => ts.into(),
36        Err(e) => e.to_compile_error().into(),
37    }
38}