1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
//! Dispatcher Chain and Dispatcher Render Macros
//!
//! This module provides macros for creating dispatcher chain and dispatcher render structs
//! with automatic implementations of the `DispatcherChain` trait.
use proc_macro::TokenStream;
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{Ident, Result as SynResult, Token};
/// Parses input in the format: `"command_name", CommandStruct => ChainStruct`
struct DispatcherChainInput {
command_name: syn::LitStr,
command_struct: Ident,
pack: Ident,
}
impl Parse for DispatcherChainInput {
fn parse(input: ParseStream) -> SynResult<Self> {
let command_name = input.parse()?;
input.parse::<Token![,]>()?;
let command_struct = input.parse()?;
input.parse::<Token![=>]>()?;
let pack = input.parse()?;
Ok(DispatcherChainInput {
command_name,
command_struct,
pack,
})
}
}
pub fn dispatcher_chain(input: TokenStream) -> TokenStream {
let DispatcherChainInput {
command_name,
command_struct,
pack,
} = syn::parse_macro_input!(input as DispatcherChainInput);
let command_name_str = command_name.value();
let expanded = quote! {
#[derive(Debug, Default)]
pub struct #command_struct;
::mingling::macros::pack!(#pack = Vec<String>);
impl ::mingling::Dispatcher for #command_struct {
fn node(&self) -> ::mingling::Node {
::mingling::macros::node!(#command_name_str)
}
fn begin(&self, args: Vec<String>) -> ::mingling::ChainProcess {
#pack::new(args).to_chain()
}
fn clone_dispatcher(&self) -> Box<dyn ::mingling::Dispatcher> {
Box::new(#command_struct)
}
}
};
expanded.into()
}
pub fn dispatcher_render(input: TokenStream) -> TokenStream {
let DispatcherChainInput {
command_name,
command_struct,
pack,
} = syn::parse_macro_input!(input as DispatcherChainInput);
let command_name_str = command_name.value();
let expanded = quote! {
#[derive(Debug, Default)]
pub struct #command_struct;
::mingling::macros::pack!(#pack = Vec<String>);
impl ::mingling::Dispatcher for #command_struct {
fn node(&self) -> ::mingling::Node {
::mingling::macros::node!(#command_name_str)
}
fn begin(&self, args: Vec<String>) -> ::mingling::ChainProcess {
#pack::new(args).to_render()
}
fn clone_dispatcher(&self) -> Box<dyn ::mingling::Dispatcher> {
Box::new(#command_struct)
}
}
};
expanded.into()
}
|