fovea-derive 0.1.1

Derive macros for fovea pixel and image traits
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Attribute, Data, DeriveInput, Fields, Type};

/// Main entry point for the HomogeneousPixel derive macro.
///
/// # Steps:
/// 1. Validate that the input is a struct (not enum or union)
/// 2. Validate that the struct has `#[repr(C)]` or `#[repr(transparent)]`
/// 3. Extract fields and verify all fields have the same type
/// 4. Generate `Channel` = common field type, `Channels` = `[Channel; N]`
/// 5. Emit `unsafe impl HomogeneousPixel for StructName { ... }`
pub(crate) fn derive(input: DeriveInput) -> syn::Result<TokenStream> {
    // Step 1 - Validate struct
    let name = &input.ident;
    let fields = validate_struct(&input)?;

    // Step 2 - Validate repr
    let repr = validate_repr(&input, fields)?;

    // Step 3 - Extract channel type and count
    let (channel_ty, channel_count) = extract_uniform_channel(fields, &repr)?;

    // Step 4 - Generate the impl block
    //
    // Force evaluation of the compile-time size assertion defined as a
    // default associated constant in `HomogeneousPixel`.  See REVIEW.md issue #2.
    let expanded = quote! {
        unsafe impl ::fovea::pixel::HomogeneousPixel for #name {
            type Channel = #channel_ty;
            type Channels = [#channel_ty; #channel_count];
        }
        const _: () = { let _ = <#name as ::fovea::pixel::HomogeneousPixel>::_SIZE_ASSERT; };
    };

    Ok(expanded)
}

/// Extracts the uniform channel type and count from the struct fields.
///
/// For `repr(C)`: all fields must have the same type. Returns (field_type, field_count).
/// For `repr(transparent)`: exactly one field. Returns (field_type, 1).
fn extract_uniform_channel(fields: &Fields, repr: &Repr) -> syn::Result<(Type, usize)> {
    let field_list: Vec<_> = fields.iter().collect();

    match repr {
        Repr::C => {
            // Already validated: at least one field
            let first_ty = &field_list[0].ty;
            let first_ty_str = type_to_string(first_ty);

            for (i, field) in field_list.iter().enumerate().skip(1) {
                let ty_str = type_to_string(&field.ty);
                if ty_str != first_ty_str {
                    let msg = if let Some(ref ident) = field.ident {
                        format!(
                            "HomogeneousPixel requires all fields to have the same type, \
                             but field `{ident}` (index {i}) has type `{ty_str}` while the first field has type `{first_ty_str}`"
                        )
                    } else {
                        format!(
                            "HomogeneousPixel requires all fields to have the same type, \
                             but field at index {i} has type `{ty_str}` while the first field has type `{first_ty_str}`"
                        )
                    };
                    return Err(syn::Error::new_spanned(&field.ty, msg));
                }
            }

            Ok((first_ty.clone(), field_list.len()))
        }
        Repr::Transparent => {
            // Already validated: exactly one field
            let field = &field_list[0];
            Ok((field.ty.clone(), 1))
        }
    }
}

/// Converts a `syn::Type` to a comparable string representation.
fn type_to_string(ty: &Type) -> String {
    quote!(#ty).to_string()
}

/// Validates that the input is a struct.
/// Returns the fields if valid.
fn validate_struct(input: &DeriveInput) -> syn::Result<&Fields> {
    match &input.data {
        Data::Struct(data) => Ok(&data.fields),
        Data::Enum(_) => Err(syn::Error::new_spanned(
            &input.ident,
            "HomogeneousPixel can only be derived for structs, not enums",
        )),
        Data::Union(_) => Err(syn::Error::new_spanned(
            &input.ident,
            "HomogeneousPixel can only be derived for structs, not unions",
        )),
    }
}

#[derive(Debug)]
enum Repr {
    C,
    Transparent,
}

/// Validates that the struct has `#[repr(C)]` or `#[repr(transparent)]`.
fn validate_repr(input: &DeriveInput, fields: &Fields) -> syn::Result<Repr> {
    let has_repr_c = has_repr_c(&input.attrs);
    let has_repr_transparent = has_repr_transparent(&input.attrs);

    match (has_repr_c, has_repr_transparent) {
        (true, false) => {
            if fields.is_empty() {
                Err(syn::Error::new_spanned(
                    &input.ident,
                    "HomogeneousPixel requires at least one field",
                ))
            } else {
                Ok(Repr::C)
            }
        }
        (false, true) => {
            if fields.len() != 1 {
                Err(syn::Error::new_spanned(
                    &input.ident,
                    "HomogeneousPixel requires exactly one field when #[repr(transparent)] is used",
                ))
            } else {
                Ok(Repr::Transparent)
            }
        }
        (true, true) => Err(syn::Error::new_spanned(
            &input.ident,
            "HomogeneousPixel cannot be derived for structs with both #[repr(C)] and #[repr(transparent)] attributes",
        )),
        (false, false) => Err(syn::Error::new_spanned(
            &input.ident,
            "HomogeneousPixel requires #[repr(C)] or #[repr(transparent)] attribute",
        )),
    }
}

fn has_repr_c(attrs: &[Attribute]) -> bool {
    attrs.iter().any(|attr| {
        attr.path().is_ident("repr")
            && matches!(&attr.meta, syn::Meta::List(ml) if ml.tokens.to_string().contains("C"))
    })
}

fn has_repr_transparent(attrs: &[Attribute]) -> bool {
    attrs.iter().any(|attr| {
        attr.path().is_ident("repr")
            && matches!(&attr.meta, syn::Meta::List(ml) if ml.tokens.to_string().contains("transparent"))
    })
}

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

    #[test]
    fn test_validate_struct_named_fields() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel {
                r: u8,
                g: u8,
                b: u8,
            }
        };
        assert!(validate_struct(&input).is_ok());
    }

    #[test]
    fn test_validate_struct_tuple_fields() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel(u8, u8, u8);
        };
        assert!(validate_struct(&input).is_ok());
    }

    #[test]
    fn test_validate_struct_rejects_enum() {
        let input: DeriveInput = syn::parse_quote! {
            enum BadPixel {
                Red,
            }
        };
        let result = validate_struct(&input);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("not enums"));
    }

    #[test]
    fn test_validate_struct_rejects_union() {
        let input: DeriveInput = syn::parse_quote! {
            union BadPixel {
                a: u8,
                b: u16,
            }
        };
        let result = validate_struct(&input);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("not unions"));
    }

    #[test]
    fn test_validate_repr_c() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel {
                r: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        assert!(matches!(validate_repr(&input, fields), Ok(Repr::C)));
    }

    #[test]
    fn test_validate_repr_transparent() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(transparent)]
            struct TestPixel {
                value: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        assert!(matches!(
            validate_repr(&input, fields),
            Ok(Repr::Transparent)
        ));
    }

    #[test]
    fn test_validate_repr_missing() {
        let input: DeriveInput = syn::parse_quote! {
            struct TestPixel {
                r: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        let result = validate_repr(&input, fields);
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("requires #[repr(C)]")
        );
    }

    #[test]
    fn test_validate_repr_c_no_fields() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel {}
        };
        let fields = validate_struct(&input).unwrap();
        let result = validate_repr(&input, fields);
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("at least one field")
        );
    }

    #[test]
    fn test_extract_uniform_channel_same_types() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel {
                r: u8,
                g: u8,
                b: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        let repr = Repr::C;
        let (ty, count) = extract_uniform_channel(fields, &repr).unwrap();
        assert_eq!(type_to_string(&ty), "u8");
        assert_eq!(count, 3);
    }

    #[test]
    fn test_extract_uniform_channel_mixed_types() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel {
                r: u8,
                g: u16,
                b: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        let repr = Repr::C;
        let result = extract_uniform_channel(fields, &repr);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("same type"));
    }

    #[test]
    fn test_extract_uniform_channel_transparent_single() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(transparent)]
            struct TestPixel(u8);
        };
        let fields = validate_struct(&input).unwrap();
        let repr = Repr::Transparent;
        let (ty, count) = extract_uniform_channel(fields, &repr).unwrap();
        assert_eq!(type_to_string(&ty), "u8");
        assert_eq!(count, 1);
    }

    #[test]
    fn test_extract_uniform_channel_tuple_struct() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel(u16, u16);
        };
        let fields = validate_struct(&input).unwrap();
        let repr = Repr::C;
        let (ty, count) = extract_uniform_channel(fields, &repr).unwrap();
        assert_eq!(type_to_string(&ty), "u16");
        assert_eq!(count, 2);
    }

    #[test]
    fn test_derive_generates_correct_tokens() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct Rgb8 {
                r: u8,
                g: u8,
                b: u8,
            }
        };
        let tokens = derive(input).unwrap();
        let output = tokens.to_string();
        assert!(output.contains("HomogeneousPixel"));
        assert!(output.contains("type Channel = u8"));
        // quote! may render the count as `3usize`, `3_usize`, or `3` depending on version
        assert!(output.contains("type Channels = [u8 ;"));
        assert!(
            output.contains("_SIZE_ASSERT"),
            "must force-evaluate _SIZE_ASSERT"
        );
    }

    #[test]
    fn test_extract_uniform_channel_tuple_mixed_types() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel(u8, u16, u8);
        };
        let fields = validate_struct(&input).unwrap();
        let repr = Repr::C;
        let result = extract_uniform_channel(fields, &repr);
        assert!(result.is_err());
        let err_msg = result.err().unwrap().to_string();
        assert!(err_msg.contains("same type"));
        assert!(err_msg.contains("index"));
    }

    #[test]
    fn test_validate_repr_both_c_and_transparent() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            #[repr(transparent)]
            struct TestPixel {
                value: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        let result = validate_repr(&input, fields);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("both"));
    }

    #[test]
    fn test_validate_repr_transparent_multiple_fields() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(transparent)]
            struct TestPixel {
                a: u8,
                b: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        let result = validate_repr(&input, fields);
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("exactly one field")
        );
    }

    #[test]
    fn test_has_repr_c_true() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct Foo { x: u8 }
        };
        assert!(has_repr_c(&input.attrs));
        assert!(!has_repr_transparent(&input.attrs));
    }

    #[test]
    fn test_has_repr_transparent_true() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(transparent)]
            struct Foo { x: u8 }
        };
        assert!(!has_repr_c(&input.attrs));
        assert!(has_repr_transparent(&input.attrs));
    }

    #[test]
    fn test_has_repr_neither() {
        let input: DeriveInput = syn::parse_quote! {
            struct Foo { x: u8 }
        };
        assert!(!has_repr_c(&input.attrs));
        assert!(!has_repr_transparent(&input.attrs));
    }

    #[test]
    fn test_derive_repr_transparent_full() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(transparent)]
            struct Wrapped {
                value: u8,
            }
        };
        let tokens = derive(input).unwrap();
        let output = tokens.to_string();
        assert!(output.contains("HomogeneousPixel"));
        assert!(output.contains("type Channel = u8"));
        assert!(output.contains("type Channels = [u8 ;"));
        assert!(
            output.contains("_SIZE_ASSERT"),
            "must force-evaluate _SIZE_ASSERT"
        );
    }

    #[test]
    fn test_has_repr_with_non_repr_attr() {
        // A non-repr attribute exercises the `false` branch inside the `.any()` closure
        let input: DeriveInput = syn::parse_quote! {
            #[derive(Clone)]
            struct Foo { x: u8 }
        };
        assert!(!has_repr_c(&input.attrs));
        assert!(!has_repr_transparent(&input.attrs));
    }

    // -----------------------------------------------------------------------
    // derive() error-path tests — exercise the `?` branches inside derive()
    // -----------------------------------------------------------------------

    #[test]
    fn test_derive_rejects_enum() {
        let input: DeriveInput = syn::parse_quote! {
            enum BadPixel { Red }
        };
        let result = derive(input);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("not enums"));
    }

    #[test]
    fn test_derive_rejects_union() {
        let input: DeriveInput = syn::parse_quote! {
            union BadPixel { a: u8, b: u16 }
        };
        let result = derive(input);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("not unions"));
    }

    #[test]
    fn test_derive_rejects_missing_repr() {
        let input: DeriveInput = syn::parse_quote! {
            struct TestPixel { r: u8 }
        };
        let result = derive(input);
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("requires #[repr(C)]")
        );
    }

    #[test]
    fn test_derive_rejects_repr_c_no_fields() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct Empty {}
        };
        let result = derive(input);
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("at least one field")
        );
    }

    #[test]
    fn test_derive_rejects_mixed_types() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct BadPixel { r: u8, g: u16 }
        };
        let result = derive(input);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("same type"));
    }
}