es-entity-macros 0.20.2

Proc macros for es-entity
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
use darling::{FromDeriveInput, FromField, ToTokens};
use proc_macro2::TokenStream;
use quote::{TokenStreamExt, quote};
use syn::{Type, ext::IdentExt};

#[derive(Debug, FromField)]
#[darling(attributes(es_entity))]
struct Field {
    ident: Option<syn::Ident>,
    ty: Type,
    #[darling(default)]
    events: bool,
    #[darling(default)]
    nested: bool,
    #[darling(default)]
    default: bool,
}

impl Field {
    fn option_inner_type(&self) -> Option<&Type> {
        if let Type::Path(type_path) = &self.ty
            && type_path.qself.is_none()
            && let Some(segment) = type_path.path.segments.last()
            && segment.ident == "Option"
            && let syn::PathArguments::AngleBracketed(generic_args) = &segment.arguments
            && let Some(syn::GenericArgument::Type(inner_type)) = generic_args.args.first()
        {
            return Some(inner_type);
        }
        None
    }

    fn defaults_when_unset(&self) -> bool {
        self.default || self.nested
    }

    fn is_events_field(&self) -> bool {
        self.events || self.ident.as_ref().is_some_and(|i| i == "events")
    }

    /// The `S` in `EntityEvents<E, S>`, or `es_entity::NoSnapshot` when the
    /// field is bare `EntityEvents<E>`.
    fn snapshot_type(&self) -> TokenStream {
        if let Type::Path(type_path) = &self.ty
            && let Some(segment) = type_path.path.segments.last()
            && segment.ident == "EntityEvents"
            && let syn::PathArguments::AngleBracketed(generic_args) = &segment.arguments
            && let Some(syn::GenericArgument::Type(snapshot_ty)) = generic_args.args.get(1)
        {
            return quote! { #snapshot_ty };
        }
        quote! { es_entity::NoSnapshot }
    }

    fn extract_nested_entity_type(&self) -> &Type {
        if let Type::Path(type_path) = &self.ty
            && let Some(segment) = type_path.path.segments.last()
            && segment.ident == "Nested"
            && let syn::PathArguments::AngleBracketed(generic_args) = &segment.arguments
            && let Some(syn::GenericArgument::Type(inner_type)) = generic_args.args.first()
        {
            return inner_type;
        }
        panic!("Field must be of type Nested<T>");
    }
}

#[derive(Debug, FromDeriveInput)]
#[darling(supports(struct_named), attributes(es_entity))]
pub struct EsEntity {
    ident: syn::Ident,
    #[darling(default, rename = "new")]
    new_entity_ident: Option<syn::Ident>,
    #[darling(default, rename = "event")]
    event_ident: Option<syn::Ident>,
    #[darling(default, rename = "hydrator")]
    hydrator_vis: Option<syn::Visibility>,
    data: darling::ast::Data<(), Field>,
}

impl EsEntity {
    fn find_events_field(&self) -> Option<&Field> {
        match &self.data {
            darling::ast::Data::Struct(fields) => {
                fields.iter().find(|field| field.is_events_field())
            }
            _ => None,
        }
    }

    fn nested_fields(&self) -> Vec<&Field> {
        match &self.data {
            darling::ast::Data::Struct(fields) => {
                fields.iter().filter(|field| field.nested).collect()
            }
            _ => Vec::new(),
        }
    }

    fn hydrated_fields(&self) -> Vec<&Field> {
        match &self.data {
            darling::ast::Data::Struct(fields) => fields
                .iter()
                .filter(|field| !field.is_events_field())
                .collect(),
            _ => Vec::new(),
        }
    }

    fn validate_hydrator(&self) -> darling::Result<()> {
        let mut errors = darling::Error::accumulator();
        let mut setters: Vec<(String, &syn::Ident)> = Vec::new();
        for field in self.hydrated_fields() {
            let Some(ident) = field.ident.as_ref() else {
                continue;
            };
            let name = ident.unraw().to_string();
            if name == "build" {
                errors.push(
                    darling::Error::custom(
                        "a field named `build` collides with the generated hydrator's `build`",
                    )
                    .with_span(ident),
                );
                continue;
            }
            let mut names = vec![name.clone()];
            if field.option_inner_type().is_some() {
                names.push(format!("maybe_{name}"));
            }
            for setter in names {
                if let Some((_, other)) = setters.iter().find(|(existing, _)| *existing == setter) {
                    errors.push(
                        darling::Error::custom(format!(
                            "hydrator setter `{setter}` for field `{name}` collides with the setter of field `{}`",
                            other.unraw()
                        ))
                        .with_span(ident),
                    );
                } else {
                    setters.push((setter, ident));
                }
            }
        }
        errors.finish()
    }

