-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.rs
More file actions
228 lines (222 loc) · 6.53 KB
/
Copy pathlib.rs
File metadata and controls
228 lines (222 loc) · 6.53 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use std::str::FromStr;
use syn::DeriveInput;
/// Use this on a struct to generate a built adapter around it, including useful impls.
///
/// # Examples
/// ```
/// # use gateway_addon_rust::prelude::*;
/// # use async_trait::async_trait;
/// #[adapter]
/// struct ExampleAdapter {
/// foo: i32,
/// }
///
/// #[async_trait]
/// impl Adapter for BuiltExampleAdapter {
/// async fn on_unload(&mut self) -> Result<(), String> {
/// println!("Foo: {}", self.foo);
/// Ok(())
/// }
/// }
/// ```
/// will expand to
/// ```
/// # use gateway_addon_rust::{prelude::*, adapter::AdapterHandleWrapper};
/// # use std::ops::{Deref, DerefMut};
/// # use async_trait::async_trait;
/// struct ExampleAdapter {
/// foo: i32,
/// }
///
/// struct BuiltExampleAdapter {
/// data: ExampleAdapter,
/// adapter_handle: AdapterHandle,
/// }
///
/// impl AdapterHandleWrapper for BuiltExampleAdapter {
/// fn adapter_handle(&self) -> &AdapterHandle {
/// &self.adapter_handle
/// }
/// fn adapter_handle_mut(&mut self) -> &mut AdapterHandle {
/// &mut self.adapter_handle
/// }
/// }
///
/// impl BuildAdapter for ExampleAdapter {
/// type BuiltAdapter = BuiltExampleAdapter;
/// fn build(data: Self, adapter_handle: AdapterHandle) -> Self::BuiltAdapter {
/// BuiltExampleAdapter {
/// data,
/// adapter_handle,
/// }
/// }
/// }
///
/// impl Deref for BuiltExampleAdapter {
/// type Target = ExampleAdapter;
/// fn deref(&self) -> &Self::Target {
/// &self.data
/// }
/// }
///
/// impl DerefMut for BuiltExampleAdapter {
/// fn deref_mut(&mut self) -> &mut Self::Target {
/// &mut self.data
/// }
/// }
///
/// #[async_trait]
/// impl Adapter for BuiltExampleAdapter {
/// // ...
/// }
/// ```
#[proc_macro_attribute]
pub fn adapter(_args: TokenStream, input: TokenStream) -> TokenStream {
apply_macro(input, "adapter", "Adapter")
}
/// Use this on a struct to generate a built device around it, including useful impls.
///
/// # Examples
/// ```
/// # use gateway_addon_rust::prelude::*;
/// # use async_trait::async_trait;
/// #[device]
/// struct ExamplDevice {
/// foo: i32,
/// }
///
/// impl DeviceStructure for ExampleDevice {
/// // ...
/// # fn id(&self) -> String {
/// # "example-device".to_owned()
/// # }
/// # fn description(&self) -> DeviceDescription {
/// # DeviceDescription::default()
/// # }
/// }
///
/// #[async_trait]
/// impl Device for BuiltExampleDevice {}
/// ```
/// will expand to
/// ```
/// # use gateway_addon_rust::{prelude::*, device::DeviceHandleWrapper};
/// # use std::ops::{Deref, DerefMut};
/// # use async_trait::async_trait;
/// struct ExampleDevice { foo: i32 }
///
/// struct BuiltExampleDevice{
/// data: ExampleDevice,,
/// device_handle: DeviceHandle
/// }
///
/// impl DeviceHandleWrapper for BuiltExampleDevice {
/// fn device_handle(&self) -> &DeviceHandle {
/// &self.device_handle
/// }
/// fn device_handle_mut(&mut self) -> &mut DeviceHandle {
/// &mut self.device_handle
/// }
/// }
///
/// impl BuildDevice for ExampleDevice {
/// type BuiltDevice = BuiltExampleDevice;
/// fn build(data: Self, device_handle: DeviceHandle) -> Self::BuiltDevice {
/// BuiltExampleDevice { data, device_handle }
/// }
/// }
///
/// impl Deref for BuiltExampleDevice {
/// type Target = ExampleDevice;
/// fn deref(&self) -> &Self::Target {
/// &self.data
/// }
/// }
///
/// impl DerefMut for BuiltExampleDevice {
/// fn deref_mut(&mut self) -> &mut Self::Target {
/// &mut self.data
/// }
/// }
///
/// impl DeviceStructure for ExampleDevice {
/// // ...
/// # fn id(&self) -> String {
/// # "example-device".to_owned()
/// # }
/// # fn description(&self) -> DeviceDescription {
/// # DeviceDescription::default()
/// # }
/// }
///
/// #[async_trait]
/// impl Device for BuiltExampleDevice {}
/// ```
#[proc_macro_attribute]
pub fn device(_args: TokenStream, input: TokenStream) -> TokenStream {
apply_macro(input, "device", "Device")
}
fn apply_macro(input: TokenStream, name_snail_case: &str, name_camel_case: &str) -> TokenStream {
if let Ok(ast) = syn::parse2::<DeriveInput>(input.into()) {
alter_struct(ast, name_snail_case, name_camel_case).into()
} else {
panic!("`{}` has to be used with structs", name_snail_case)
}
}
fn alter_struct(ast: DeriveInput, name_snail_case: &str, name_camel_case: &str) -> TokenStream2 {
let trait_handle_wrapper = TokenStream2::from_str(&format!(
"gateway_addon_rust::{}::{}HandleWrapper",
name_snail_case, name_camel_case
))
.unwrap();
let trait_build = TokenStream2::from_str(&format!(
"gateway_addon_rust::{}::Build{}",
name_snail_case, name_camel_case
))
.unwrap();
let struct_built = TokenStream2::from_str(&format!("Built{}", name_camel_case)).unwrap();
let struct_handle = TokenStream2::from_str(&format!(
"gateway_addon_rust::{}::{}Handle",
name_snail_case, name_camel_case
))
.unwrap();
let fn_handle = TokenStream2::from_str(&format!("{}_handle", name_snail_case)).unwrap();
let fn_handle_mut = TokenStream2::from_str(&format!("{}_handle_mut", name_snail_case)).unwrap();
let struct_name = ast.ident.clone();
let struct_built_name = TokenStream2::from_str(&format!("Built{}", struct_name)).unwrap();
quote! {
#ast
impl #trait_build for #struct_name {
type #struct_built = #struct_built_name;
fn build(data: Self, #fn_handle: #struct_handle) -> Self::#struct_built {
#struct_built_name { data, #fn_handle }
}
}
struct #struct_built_name {
data: #struct_name,
#fn_handle: #struct_handle,
}
impl #trait_handle_wrapper for #struct_built_name {
fn #fn_handle(&self) -> &#struct_handle {
&self.#fn_handle
}
fn #fn_handle_mut(&mut self) -> &mut #struct_handle {
&mut self.#fn_handle
}
}
impl std::ops::Deref for #struct_built_name {
type Target = #struct_name;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl std::ops::DerefMut for #struct_built_name {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
}
}