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
extern crate proc_macro;

use heck::{KebabCase, SnakeCase};
use proc_macro_error::{abort, proc_macro_error, ResultExt};
use quote::{format_ident, quote};
use syn::{
    self,
    parse::{Parse, ParseStream},
    parse_macro_input,
    punctuated::Punctuated,
    DeriveInput, Ident, LitStr, Token,
};

/// Proc macro used for autogenerating "Scopes" from resource objects.
///
/// This crate is very tightly coupled with the "zone" crate; it injects
/// methods into the `zone::Config` object.
///
/// The following attribute macros may be used:
/// - resource(name = "NAME")
///   Allows setting a custom name for a field to be emitted to the zonecfg command.
/// - resource(selector)
///   Identifies that this field may be used to select the resource, as a
///   query parameter for searching across resources.
/// - resources(global)
///   (Can be attached to any field, it is parsed per-resource)
///   Identifies that this is the Global resource, which has some unique
///   handling.
///
/// For each resource, the following is generated:
/// - For non-global resources:
///     Config::add_{resource_name} to add a new resource.
///     Config::select_{resource_name}_by_{selector} for all selectors.
///     Config::remove_all_{resource} to remove a resource
///     Config::remove_{resource_name}_by_{selector} for all selectors.
/// - For the global resource:
///     Config::get_global to select the global resource.
/// - For all resources:
///     {resource_name}Scope, an object representing the scope,
///     which contains a collection of setters for each field.
///     Objects which can be cleared accept optional arguments; providing
///     `None` clears the parameter, providing `Some(...)` sets the parameter.
#[proc_macro_derive(Resource, attributes(resource))]
#[proc_macro_error]
pub fn derive_resource(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(item as DeriveInput);
    let input_name = input.ident;

    let data = match input.data {
        syn::Data::Struct(data) => data,
        _ => panic!("Resource only valid for structures"),
    };

    let fields = match data.fields {
        syn::Fields::Named(fields) => fields,
        _ => panic!("Named fields only"),
    };

    let mut global_attrs = GlobalAttrs { attrs: vec![] };

    let parsed_fields: Vec<ParsedField> = fields
        .named
        .into_iter()
        .map(|field| {
            let (globals, field_attrs) = parse_attributes(&field.attrs);
            global_attrs.attrs.extend(globals);
            ParsedField {
                field,
                attrs: field_attrs,
            }
        })
        .collect();

    let scope_name = get_scope_name(&input_name);

    // Within the scope object, provide setters.
    let scope_setters = setters(&scope_name, &parsed_fields);

    // Mechanism to construct/destroy scope.
    let scope_constructor = constructor(&input_name, &parsed_fields, &global_attrs);

    let scope_selectors = selectors(&input_name, &parsed_fields);

    let scope_msg = format!(
        "Generated scope for the [{}] resource.\n\n\
        This object represents the resource scope for a zone configuration, and
        automatically closes that scope when dropped.\n\n\
        To construct this object, refer to [Config::{}].",
        input_name.to_string(),
        if global_attrs.is_global_resource() {
            format!("get_{}", input_name.to_string().to_snake_case())
        } else {
            format!("add_{}", input_name.to_string().to_snake_case())
        }
    );
    let tokens = quote! {
        // Auto-generated implementation of Scoped resource.

        #[doc = #scope_msg]
        pub struct #scope_name<'a> {
            config: &'a mut Config,
        }

        impl<'a> #scope_name<'a> {
            fn push(&mut self, arg: impl Into<String>) {
                self.config.push(arg.into())
            }
        }

        #scope_setters
        #scope_constructor
        #scope_selectors
    };
    proc_macro::TokenStream::from(tokens)
}

fn get_scope_name(input_name: &Ident) -> Ident {
    format_ident!("{}Scope", input_name)
}

