occams-rpc-stream-macros 0.2.0

The macro crate for occams-rpc-stream. occams-rpc is a modular, pluggable RPC for high throughput scenario, supports various runtimes, with a low-level streaming interface, and high-level remote API call interface.
Documentation
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use proc_macro::TokenStream;
use quote::quote;
use std::collections::HashMap;
use syn::{Data, DeriveInput, Fields, Meta, NestedMeta, Variant, parse_macro_input};

struct ServerTaskEnumAttrs {
    req: bool,
    resp: bool,
    resp_type: Option<syn::Type>,
    error: syn::Type,
}

impl syn::parse::Parse for ServerTaskEnumAttrs {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let mut req = false;
        let mut resp = false;
        let mut resp_type = None;
        let mut error = None;

        while !input.is_empty() {
            let lookahead = input.lookahead1();
            if lookahead.peek(syn::Ident) {
                let ident: syn::Ident = input.parse()?;
                if ident == "req" {
                    req = true;
                } else if ident == "resp" {
                    resp = true;
                } else if ident == "resp_type" {
                    input.parse::<syn::Token![=]>()?;
                    resp_type = Some(input.parse()?);
                } else if ident == "error" {
                    input.parse::<syn::Token![=]>()?;
                    error = Some(input.parse()?);
                } else {
                    return Err(input.error(format!("unexpected attribute: {}", ident)));
                }
            } else if lookahead.peek(syn::Token![,]) {
                input.parse::<syn::Token![,]>()?;
            } else {
                return Err(input.error("unexpected token"));
            }
        }

        let error = error.ok_or_else(|| {
            input.error("#[server_task_enum] requires an `error = <Type>` attribute")
        })?;

        Ok(ServerTaskEnumAttrs { req, resp, resp_type, error })
    }
}

