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
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::{parse_macro_input, spanned::Spanned, Data, DeriveInput, Fields};

extern crate proc_macro;

/// Derives RosParams trait for a structure to use it with
/// `r2r::Node::make_derived_parameter_handler()`.
#[proc_macro_derive(RosParams)]
pub fn derive_r2r_params(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    // Parse the input tokens into a syntax tree.
    let input = parse_macro_input!(input as DeriveInput);

    // Used in the quasi-quotation below as `#name`.
    let name = input.ident;

    let register_calls = get_register_calls(&input.data);
    let get_param_matches = param_matches_for(quote!(get_parameter(suffix)), &input.data);
    let set_param_matches =
        param_matches_for(quote!(set_parameter(suffix, param_val)), &input.data);

    let expanded = quote! {
        // The generated impl.
        impl ::r2r::RosParams for #name {
            fn register_parameters(
                &mut self,
                prefix: &str,
                desc: ::std::option::Option<::r2r::Parameter>,
                params: &mut ::r2r::indexmap::IndexMap<String, ::r2r::Parameter>,
            ) -> ::r2r::Result<()> {
                let prefix = if prefix.is_empty() {
                    String::from("")
                } else {
                    format!("{prefix}.")
                };
                #register_calls
                Ok(())
            }
            fn get_parameter(&mut self, param_name: &str) -> ::r2r::Result<::r2r::ParameterValue>
            {
                let (prefix, suffix) = match param_name.split_once('.') {
                    None => (param_name, ""),
                    Some((prefix, suffix)) => (prefix, suffix)
                };
                let result = match prefix {
                    #get_param_matches
                    _ => Err(::r2r::Error::InvalidParameterName {
                        name: "".into(),
                    }),
                };
                result.map_err(|e| e.update_param_name(&param_name))
            }
            fn set_parameter(&mut self, param_name: &str, param_val: &::r2r::ParameterValue) -> ::r2r::Result<()>
            {
                let (prefix, suffix) = match param_name.split_once('.') {
                    None => (param_name, ""),
                    Some((prefix, suffix)) => (prefix, suffix)
                };
                let result = match prefix {
                    #set_param_matches
                    _ => Err(::r2r::Error::InvalidParameterName {
                        name: "".into(),
                    }),
                };
                result.map_err(|e| e.update_param_name(&param_name))
            }
        }
    };

    // Hand the output tokens back to the compiler.
    proc_macro::TokenStream::from(expanded)
}

// Generate calls to register functions of struct fields
fn get_register_calls(data: &Data) -> TokenStream {
    match *data {
        Data::Struct(ref data) => match data.fields {
            Fields::Named(ref fields) => {
                let field_matches = fields.named.iter().map(|f| {
                    let name = &f.ident;
                    let format_str = format!("{{prefix}}{}", name.as_ref().unwrap());
                    let desc = get_field_doc(f);
                    quote_spanned! {
                        f.span() =>
                            let param = ::r2r::Parameter {
                                value: ::r2r::ParameterValue::NotSet, // will be set for leaf params by register_parameters() below
                                description: #desc,
                            };
                            self.#name.register_parameters(&format!(#format_str), Some(param), params)?;
                    }
                });
                quote! {
                    #(#field_matches)*
                }
            }
            _ => unimplemented!(),
        },
        Data::Enum(_) | Data::Union(_) => unimplemented!(),
    }
}

fn get_field_doc(f: &syn::Field) -> String {
    if let Some(doc) = f
        .attrs
        .iter()
        .find(|&attr| attr.path().get_ident().is_some_and(|id| id == "doc"))
    {
        match &doc.meta.require_name_value().unwrap().value {
            ::syn::Expr::Lit(exprlit) => match &exprlit.lit {
                ::syn::Lit::Str(s) => s.value().trim().to_owned(),
                _ => unimplemented!(),
            },
            _ => unimplemented!(),
        }
    } else {
        "".to_string()
    }
}

// Generate match arms for RosParams::update_parameters()
fn param_matches_for(call: TokenStream, data: &Data) -> TokenStream {
    match *data {
        Data::Struct(ref data) => match data.fields {
            Fields::Named(ref fields) => {
                let field_matches = fields.named.iter().map(|f| {
                    let name = &f.ident;
                    let name_str = format!("{}", name.as_ref().unwrap());
                    quote_spanned! {
                        f.span() =>
                            #name_str => {
                                self.#name.#call
                            }
                    }
                });
                quote! {
                    #(#field_matches)*
                }
            }
            _ => unimplemented!(),
        },
        Data::Enum(_) | Data::Union(_) => unimplemented!(),
    }
}