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
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_derive(StreamablePlugin)]
pub fn piped_command_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_piped_command(&ast)
}
fn impl_piped_command(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
#[async_trait::async_trait]
impl StreamablePlugin for #name {
async fn stream(
&self,
mut input: futures::channel::mpsc::UnboundedReceiver<Message>,
mut output: futures::channel::mpsc::UnboundedSender<Vec<Message>>)
-> Result<(), bot_rs_core::plugin::PluginError> {
while let Some(msg) = futures::stream::StreamExt::next(&mut input).await {
let results = self.call(msg).await?;
futures::sink::SinkExt::send(&mut output, results).await.expect("failed to send results to output");
}
Ok(())
}
fn info(&self) -> PluginInfo {
Plugin::info(self)
}
}
};
gen.into()
}