inst_derive 0.1.1

Derive macro for tagged union enums implementing the Instantiable trait in safety-net
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{Data, DeriveInput, Fields};

/// Derive macro for the Instantiable trait.
///
/// This macro works with enums where each variant wraps a type that implements Instantiable.
/// It generates an implementation that delegates all trait methods to the wrapped type.
///
/// Use the `#[instantiable(constant)]` attribute on a variant to specify which variant
/// should be used for `from_constant()`. The variant must have a `pub fn from_constant(val: Logic) -> Option<Self>`
/// method that returns an Option of the variant type.
///
/// # Example
///
/// ```rust
/// use inst_derive::Instantiable;
/// use safety_net::{Gate, Identifier, Instantiable, Logic, Net, Parameter};
/// #[derive(Debug, Clone, Instantiable)]
/// enum Cell {
///     #[instantiable(constant)]
///     Gate(Gate),
///     // additional variants
/// }
/// ```
fn impl_instantiable_trait(ast: DeriveInput) -> TokenStream2 {
    let ident = ast.ident;

    // Only support enums
    let variants = match ast.data {
        Data::Enum(data_enum) => data_enum.variants,
        _ => {
            return syn::Error::new_spanned(ident, "Instantiable can only be derived for enums")
                .to_compile_error();
        }
    };

    // Extract variant names and find the constant variant
    let mut variant_names = Vec::new();
    let mut constant_variant: Option<syn::Ident> = None;

    for variant in variants {
        let variant_name = &variant.ident;

        // Validate that the variant has exactly one unnamed field
        match variant.fields {
            Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {}
            _ => {
                return syn::Error::new_spanned(
                    variant,
                    "Each enum variant must have exactly one unnamed field",
                )
                .to_compile_error();
            }
        }

        // Check for #[instantiable(constant)] attribute
        for attr in &variant.attrs {
            if attr.path().is_ident("instantiable") {
                let result = attr.parse_nested_meta(|meta| {
                    if meta.path.is_ident("constant") {
                        if constant_variant.is_some() {
                            return Err(syn::Error::new_spanned(
                                attr,
                                "Only one variant can be marked with #[instantiable(constant)]",
                            ));
                        }
                        constant_variant = Some(variant_name.clone());
                        Ok(())
                    } else {
                        Err(meta.error("expected 'constant'"))
                    }
                });

                if let Err(err) = result {
                    return err.to_compile_error();
                }
            }
        }
        variant_names.push(variant_name.clone());
    }

    // Generate match arms for each method
    let get_name_arms = variant_names.iter().map(|v| {
        quote! { #ident::#v(inner) => inner.get_name() }
    });

    let get_input_ports_arms = variant_names.iter().map(|v| {
        quote! { #ident::#v(inner) => inner.get_input_ports().into_iter().collect::<Vec<_>>() }
    });

    let get_output_ports_arms = variant_names.iter().map(|v| {
        quote! { #ident::#v(inner) => inner.get_output_ports().into_iter().collect::<Vec<_>>() }
    });

    let has_parameter_arms = variant_names.iter().map(|v| {
        quote! { #ident::#v(inner) => inner.has_parameter(id) }
    });

    let get_parameter_arms = variant_names.iter().map(|v| {
        quote! { #ident::#v(inner) => inner.get_parameter(id) }
    });

    let set_parameter_arms = variant_names.iter().map(|v| {
        quote! { #ident::#v(inner) => inner.set_parameter(id, val) }
    });

    let parameters_arms = variant_names.iter().map(|v| {
        quote! { #ident::#v(inner) => inner.parameters().collect::<Vec<_>>().into_iter() }
    });

    let get_constant_arms = variant_names.iter().map(|v| {
        quote! { #ident::#v(inner) => inner.get_constant() }
    });

    let is_seq_arms = variant_names.iter().map(|v| {
        quote! { #ident::#v(inner) => inner.is_seq() }
    });

    // Generate from_constant implementation based on the marked variant
    let from_constant_impl = if let Some(const_var) = constant_variant {
        quote! {
            fn from_constant(val: Logic) -> Option<Self> {
                if (val == Logic::True) || (val == Logic::False) {
                    return #const_var::from_constant(val).map(#ident::#const_var);
                } else {
                    return None;
                }
            }
        }
    } else {
        quote! {
            fn from_constant(_val: Logic) -> Option<Self> {
                None
            }
        }
    };

    // Generate the implementation
    quote! {
        impl Instantiable for #ident {
            fn get_name(&self) -> &Identifier {
                match self {
                    #(#get_name_arms),*
                }
            }

            fn get_input_ports(&self) -> impl IntoIterator<Item = &Net> {
                match self {
                    #(#get_input_ports_arms),*
                }
            }

            fn get_output_ports(&self) -> impl IntoIterator<Item = &Net> {
                match self {
                    #(#get_output_ports_arms),*
                }
            }

            fn has_parameter(&self, id: &Identifier) -> bool {
                match self {
                    #(#has_parameter_arms),*
                }
            }

            fn get_parameter(&self, id: &Identifier) -> Option<Parameter> {
                match self {
                    #(#get_parameter_arms),*
                }
            }

            fn set_parameter(&mut self, id: &Identifier, val: Parameter) -> Option<Parameter> {
                match self {
                    #(#set_parameter_arms),*
                }
            }

            fn parameters(&self) -> impl Iterator<Item = (Identifier, Parameter)> {
                match self {
                    #(#parameters_arms),*
                }
            }

            #from_constant_impl

            fn get_constant(&self) -> Option<Logic> {
                match self {
                    #(#get_constant_arms),*
                }
            }

            fn is_seq(&self) -> bool {
                match self {
                    #(#is_seq_arms),*
                }
            }
        }
    }
}

#[proc_macro_derive(Instantiable, attributes(instantiable))]
pub fn inst_derive_macro(item: TokenStream) -> TokenStream {
    let ast = syn::parse(item).unwrap();
    TokenStream::from(impl_instantiable_trait(ast))
}

#[cfg(test)]
mod tests {
    use super::*;
    use syn::parse_quote;

    /// Helper function to normalize TokenStreams for comparison
    /// This removes whitespace differences and formats consistently
    fn normalize_tokenstream(ts: TokenStream2) -> String {
        // Parse and re-format to normalize
        syn::parse2::<syn::Item>(ts.clone())
            .map(|item| quote!(#item).to_string())
            .unwrap_or_else(|_| ts.to_string())
    }

    /// Compare two TokenStreams, ignoring whitespace differences
    fn assert_tokens_eq(actual: TokenStream2, expected: TokenStream2) {
        let actual_normalized = normalize_tokenstream(actual.clone());
        let expected_normalized = normalize_tokenstream(expected.clone());

        if actual_normalized != expected_normalized {
            eprintln!("=== ACTUAL ===");
            eprintln!("{}", actual_normalized);
            eprintln!("\n=== EXPECTED ===");
            eprintln!("{}", expected_normalized);
            eprintln!("\n=== DIFFERENCE ===");

            // Show a simple diff
            let actual_lines: Vec<&str> = actual_normalized.lines().collect();
            let expected_lines: Vec<&str> = expected_normalized.lines().collect();

            for (i, (a, e)) in actual_lines.iter().zip(expected_lines.iter()).enumerate() {
                if a != e {
                    eprintln!("Line {}: ", i + 1);
                    eprintln!("  Actual:   {}", a);
                    eprintln!("  Expected: {}", e);
                }
            }

            panic!("TokenStreams do not match");
        }
    }

    #[test]
    fn test_simple_enum_tokenstream() {
        let input: DeriveInput = parse_quote! {
            #[derive(Instantiable)]
            enum SimpleCell {
                Lut(Lut),
                #[instantiable(constant)]
                Gate(Gate),
            }
        };

        let output = impl_instantiable_trait(input);

        let expected = quote! {
            impl Instantiable for SimpleCell {
                fn get_name(&self) -> &Identifier {
                    match self {
                        SimpleCell::Lut(inner) => inner.get_name(),
                        SimpleCell::Gate(inner) => inner.get_name()
                    }
                }

                fn get_input_ports(&self) -> impl IntoIterator<Item = &Net> {
                    match self {
                        SimpleCell::Lut(inner) => inner.get_input_ports().into_iter().collect::<Vec<_>>(),
                        SimpleCell::Gate(inner) => inner.get_input_ports().into_iter().collect::<Vec<_>>()
                    }
                }

                fn get_output_ports(&self) -> impl IntoIterator<Item = &Net> {
                    match self {
                        SimpleCell::Lut(inner) => inner.get_output_ports().into_iter().collect::<Vec<_>>(),
                        SimpleCell::Gate(inner) => inner.get_output_ports().into_iter().collect::<Vec<_>>()
                    }
                }

                fn has_parameter(&self, id: &Identifier) -> bool {
                    match self {
                        SimpleCell::Lut(inner) => inner.has_parameter(id),
                        SimpleCell::Gate(inner) => inner.has_parameter(id)
                    }
                }

                fn get_parameter(&self, id: &Identifier) -> Option<Parameter> {
                    match self {
                        SimpleCell::Lut(inner) => inner.get_parameter(id),
                        SimpleCell::Gate(inner) => inner.get_parameter(id)
                    }
                }

                fn set_parameter(&mut self, id: &Identifier, val: Parameter) -> Option<Parameter> {
                    match self {
                        SimpleCell::Lut(inner) => inner.set_parameter(id, val),
                        SimpleCell::Gate(inner) => inner.set_parameter(id, val)
                    }
                }

                fn parameters(&self) -> impl Iterator<Item = (Identifier, Parameter)> {
                    match self {
                        SimpleCell::Lut(inner) => inner.parameters().collect::<Vec<_>>().into_iter(),
                        SimpleCell::Gate(inner) => inner.parameters().collect::<Vec<_>>().into_iter()
                    }
                }

                fn from_constant(val: Logic) -> Option<Self> {
                    if (val == Logic::True) || (val == Logic::False) {
                        return Gate::from_constant(val).map(SimpleCell::Gate);
                    } else {
                        return None;
                    }
                }

                fn get_constant(&self) -> Option<Logic> {
                    match self {
                        SimpleCell::Lut(inner) => inner.get_constant(),
                        SimpleCell::Gate(inner) => inner.get_constant()
                    }
                }

                fn is_seq(&self) -> bool {
                    match self {
                        SimpleCell::Lut(inner) => inner.is_seq(),
                        SimpleCell::Gate(inner) => inner.is_seq()
                    }
                }
            }
        };

        assert_tokens_eq(output, expected);
    }

    #[test]
    fn test_no_constant_variant_tokenstream() {
        let input: DeriveInput = parse_quote! {
            #[derive(Instantiable)]
            enum SimpleCell {
                Lut(Lut),
                Gate(Gate),
            }
        };

        let output = impl_instantiable_trait(input);

        let expected = quote! {
            impl Instantiable for SimpleCell {
                fn get_name(&self) -> &Identifier {
                    match self {
                        SimpleCell::Lut(inner) => inner.get_name(),
                        SimpleCell::Gate(inner) => inner.get_name()
                    }
                }

                fn get_input_ports(&self) -> impl IntoIterator<Item = &Net> {
                    match self {
                        SimpleCell::Lut(inner) => inner.get_input_ports().into_iter().collect::<Vec<_>>(),
                        SimpleCell::Gate(inner) => inner.get_input_ports().into_iter().collect::<Vec<_>>()
                    }
                }

                fn get_output_ports(&self) -> impl IntoIterator<Item = &Net> {
                    match self {
                        SimpleCell::Lut(inner) => inner.get_output_ports().into_iter().collect::<Vec<_>>(),
                        SimpleCell::Gate(inner) => inner.get_output_ports().into_iter().collect::<Vec<_>>()
                    }
                }

                fn has_parameter(&self, id: &Identifier) -> bool {
                    match self {
                        SimpleCell::Lut(inner) => inner.has_parameter(id),
                        SimpleCell::Gate(inner) => inner.has_parameter(id)
                    }
                }

                fn get_parameter(&self, id: &Identifier) -> Option<Parameter> {
                    match self {
                        SimpleCell::Lut(inner) => inner.get_parameter(id),
                        SimpleCell::Gate(inner) => inner.get_parameter(id)
                    }
                }

                fn set_parameter(&mut self, id: &Identifier, val: Parameter) -> Option<Parameter> {
                    match self {
                        SimpleCell::Lut(inner) => inner.set_parameter(id, val),
                        SimpleCell::Gate(inner) => inner.set_parameter(id, val)
                    }
                }

                fn parameters(&self) -> impl Iterator<Item = (Identifier, Parameter)> {
                    match self {
                        SimpleCell::Lut(inner) => inner.parameters().collect::<Vec<_>>().into_iter(),
                        SimpleCell::Gate(inner) => inner.parameters().collect::<Vec<_>>().into_iter()
                    }
                }

                fn from_constant(_val: Logic) -> Option<Self> {
                    None
                }

                fn get_constant(&self) -> Option<Logic> {
                    match self {
                        SimpleCell::Lut(inner) => inner.get_constant(),
                        SimpleCell::Gate(inner) => inner.get_constant()
                    }
                }

                fn is_seq(&self) -> bool {
                    match self {
                        SimpleCell::Lut(inner) => inner.is_seq(),
                        SimpleCell::Gate(inner) => inner.is_seq()
                    }
                }
            }
        };

        assert_tokens_eq(output, expected);
    }

    #[test]
    fn test_two_constant_variants_error() {
        let input: DeriveInput = parse_quote! {
            #[derive(Instantiable)]
            enum SimpleCell {
                #[instantiable(constant)]
                Lut(Lut),
                #[instantiable(constant)]
                Gate(Gate),
            }
        };

        let output = impl_instantiable_trait(input);

        let expected_error_msg = "Only one variant can be marked with #[instantiable(constant)]";

        let output_str = output.to_string();
        assert!(
            output_str.contains(expected_error_msg),
            "Expected error message not found. Output was:\n{}",
            output_str
        );
    }
}