// Within the scope object, provide setters.
fn setters(scope_name: &Ident, parsed_fields: &Vec<ParsedField>) -> proc_macro2::TokenStream {
    parsed_fields
        .iter()
        .map(|parsed| {
            let name = parsed.name();
            let ty = parsed.ty();
            let setter = format_ident!("set_{}", parsed.field_name());

            quote! {
                impl<'a> #scope_name<'a> {
                    pub fn #setter(&mut self, value: impl Into<#ty>) -> &mut Self {
                        let value: #ty = value.into();
                        for property in value.get_properties() {
                            let name = match &property.name {
                                PropertyName::Implicit => #name,
                                PropertyName::Explicit(name) => name,
                            };
                            self.push(format!("set {}={}", name, property.value));
                        }
                        for property_name in value.get_clearables() {
                            let name = match &property_name {
                                PropertyName::Implicit => #name,
                                PropertyName::Explicit(name) => name,
                            };
                            self.push(format!("clear {}", name));
                        }
                        self
                    }
                }
            }
        })
        .collect()
}

fn selectors(input_name: &Ident, parsed_fields: &Vec<ParsedField>) -> proc_macro2::TokenStream {
    let scope_name = get_scope_name(&input_name);
    let input_name_kebab = input_name.to_string().to_kebab_case();
    parsed_fields
        .iter()
        .map(|parsed| {
            if parsed.selector() {
                let name = parsed.name();
                let snake_input_name = input_name.to_string().to_snake_case();
                let ty = parsed.ty();
                let selector = format_ident!("select_{}_by_{}", snake_input_name, name,);
                let selector_msg = format!(
                    "Generated selector for the [{}] resource.\n\n\
                    Allows the selection of an existing resource for modification
                    with a matching value of [{}::{}].",
                    input_name.to_string(),
                    input_name.to_string(),
                    parsed.field_name(),
                );

                let remover = format_ident!("remove_{}_by_{}", snake_input_name, name,);
                let remover_msg = format!(
                    "Generated removal function for the [{}] resource\n\n\
                    Allows the removal of all existing resources with a matching
                    value of [{}::{}].",
                    input_name.to_string(),
                    input_name.to_string(),
                    parsed.field_name(),
                );
                quote! {
                    impl Config {
                        #[doc = #selector_msg]
                        pub fn #selector(&mut self, value: impl Into<#ty>) -> #scope_name {
                            let value: #ty = value.into();
                            let mut scope = #scope_name {
                                config: self
                            };
                            scope.push(
                                format!("select {} {}={}",
                                    #input_name_kebab,
                                    #name,
                                    value,
                                )
                            );
                            scope
                        }

                        #[doc = #remover_msg]
                        pub fn #remover(&mut self, value: impl Into<#ty>) {
                            let value: #ty = value.into();
                            self.push(
                                format!(
                                    "remove -F {} {}={}",
                                    #input_name_kebab,
                                    #name,
                                    value,
                                )
                            );
                        }

                    }
                }
            } else {
                quote! {}
            }
        })
        .collect()
}

