-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
134 lines (119 loc) · 3.81 KB
/
lib.rs
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
#![allow(clippy::eval_order_dependence)]
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{
braced,
parse::{Parse, ParseStream},
parse_macro_input,
punctuated::Punctuated,
token, Ident, Result, Token,
};
struct Delegation {
variant: Ident,
#[allow(dead_code)]
colon: Token![:],
function: Ident,
}
impl Parse for Delegation {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self {
variant: input.parse()?,
colon: input.parse()?,
function: input.parse()?,
})
}
}
enum Definition {
Actor(Ident),
}
impl Parse for Definition {
fn parse(input: ParseStream) -> Result<Self> {
let name: Ident = input.parse()?;
let name_str = &name.to_string();
let _: Token![:] = input.parse()?;
if name_str == "actor" {
Ok(Self::Actor(input.parse()?))
} else {
panic!("{} is not a known configuration name", name_str)
}
}
}
struct Configuration {
#[allow(dead_code)]
first_brace: token::Brace,
definitions: Punctuated<Definition, Token![,]>,
#[allow(dead_code)]
comma: Token![,],
#[allow(dead_code)]
second_brace: token::Brace,
delegations: Punctuated<Delegation, Token![,]>,
}
impl Parse for Configuration {
fn parse(input: ParseStream) -> Result<Self> {
let definitions;
let delegations;
Ok(Self {
first_brace: braced!(definitions in input),
definitions: definitions.parse_terminated(Definition::parse)?,
comma: input.parse()?,
second_brace: braced!(delegations in input),
delegations: delegations.parse_terminated(Delegation::parse)?,
})
}
}
#[proc_macro]
pub fn generate(input: TokenStream) -> TokenStream {
let Configuration {
definitions,
delegations,
..
} = parse_macro_input!(input as Configuration);
let actor_name = match definitions.iter().next().unwrap() {
Definition::Actor(ident) => ident.to_string(),
};
let actor_lock = Ident::new(&actor_name.to_ascii_uppercase(), Span::call_site());
let actor = Ident::new(&actor_name, Span::call_site());
let actor_msg = Ident::new(format!("{}Msg", &actor_name).as_str(), Span::call_site());
let matches = delegations.iter().map(
|Delegation {
variant, function, ..
}| {
let actor_msg = Ident::new(format!("{}Msg", &actor_name).as_str(), Span::call_site());
quote! {
crate::messages::#actor_msg::#variant(msg) => {
let _ =
msg.reply.send(match #function(&msg).await {
Ok(output) => Some(output),
err => {
log::error!("{:#?}", err);
None
}
});
}
}
},
);
(quote! {
pub fn actor(children: bastion::prelude::Children) -> bastion::prelude::Children {
children
.with_name(crate::messages::ActorGroups::#actor.as_ref())
.with_exec(move |_| async move {
let (channel, r) = crossbeam_channel::unbounded::<crate::messages::#actor_msg>();
{
let mut lock = crate::messages::#actor_lock.get().unwrap().write();
*lock = Some(channel);
}
loop {
match crate::logging::logged(r.recv().unwrap()) {
#(#matches),*
}
}
#[allow(unreachable_code)]
Ok(())
})
}
})
.into()
}