flow_record_derive/
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
use darling::FromDeriveInput;
use proc_macro::TokenStream;
use quote::quote;
use record_attributes::RecordAttributes;
use struct_info::StructInfo;
use syn::{parse_macro_input, DeriveInput};

mod field_info;
mod record_attributes;
mod struct_info;

#[proc_macro_derive(FlowRecord, attributes(flow_record))]
pub fn derive_flow_record(input: TokenStream) -> TokenStream {
    let mut input = parse_macro_input!(input as DeriveInput);
    FromDeriveInput::from_derive_input(&input)
        .and_then(|attrs| expand(&mut input, attrs))
        .map(Into::into)
        // Error handling
        .unwrap_or_else(|e| e.write_errors().into())
}

fn expand(ast: &mut syn::DeriveInput, attrs: RecordAttributes) -> darling::Result<TokenStream> {
    let name = &ast.ident;
    let name_as_string = name.to_string();

    let descriptor;
    let hash;
    let values: Vec<_>;

    let from_parameter_name = quote! {self};

    match &ast.data {
        syn::Data::Struct(s) => {
            let struct_info = StructInfo::new(name_as_string.clone(), s, attrs);
            descriptor = struct_info.descriptor();
            hash = struct_info.descriptor_hash();
            values = struct_info.values(quote! {#from_parameter_name}).collect();
        }

        syn::Data::Enum(_) => panic!("no support for enums yet"),
        syn::Data::Union(_) => panic!("no support for unions yet"),
    }

    let gen = quote!(
        use rmpv::Value;

        impl flow_record_common::FlowRecord for #name {
            fn name() -> &'static str {
                #name_as_string
            }
            fn descriptor() -> &'static Value {
                static D: std::sync::LazyLock<Value> = std::sync::LazyLock::new(|| Value::from(#descriptor));
                &*D
            }
            fn descriptor_hash() -> u32 {
                static H: std::sync::LazyLock<u32> = std::sync::LazyLock::new(|| #hash);
                *H
            }
            fn into_value(self) -> Value {
                Value::Array(vec![
                    #(#values),*,
                ])
            }
        }
    );
    Ok(gen.into())
}