pub fn server_task_enum_impl(attrs: TokenStream, input: TokenStream) -> TokenStream {
    let mut ast = parse_macro_input!(input as DeriveInput);
    let enum_name = &ast.ident;
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

    let variants = if let Data::Enum(ref mut data) = ast.data {
        &mut data.variants
    } else {
        panic!("#[server_task_enum] can only be applied to enums");
    };

    let mut from_impls = Vec::new();
    let mut decode_arms = Vec::new();
    let mut get_action_arms = Vec::new();
    let mut encode_arms = Vec::new();
    let mut set_result_arms = Vec::new();
    let mut where_clauses_for_decode = Vec::new();

    let mut inner_type_counts: HashMap<String, usize> = HashMap::new();
    for variant in variants.iter() {
        let inner_type = match &variant.fields {
            Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
                &fields.unnamed.first().unwrap().ty
            }
            _ => panic!("Enum variants must be tuple-style with a single field"),
        };
        let inner_type_str = quote! {#inner_type}.to_string();
        *inner_type_counts.entry(inner_type_str).or_insert(0) += 1;
    }

    let macro_attrs = parse_macro_input!(attrs as ServerTaskEnumAttrs);
    let has_req = macro_attrs.req;
    let has_resp = macro_attrs.resp;
    let error_type = macro_attrs.error.clone();

    let resp_type = if has_resp {
        if macro_attrs.resp_type.is_some() {
            panic!("Cannot specify 'resp_type' when 'resp' is present. Response type is Self.");
        }
        quote! { #enum_name }
    } else {
        let r_type =
            macro_attrs.resp_type.expect("resp_type must be specified when 'resp' is not present");
        quote! { #r_type }
    };

    for variant in variants.iter_mut() {
        let variant_name = &variant.ident;
        let inner_type = match &variant.fields {
            Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
                &fields.unnamed.first().unwrap().ty
            }
            _ => panic!("Enum variants must be tuple-style with a single field"),
        };

        if has_req {
            let actions = get_action_attribute(variant);
            variant.attrs.retain(|attr| !attr.path.is_ident("action"));
            // Logic for decode_arms
            for action_meta in &actions {
                let action_token_stream = match action_meta {
                    NestedMeta::Lit(syn::Lit::Str(s)) => {
                        let action_str = s.value();
                        quote! { occams_rpc_stream::proto::RpcAction::Str(#action_str) }
                    }
                    NestedMeta::Lit(syn::Lit::Int(i)) => {
                        let action_int =
                            i.base10_parse::<i32>().expect("Invalid integer literal for action");
                        quote! { occams_rpc_stream::proto::RpcAction::Num(#action_int) }
                    }
                    NestedMeta::Meta(syn::Meta::Path(p)) => {
                        quote! { occams_rpc_stream::proto::RpcAction::Num(val) if val == (#p as i32) }
                    }
                    _ => panic!(
                        "Unsupported action type for decode_arms. Only string/integer literals and enum variants are supported."
                    ),
                };
                decode_arms.push(quote! {
                            #action_token_stream => {
                                let task = <#inner_type as occams_rpc_stream::server::task::ServerTaskDecode<#resp_type>>::decode_req::<C>(codec, action, seq, req, blob, noti)?;
                                Ok(#enum_name::#variant_name(task))
                            }
                        });
            }

            // Logic for where_clauses_for_decode (conditional)
            let inner_type_exists = match &variant.fields {
                Fields::Unnamed(fields) if fields.unnamed.len() == 1 => true,
                _ => false,
            };

            if actions.len() > 1 || (actions.len() == 0 && inner_type_exists) {
                where_clauses_for_decode.push(quote! {
                    #inner_type: occams_rpc_stream::server::task::ServerTaskDecode<#resp_type> + occams_rpc_stream::server::task::ServerTaskAction
                });
            } else {
                where_clauses_for_decode.push(quote! {
                    #inner_type: occams_rpc_stream::server::task::ServerTaskDecode<#resp_type>
                });
            }

            // Logic for get_action_arms (conditional and RpcAction return type)
            if actions.len() > 1 {
                get_action_arms.push(quote! {
                    #enum_name::#variant_name(inner) => inner.get_action(),
                });
            } else if actions.len() == 1 {
                let action_meta = &actions[0];
                let action_token_stream = match action_meta {
                    NestedMeta::Lit(syn::Lit::Str(s)) => {
                        let action_str = s.value();
                        quote! { occams_rpc_stream::proto::RpcAction::Str(#action_str) }
                    }
                    NestedMeta::Lit(syn::Lit::Int(i)) => {
                        let action_int =
                            i.base10_parse::<i32>().expect("Invalid integer literal for action");
                        quote! { occams_rpc_stream::proto::RpcAction::Num(#action_int) }
                    }
                    NestedMeta::Meta(syn::Meta::Path(p)) => {
                        quote! { occams_rpc_stream::proto::RpcAction::Num(#p as i32) }
                    }
                    _ => panic!(
                        "Unsupported action type for get_action. Only string/integer literals and enum variants are supported."
                    ),
                };
                get_action_arms.push(quote! {
                    #enum_name::#variant_name(_) => #action_token_stream,
                });
            } else {
                panic!("Must specify #[action] attribute for req case");
            }
        }

        let inner_type_str = quote! {#inner_type}.to_string();
        let count = *inner_type_counts.get(&inner_type_str).unwrap_or(&0);
        if count == 1 {
            // Only generate if count is 1, prevent duplicate sub-types
            from_impls.push(quote! {
                impl #impl_generics From<#inner_type> for #enum_name #ty_generics #where_clause {
                    #[inline]
                    fn from(task: #inner_type) -> Self {
                        #enum_name::#variant_name(task)
                    }
                }
            });
        } else if count > 1 {
            // Explicitly panic if a duplicate sub-type is found
            panic!(
                "Duplicate sub-type `{}` found in enum `{}`. `From` implementation cannot be generated for duplicate sub-types.",
                inner_type_str, enum_name
            );
        }
        if has_resp {
            encode_arms.push(quote! {
                #enum_name::#variant_name(task) => task.encode_resp(codec, buf),
            });

            set_result_arms.push(quote! {
                #enum_name::#variant_name(task) => task._set_result(res),
            });
        }
    }

    let req_impl = if has_req {
        quote! {

            impl #impl_generics occams_rpc_stream::server::task::ServerTaskDecode<#resp_type> for #enum_name #ty_generics #where_clause
            where
                #(#where_clauses_for_decode),*
            {
                #[inline]
                fn decode_req<'a, C: occams_rpc_stream::Codec>(
                    codec: &'a C,
                    action: occams_rpc_stream::proto::RpcAction<'a>,
                    seq: u64,
                    req: &'a [u8],
                    blob: Option<io_buffer::Buffer>,
                    noti: occams_rpc_stream::server::task::RespNoti<#resp_type>,
                ) -> Result<Self, ()> {
                    match action {
                        #(#decode_arms)*
                        _ => {
                            log::error!("Unknown action: {:?}", action);
                            Err(())
                        }
                    }
                }
            }
        }
    } else {
        quote! {}
    };

    let resp_impl = if has_resp {
        quote! {
            impl #impl_generics occams_rpc_stream::server::task::ServerTaskResp for #enum_name #ty_generics #where_clause {}

            impl #impl_generics occams_rpc_stream::server::task::ServerTaskEncode for #enum_name #ty_generics #where_clause {
                #[inline]
                fn encode_resp<'a, 'b, C: occams_rpc_stream::Codec>(
                    &'a mut self,
                    codec: &C,
                    buf: &'b mut Vec<u8>,
                ) -> (u64, Result<(usize, Option<&'a [u8]>), occams_rpc_stream::EncodedErr>) {
                    match self {
                        #(#encode_arms)*
                    }
                }
            }

            impl #impl_generics occams_rpc_stream::server::task::ServerTaskDone<#resp_type, #error_type> for #enum_name #ty_generics #where_clause {
                #[inline]
                fn _set_result(&mut self, res: Result<(), #error_type>) -> occams_rpc_stream::server::task::RespNoti<#resp_type> {
                    match self {
                        #(#set_result_arms)*
                    }
                }
            }
        }
    } else {
        quote! {}
    };

    let get_action_impl = if has_req {
        quote! {
            impl #impl_generics occams_rpc_stream::server::task::ServerTaskAction for #enum_name #ty_generics #where_clause {
                #[inline]
                fn get_action<'a>(&'a self) -> occams_rpc_stream::proto::RpcAction<'a> {
                    match self {
                        #(#get_action_arms)*
                    }
                }
            }
        }
    } else {
        quote! {}
    };

    let expanded = quote! {
        #ast

        #(#from_impls)*

        #req_impl

        #resp_impl

        #get_action_impl
    };
    TokenStream::from(expanded)
}

fn get_action_attribute(variant: &Variant) -> Vec<NestedMeta> {
    for attr in &variant.attrs {
        if attr.path.is_ident("action") {
            if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
                let actions: Vec<NestedMeta> = meta_list.nested.into_iter().collect();
                if !actions.is_empty() {
                    return actions;
                }
            }
        }
    }
    // No action value needed if no req
    Vec::new()
}

/// ```compile_fail
/// use occams_rpc_stream_macros::server_task_enum;
/// #[server_task_enum(req)]
/// pub struct NotAnEnum;
/// ```
#[doc(hidden)]
#[allow(dead_code)]
fn test_not_an_enum() {}

/// ```compile_fail
/// use occams_rpc_stream_macros::server_task_enum;
/// use serde_derive::{Deserialize, Serialize};
/// #[derive(Serialize, Deserialize)]
/// struct MyMsg;
/// #[server_task_enum(req)]
/// pub enum EnumNoErrorType {
///     #[action(1)]
///     Task1, // Unit variant
///     #[action(2)]
///     Task2(MyMsg, MyMsg), // Multiple fields
/// }
/// ```
#[doc(hidden)]
#[allow(dead_code)]
fn test_no_error_type() {}

/// ```compile_fail
/// use occams_rpc_stream_macros::server_task_enum;
/// use serde_derive::{Deserialize, Serialize};
/// #[derive(Serialize, Deserialize)]
/// struct MyMsg;
/// #[server_task_enum(req, error=())]
/// pub enum InvalidVariantFieldCount {
///     #[action(1)]
///     Task1, // Unit variant
///     #[action(2)]
///     Task2(MyMsg, MyMsg), // Multiple fields
/// }
/// ```
#[doc(hidden)]
#[allow(dead_code)]
fn test_invalid_variant_field_count() {}

/// ```compile_fail
/// use occams_rpc_stream_macros::server_task_enum;
/// use serde_derive::{Deserialize, Serialize};
/// #[derive(Serialize, Deserialize)]
/// struct MyMsg;
/// #[server_task_enum(req, resp, resp_type=MyMsg, error=())]
/// pub enum RespAndRespType {
///     #[action(1)]
///     Task1(MyMsg),
/// }
/// ```
#[doc(hidden)]
#[allow(dead_code)]
fn test_resp_and_resp_type() {}

/// ```compile_fail
/// use occams_rpc_stream_macros::server_task_enum;
/// use serde_derive::{Deserialize, Serialize};
/// #[derive(Serialize, Deserialize)]
/// struct MyMsg;
/// #[server_task_enum(req, error=())] // Missing resp_type
/// pub enum MissingRespType {
///     #[action(1)]
///     Task1(MyMsg),
/// }
/// ```
#[doc(hidden)]
#[allow(dead_code)]
fn test_missing_resp_type() {}

/// ```compile_fail
/// use occams_rpc_stream_macros::server_task_enum;
/// use serde_derive::{Deserialize, Serialize};
/// #[derive(Serialize, Deserialize)]
/// struct MyMsg;
/// #[server_task_enum(req, error=())]
/// pub enum MissingActionAttribute {
///     Task1(MyMsg),
/// }
/// ```
#[doc(hidden)]
#[allow(dead_code)]
fn test_missing_action_attribute() {}

/// ```compile_fail
/// use occams_rpc_stream_macros::server_task_enum;
/// use occams_rpc_stream::server::task::ServerTaskVariant;
/// use occams_rpc_stream::server::task::RespNoti;
/// use occams_rpc_stream::proto::RpcActionOwned;
/// use serde_derive::{Deserialize, Serialize};
///
/// #[derive(Default, Deserialize, Serialize)]
/// pub struct MyServerReq;
///
/// #[server_task_enum(req, resp_type=MyServerResp, error=())]
/// pub enum MyServerEnumTask {
///     #[action(1)]
///     VariantA(ServerTaskVariant<MyServerResp, MyServerReq, ()>),
///     #[action(2)]
///     VariantB(ServerTaskVariant<MyServerResp, MyServerReq, ()>), // Duplicate sub-type MyServerReq
/// }
///
/// #[derive(Default, Debug)]
/// pub struct MyServerResp;
/// ```
#[doc(hidden)]
#[allow(dead_code)]
fn test_server_task_enum_duplicate_subtype() {}