-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.rs
56 lines (44 loc) · 1.28 KB
/
macros.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
macro_rules! get_type {
([$t:ident $body:tt] ) => (Vec<get_type!($t)>);
($t:ident $body:tt) => ($t);
($t:ident) => ($t);
([$t:ident]) => (Vec<$t>);
}
macro_rules! request {
($id:ident) => {};
( [$id:ident] ) => {};
// Array of complex type
( [$sname:ident $tp:tt]) => {request!($sname $tp);};
($sname:ident $(, $response:ident)* { $($f:ident : $tp:tt),* } ) => {
pub struct $sname {
$(pub $f : get_type!($tp) ),*
}
impl ToKafka for $sname {
fn to_kafka(&self, buff: &mut BufMut) {
$(self.$f.to_kafka(buff);)*
}
}
$(impl Request for $sname {
type Response = $response;
})*
$(request!($tp);)*
};
}
macro_rules! response {
($id:ident) => {};
( [$id:ident] ) => {};
// Array of complex type
( [ $sname:ident $tp:tt ] ) => (response!($sname $tp););
($sname:ident { $($f:ident : $tp:tt),* }) => {
#[derive(Debug)]
pub struct $sname {
$(pub $f: get_type!($tp) ),*
}
impl FromKafka for $sname {
fn from_kafka(buff: &mut Buf) -> $sname {
$sname { $($f: <get_type!($tp)>::from_kafka(buff)),* }
}
}
$( response!($tp); )*
};
}