    fn hydrator_tokens(&self, event: &syn::Ident, snapshot: &TokenStream) -> TokenStream {
        let ident = &self.ident;
        let vis = self
            .hydrator_vis
            .clone()
            .unwrap_or(syn::Visibility::Inherited);
        let hydrator = syn::Ident::new(&format!("{ident}Hydrator"), ident.span());
        let events_field = self
            .find_events_field()
            .and_then(|f| f.ident.as_ref())
            .expect("Not ident on #[events]");
        let entity_name = ident.to_string();

        let fields = self.hydrated_fields();
        let mut members = Vec::new();
        let mut setters = Vec::new();
        let mut assignments = Vec::new();
        for field in fields {
            let name = field.ident.as_ref().expect("named field");
            let ty = &field.ty;
            let field_name = name.unraw().to_string();
            if let Some(inner) = field.option_inner_type() {
                let maybe = syn::Ident::new(&format!("maybe_{field_name}"), name.span());
                members.push(quote! { #name: #ty });
                setters.push(quote! {
                    #vis fn #name(&mut self, #name: #inner) -> &mut Self {
                        self.#name = Some(#name);
                        self
                    }
                    #vis fn #maybe(&mut self, #name: #ty) -> &mut Self {
                        self.#name = #name;
                        self
                    }
                });
                assignments.push(quote! { #name: self.#name });
            } else {
                members.push(quote! { #name: Option<#ty> });
                setters.push(quote! {
                    #vis fn #name(&mut self, #name: #ty) -> &mut Self {
                        self.#name = Some(#name);
                        self
                    }
                });
                if field.defaults_when_unset() {
                    assignments.push(quote! { #name: self.#name.unwrap_or_default() });
                } else {
                    assignments.push(quote! {
                        #name: self.#name.ok_or(es_entity::EntityHydrationError::MissingField {
                            entity: #entity_name,
                            field: #field_name,
                        })?
                    });
                }
            }
        }

        let hydrator_doc =
            format!("Accumulates the fields of a [`{ident}`] while its events are replayed.");
        let constructor_doc = format!("Starts an empty [`{hydrator}`].");

        quote! {
            #[doc = #hydrator_doc]
            #[derive(Default)]
            #[allow(dead_code)]
            #vis struct #hydrator {
                #(#members,)*
            }

            #[allow(dead_code)]
            impl #ident {
                #[doc = #constructor_doc]
                #vis fn hydrator() -> #hydrator {
                    #hydrator::default()
                }
            }

            #[allow(dead_code)]
            impl #hydrator {
                #(#setters)*

                /// Builds the entity, failing with `MissingField` for the first required
                /// field no event has set.
                #vis fn build(
                    self,
                    events: es_entity::EntityEvents<#event, #snapshot>,
                ) -> Result<#ident, es_entity::EntityHydrationError> {
                    Ok(#ident {
                        #(#assignments,)*
                        #events_field: events,
                    })
                }
            }
        }
    }
}