// Create the mechanism to create/destroy the scope object.
fn constructor(
    input_name: &Ident,
    parsed_fields: &Vec<ParsedField>,
    global_attrs: &GlobalAttrs,
) -> proc_macro2::TokenStream {
    let scope_name = get_scope_name(&input_name);
    let input_name_snake = input_name.to_string().to_snake_case();
    let input_name_kebab = input_name.to_string().to_kebab_case();

    // Given:
    // - A scope object named "scope"
    // - A local named "values"
    //
    // Push back all possible values to the scope.
    let initial_set_values: proc_macro2::TokenStream = parsed_fields
        .iter()
        .map(|parsed| {
            let values = format_ident!("values");
            let field = parsed.field.ident.as_ref().unwrap();
            let name = parsed.name();
            quote! {
                for property in #values.#field.get_properties() {
                    let name = match &property.name {
                        PropertyName::Implicit => #name,
                        PropertyName::Explicit(name) => name,
                    };
                    scope.push(format!("set {}={}", name, property.value));
                }
            }
        })
        .collect();

    if global_attrs.is_global_resource() {
        let scope_get = format_ident!("get_{}", input_name_snake);
        let scope_get_msg = format!(
            "Acquire a reference to the global resource scope.
            This scope allows callers to safely set values within the [{}] object.",
            input_name.to_string()
        );
        quote! {
            impl<'a> #scope_name<'a> {
                fn new(config: &'a mut Config) -> Self {
                    let mut scope = #scope_name {
                        config
                    };
                    scope
                }
            }

            impl Config {
                #[doc = #scope_get_msg]
                pub fn #scope_get(&mut self) -> #scope_name {
                    #scope_name::new(self)
                }
            }
        }
    } else {
        let scope_adder = format_ident!("add_{}", input_name_snake);
        let scope_adder_msg = format!(
            "Creates a new scope from a [{}] object. This begins
            specification for the resource, and returns an object which
            represents the new scope.",
            input_name.to_string()
        );

        let scope_removal = format_ident!("remove_all_{}", input_name_snake);
        let scope_removal_msg = format!(
            "Deletes resources associated with the [{}] object.",
            input_name.to_string()
        );

        quote! {
            impl<'a> Drop for #scope_name<'a> {
                /// Emits an "end" token, signifing the end of a resource scope.
                fn drop(&mut self) {
                    self.push("end".to_string());
                }
            }

            // Auto-generated bindings within the config object.
            impl Config {
                #[doc = #scope_adder_msg]
                pub fn #scope_adder(&mut self, values: &#input_name) -> #scope_name {
                    let mut scope = #scope_name {
                        config: self
                    };

                    scope.push(format!("add {}", #input_name_kebab));
                    #initial_set_values
                    scope
                }

                #[doc = #scope_removal_msg]
                pub fn #scope_removal(&mut self) {
                    self.push(format!("remove -F {}", #input_name_kebab));
                }
            }
        }
    }
}

struct GlobalAttrs {
    attrs: Vec<ResourceAttr>,
}

impl GlobalAttrs {
    fn is_global_resource(&self) -> bool {
        for attr in &self.attrs {
            if let ResourceAttr::Global(_) = attr {
                return true;
            }
        }
        false
    }
}

struct ParsedField {
    field: syn::Field,
    attrs: Vec<ResourceAttr>,
}

impl ParsedField {
    fn selector(&self) -> bool {
        for attr in &self.attrs {
            if let ResourceAttr::Selector(_) = attr {
                return true;
            }
        }
        false
    }

    fn field_name(&self) -> String {
        self.field.ident.as_ref().unwrap().to_string()
    }

    fn name(&self) -> String {
        for attr in &self.attrs {
            if let ResourceAttr::Name(_, s) = attr {
                return s.value();
            }
        }
        self.field_name()
    }

    fn ty(&self) -> proc_macro2::TokenStream {
        let ty = &self.field.ty;
        quote! { #ty }
    }
}

enum ResourceAttr {
    // Per-resource attributes
    Global(Ident),

    // Per-field attributes
    Selector(Ident),
    Name(Ident, LitStr),
}

impl Parse for ResourceAttr {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let name: Ident = input.parse()?;
        let name_str = name.to_string();

        if input.peek(Token![=]) {
            // skip '='
            let _ = input.parse::<Token![=]>()?;

            let lit: LitStr = input.parse()?;
            match name_str.as_ref() {
                "name" => Ok(ResourceAttr::Name(name, lit)),
                _ => abort!(name, "Unexpected attribute: {}", name_str),
            }
        } else {
            match name_str.as_ref() {
                "selector" => Ok(ResourceAttr::Selector(name)),
                "global" => Ok(ResourceAttr::Global(name)),
                _ => abort!(name, "Unexpected attribute: {}", name_str),
            }
        }
    }
}

fn parse_attributes(attrs: &[syn::Attribute]) -> (Vec<ResourceAttr>, Vec<ResourceAttr>) {
    attrs
        .iter()
        .filter(|attr| attr.path.is_ident("resource"))
        .flat_map(|attr| {
            attr.parse_args_with(Punctuated::<ResourceAttr, Token![,]>::parse_terminated)
                .unwrap_or_abort()
        })
        .partition(|attr| match attr {
            ResourceAttr::Global(_) => true,
            _ => false,
        })
}