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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#![doc(html_favicon_url = "https://www.ruma.io/favicon.ico")]
#![doc(html_logo_url = "https://www.ruma.io/images/logo.png")]
#![warn(missing_docs)]
use event_parse::EventEnumInput;
use proc_macro::TokenStream;
use proc_macro2 as pm2;
use proc_macro_crate::{crate_name, FoundCrate};
use quote::{format_ident, quote};
use syn::{parse_macro_input, DeriveInput};
use crate::event_enum::expand_from_impls_derived;
use self::{
event::expand_event, event_content::expand_event_content, event_enum::expand_event_enum,
event_type::expand_event_type_enum,
};
mod event;
mod event_content;
mod event_enum;
mod event_parse;
mod event_type;
#[proc_macro]
pub fn event_enum(input: TokenStream) -> TokenStream {
let ruma_events = import_ruma_events();
let event_enum_input = syn::parse_macro_input!(input as EventEnumInput);
let enums = event_enum_input
.enums
.iter()
.map(expand_event_enum)
.collect::<syn::Result<pm2::TokenStream>>();
let event_types = expand_event_type_enum(event_enum_input, ruma_events);
event_types
.and_then(|types| {
enums.map(|mut enums| {
enums.extend(types);
enums
})
})
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
#[proc_macro_derive(EventContent, attributes(ruma_event))]
pub fn derive_event_content(input: TokenStream) -> TokenStream {
let ruma_events = import_ruma_events();
let input = parse_macro_input!(input as DeriveInput);
expand_event_content(&input, &ruma_events).unwrap_or_else(syn::Error::into_compile_error).into()
}
#[proc_macro_derive(Event, attributes(ruma_event))]
pub fn derive_state_event(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_event(input).unwrap_or_else(syn::Error::into_compile_error).into()
}
pub(crate) fn import_ruma_events() -> pm2::TokenStream {
if let Ok(FoundCrate::Name(name)) = crate_name("ruma-events") {
let import = format_ident!("{}", name);
quote! { ::#import }
} else if let Ok(FoundCrate::Name(name)) = crate_name("ruma") {
let import = format_ident!("{}", name);
quote! { ::#import::events }
} else if let Ok(FoundCrate::Name(name)) = crate_name("matrix-sdk") {
let import = format_ident!("{}", name);
quote! { ::#import::ruma::events }
} else if let Ok(FoundCrate::Name(name)) = crate_name("matrix-sdk-appservice") {
let import = format_ident!("{}", name);
quote! { ::#import::ruma::events }
} else {
quote! { ::ruma_events }
}
}
#[proc_macro_derive(EventEnumFromEvent)]
pub fn derive_from_event_to_enum(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_from_impls_derived(input).into()
}