pub fn derive(ast: syn::DeriveInput) -> darling::Result<proc_macro2::TokenStream> {
    let entity = EsEntity::from_derive_input(&ast)?;
    entity.validate_hydrator()?;
    Ok(quote!(#entity))
}

impl ToTokens for EsEntity {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let ident = &self.ident;
        let events_field_def = self
            .find_events_field()
            .expect("Struct must have a field marked with #[es_entity(events)]");
        let events_field = events_field_def
            .ident
            .as_ref()
            .expect("Not ident on #[events]");
        let snapshot = events_field_def.snapshot_type();

        let event = self.event_ident.clone().unwrap_or_else(|| {
            syn::Ident::new(
                &format!("{}Event", self.ident),
                proc_macro2::Span::call_site(),
            )
        });
        let new = self.new_entity_ident.clone().unwrap_or_else(|| {
            syn::Ident::new(
                &format!("New{}", self.ident),
                proc_macro2::Span::call_site(),
            )
        });

        let nested = self.nested_fields().into_iter().map(|f| {
            let field = &f.ident;
            let ty = f.extract_nested_entity_type();
            quote! {
                impl es_entity::Parent<#ty> for #ident {
                    fn new_children_mut(&mut self) -> &mut Vec<<#ty as es_entity::EsEntity>::New> {
                        self.#field.new_entities_mut()
                    }

                    fn inject_children(&mut self, children: impl IntoIterator<Item = #ty>) {
                        self.#field.load(children)
                    }

                    fn iter_persisted_children_mut(
                        &mut self
                    ) -> std::collections::hash_map::ValuesMut<'_, <<#ty as EsEntity>::Event as EsEvent>::EntityId, #ty>
                    {
                        self.#field.iter_persisted_mut()
                    }
                }
            }
        });

        let hydrator = self.hydrator_tokens(&event, &snapshot);

        tokens.append_all(quote! {
            #hydrator

            impl es_entity::EsEntity for #ident {
                type Event = #event;
                type New = #new;
                type Snapshot = #snapshot;

                fn events_mut(&mut self) -> &mut es_entity::EntityEvents<#event, #snapshot> {
                    &mut self.#events_field
                }
                fn events(&self) -> &es_entity::EntityEvents<#event, #snapshot> {
                    &self.#events_field
                }
                fn __entity_id(&self) -> &<#event as es_entity::EsEvent>::EntityId {
                    &self.id
                }
            }

            #(#nested)*
        });
    }
}

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

    #[test]
    fn test_derive_es_entity() {
        let input: syn::DeriveInput = parse_quote! {
            #[derive(EsEntity)]
            pub struct User {
                pub id: UserId,
                pub email: String,
                #[es_entity(events)]
                the_events: EntityEvents<UserEvent>
            }
        };

        let output = derive(input).unwrap();
        let expected = quote! {
            #[doc = "Accumulates the fields of a [`User`] while its events are replayed."]
            #[derive(Default)]
            #[allow(dead_code)]
            struct UserHydrator {
                id: Option<UserId>,
                email: Option<String>,
            }

            #[allow(dead_code)]
            impl User {
                #[doc = "Starts an empty [`UserHydrator`]."]
                fn hydrator() -> UserHydrator {
                    UserHydrator::default()
                }
            }

            #[allow(dead_code)]
            impl UserHydrator {
                fn id(&mut self, id: UserId) -> &mut Self {
                    self.id = Some(id);
                    self
                }
                fn email(&mut self, email: String) -> &mut Self {
                    self.email = Some(email);
                    self
                }

                /// Builds the entity, failing with `MissingField` for the first required
                /// field no event has set.
                fn build(
                    self,
                    events: es_entity::EntityEvents<UserEvent, es_entity::NoSnapshot>,
                ) -> Result<User, es_entity::EntityHydrationError> {
                    Ok(User {
                        id: self.id.ok_or(es_entity::EntityHydrationError::MissingField {
                            entity: "User",
                            field: "id",
                        })?,
                        email: self.email.ok_or(es_entity::EntityHydrationError::MissingField {
                            entity: "User",
                            field: "email",
                        })?,
                        the_events: events,
                    })
                }
            }

            impl es_entity::EsEntity for User {
                type Event = UserEvent;
                type New = NewUser;
                type Snapshot = es_entity::NoSnapshot;
                fn events_mut(&mut self) -> &mut es_entity::EntityEvents<UserEvent, es_entity::NoSnapshot> {
                    &mut self.the_events
                }
                fn events(&self) -> &es_entity::EntityEvents<UserEvent, es_entity::NoSnapshot> {
                    &self.the_events
                }
                fn __entity_id(&self) -> &<UserEvent as es_entity::EsEvent>::EntityId {
                    &self.id
                }
            }
        };

        assert_eq!(output.to_string(), expected.to_string());
    }

    #[test]
    fn test_derive_without_events_attr() {
        let input: syn::DeriveInput = parse_quote! {
            #[derive(EsEntity)]
            pub struct User {
                pub id: UserId,
                events: EntityEvents<UserEvent>
            }
        };

        let output = derive(input).unwrap();
        let expected = quote! {
            #[doc = "Accumulates the fields of a [`User`] while its events are replayed."]
            #[derive(Default)]
            #[allow(dead_code)]
            struct UserHydrator {
                id: Option<UserId>,
            }

            #[allow(dead_code)]
            impl User {
                #[doc = "Starts an empty [`UserHydrator`]."]
                fn hydrator() -> UserHydrator {
                    UserHydrator::default()
                }
            }

            #[allow(dead_code)]
            impl UserHydrator {
                fn id(&mut self, id: UserId) -> &mut Self {
                    self.id = Some(id);
                    self
                }

                /// Builds the entity, failing with `MissingField` for the first required
                /// field no event has set.
                fn build(
                    self,
                    events: es_entity::EntityEvents<UserEvent, es_entity::NoSnapshot>,
                ) -> Result<User, es_entity::EntityHydrationError> {
                    Ok(User {
                        id: self.id.ok_or(es_entity::EntityHydrationError::MissingField {
                            entity: "User",
                            field: "id",
                        })?,
                        events: events,
                    })
                }
            }

            impl es_entity::EsEntity for User {
                type Event = UserEvent;
                type New = NewUser;
                type Snapshot = es_entity::NoSnapshot;
                fn events_mut(&mut self) -> &mut es_entity::EntityEvents<UserEvent, es_entity::NoSnapshot> {
                    &mut self.events
                }
                fn events(&self) -> &es_entity::EntityEvents<UserEvent, es_entity::NoSnapshot> {
                    &self.events
                }
                fn __entity_id(&self) -> &<UserEvent as es_entity::EsEvent>::EntityId {
                    &self.id
                }
            }
        };

        assert_eq!(output.to_string(), expected.to_string());
    }

    #[test]
    fn test_derive_with_nested() {
        let input: syn::DeriveInput = parse_quote! {
            #[derive(EsEntity)]
            pub struct User {
                pub id: UserId,
                #[es_entity(nested)]
                children: Nested<ChildEntity>,
                events: EntityEvents<UserEvent>
            }
        };

        let output = derive(input).unwrap();
        let expected = quote! {
            #[doc = "Accumulates the fields of a [`User`] while its events are replayed."]
            #[derive(Default)]
            #[allow(dead_code)]
            struct UserHydrator {
                id: Option<UserId>,
                children: Option<Nested<ChildEntity> >,
            }

            #[allow(dead_code)]
            impl User {
                #[doc = "Starts an empty [`UserHydrator`]."]
                fn hydrator() -> UserHydrator {
                    UserHydrator::default()
                }
            }

            #[allow(dead_code)]
            impl UserHydrator {
                fn id(&mut self, id: UserId) -> &mut Self {
                    self.id = Some(id);
                    self
                }
                fn children(&mut self, children: Nested<ChildEntity>) -> &mut Self {
                    self.children = Some(children);
                    self
                }

                /// Builds the entity, failing with `MissingField` for the first required
                /// field no event has set.
                fn build(
                    self,
                    events: es_entity::EntityEvents<UserEvent, es_entity::NoSnapshot>,
                ) -> Result<User, es_entity::EntityHydrationError> {
                    Ok(User {
                        id: self.id.ok_or(es_entity::EntityHydrationError::MissingField {
                            entity: "User",
                            field: "id",
                        })?,
                        children: self.children.unwrap_or_default(),
                        events: events,
                    })
                }
            }

            impl es_entity::EsEntity for User {
                type Event = UserEvent;
                type New = NewUser;
                type Snapshot = es_entity::NoSnapshot;
                fn events_mut(&mut self) -> &mut es_entity::EntityEvents<UserEvent, es_entity::NoSnapshot> {
                    &mut self.events
                }
                fn events(&self) -> &es_entity::EntityEvents<UserEvent, es_entity::NoSnapshot> {
                    &self.events
                }
                fn __entity_id(&self) -> &<UserEvent as es_entity::EsEvent>::EntityId {
                    &self.id
                }
            }

            impl es_entity::Parent<ChildEntity> for User {
                fn new_children_mut(&mut self) -> &mut Vec<<ChildEntity as es_entity::EsEntity>::New> {
                    self.children.new_entities_mut()
                }

                fn inject_children(&mut self, children: impl IntoIterator<Item = ChildEntity>) {
                    self.children.load(children)
                }

                fn iter_persisted_children_mut(
                    &mut self
                ) -> std::collections::hash_map::ValuesMut<'_, <<ChildEntity as EsEntity>::Event as EsEvent>::EntityId, ChildEntity>
                {
                    self.children.iter_persisted_mut()
                }
            }
        };

        assert_eq!(output.to_string(), expected.to_string());
    }

    fn hydrator_section(input: syn::DeriveInput) -> String {
        let output = derive(input).unwrap().to_string();
        let end = output
            .find("impl es_entity :: EsEntity")
            .expect("EsEntity impl is emitted");
        output[..end].to_string()
    }

    #[test]
    fn test_hydrator_option_field_gets_two_setters_and_defaults_to_none() {
        let section = hydrator_section(parse_quote! {
            #[derive(EsEntity)]
            pub struct User {
                pub id: UserId,
                pub email: Option<String>,
                events: EntityEvents<UserEvent>
            }
        });

        let expected_members = quote! { email: Option<String>, }.to_string();
        let expected_setters = quote! {
            fn email(&mut self, email: String) -> &mut Self {
                self.email = Some(email);
                self
            }
            fn maybe_email(&mut self, email: Option<String>) -> &mut Self {
                self.email = email;
                self
            }
        }
        .to_string();
        let expected_assignment = quote! { email: self.email, }.to_string();
        assert!(section.contains(&expected_members), "{section}");
        assert!(section.contains(&expected_setters), "{section}");
        assert!(section.contains(&expected_assignment), "{section}");
    }

    #[test]
    fn test_hydrator_default_field_falls_back_to_default() {
        let section = hydrator_section(parse_quote! {
            #[derive(EsEntity)]
            pub struct User {
                pub id: UserId,
                #[es_entity(default)]
                pub tags: Vec<Tag>,
                events: EntityEvents<UserEvent>
            }
        });

        let expected_members = quote! { tags: Option<Vec<Tag> >, }.to_string();
        let expected_setter = quote! {
            fn tags(&mut self, tags: Vec<Tag>) -> &mut Self {
                self.tags = Some(tags);
                self
            }
        }
        .to_string();
        let expected_assignment = quote! { tags: self.tags.unwrap_or_default(), }.to_string();
        assert!(section.contains(&expected_members), "{section}");
        assert!(section.contains(&expected_setter), "{section}");
        assert!(section.contains(&expected_assignment), "{section}");
    }

    #[test]
    fn test_hydrator_is_private_regardless_of_entity_visibility() {
        let section = hydrator_section(parse_quote! {
            #[derive(EsEntity)]
            pub struct User {
                pub id: UserId,
                events: EntityEvents<UserEvent>
            }
        });

        let expected_struct = quote! { struct UserHydrator { id: Option<UserId>, } }.to_string();
        let expected_constructor = quote! { fn hydrator() -> UserHydrator }.to_string();
        let expected_setter = quote! { fn id(&mut self, id: UserId) -> &mut Self }.to_string();
        assert!(section.contains(&expected_struct), "{section}");
        assert!(section.contains(&expected_constructor), "{section}");
        assert!(section.contains(&expected_setter), "{section}");
        assert!(section.contains("fn build (self"), "{section}");
        assert!(!section.contains("pub"), "{section}");
    }

    #[test]
    fn test_hydrator_visibility_is_overridable() {
        let section = hydrator_section(parse_quote! {
            #[derive(EsEntity)]
            #[es_entity(hydrator = "pub")]
            pub struct User {
                pub id: UserId,
                events: EntityEvents<UserEvent>
            }
        });

        let expected_struct =
            quote! { pub struct UserHydrator { id: Option<UserId>, } }.to_string();
        let expected_constructor = quote! { pub fn hydrator() -> UserHydrator }.to_string();
        let expected_setter = quote! { pub fn id(&mut self, id: UserId) -> &mut Self }.to_string();
        assert!(section.contains(&expected_struct), "{section}");
        assert!(section.contains(&expected_constructor), "{section}");
        assert!(section.contains(&expected_setter), "{section}");
        assert!(section.contains("pub fn build (self"), "{section}");

        let section = hydrator_section(parse_quote! {
            #[derive(EsEntity)]
            #[es_entity(hydrator = "pub(crate)")]
            pub struct User {
                pub id: UserId,
                events: EntityEvents<UserEvent>
            }
        });
        let expected_constructor = quote! { pub (crate) fn hydrator() -> UserHydrator }.to_string();
        assert!(section.contains(&expected_constructor), "{section}");
    }

    #[test]
    fn test_hydrator_rejects_a_field_named_build() {
        let err = derive(parse_quote! {
            #[derive(EsEntity)]
            pub struct User {
                pub id: UserId,
                pub build: String,
                events: EntityEvents<UserEvent>
            }
        })
        .unwrap_err();

        assert!(
            err.to_string()
                .contains("a field named `build` collides with the generated hydrator's `build`"),
            "{err}"
        );
    }

    #[test]
    fn test_hydrator_rejects_colliding_option_setters() {
        let err = derive(parse_quote! {
            #[derive(EsEntity)]
            pub struct User {
                pub id: UserId,
                pub email: Option<String>,
                pub maybe_email: String,
                events: EntityEvents<UserEvent>
            }
        })
        .unwrap_err();

        assert!(
            err.to_string().contains(
                "hydrator setter `maybe_email` for field `maybe_email` collides with the setter of field `email`"
            ),
            "{err}"
        );
    }
}