twilight_interactions_derive/
lib.rs

1//! # twilight-interactions-derive
2//!
3//! This crate provides derive macros for the `twilight-interactions` crate.
4//!
5//! Please refer to the `twilight-interactions` documentation for further
6//! information.
7
8mod command;
9mod localization;
10mod option;
11mod parse;
12
13use proc_macro::TokenStream;
14use syn::{parse_macro_input, DeriveInput};
15
16/// Derive macro for the `CommandModel` trait.
17///
18/// See the documentation of the trait for more information about usage of this
19/// macro.
20#[proc_macro_derive(CommandModel, attributes(command))]
21pub fn command_model(input: TokenStream) -> TokenStream {
22    let input = parse_macro_input!(input as DeriveInput);
23    let ident = input.ident.clone();
24
25    match command::impl_command_model(input) {
26        Ok(output) => output.into(),
27        Err(error) => command::dummy_command_model(ident, error).into(),
28    }
29}
30
31/// Derive macro for the `CreateCommand` trait.
32///
33/// See the documentation of the trait for more information about usage of this
34/// macro.
35#[proc_macro_derive(CreateCommand, attributes(command))]
36pub fn create_command(input: TokenStream) -> TokenStream {
37    let input = parse_macro_input!(input as DeriveInput);
38    let ident = input.ident.clone();
39
40    match command::impl_create_command(input) {
41        Ok(output) => output.into(),
42        Err(error) => command::dummy_create_command(ident, error).into(),
43    }
44}
45
46/// Derive macro for the `CommandOption` trait.
47///
48/// See the documentation of the trait for more information about usage of this
49/// macro.
50#[proc_macro_derive(CommandOption, attributes(option))]
51pub fn command_option(input: TokenStream) -> TokenStream {
52    let input = parse_macro_input!(input as DeriveInput);
53    let ident = input.ident.clone();
54
55    match option::impl_command_option(input) {
56        Ok(output) => output.into(),
57        Err(error) => option::dummy_command_option(ident, error).into(),
58    }
59}
60
61/// Derive macro for the `CreateOption` trait.
62///
63/// See the documentation of the trait for more information about usage of this
64/// macro.
65#[proc_macro_derive(CreateOption, attributes(option))]
66pub fn create_option(input: TokenStream) -> TokenStream {
67    let input = parse_macro_input!(input as DeriveInput);
68    let ident = input.ident.clone();
69
70    match option::impl_create_option(input) {
71        Ok(output) => output.into(),
72        Err(error) => option::dummy_create_option(ident, error).into(),
73    }
74}