es-entity-macros 0.10.36

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
mod columns;
mod delete;

use convert_case::{Case, Casing};
use darling::{FromDeriveInput, FromField, FromMeta};
use proc_macro2::Span;
use quote::quote;

pub use columns::*;
pub use delete::*;

#[derive(Debug, Clone)]
pub struct PostPersistHookConfig {
    pub method: syn::Ident,
    pub error: syn::Type,
}

impl FromMeta for PostPersistHookConfig {
    /// Old syntax: `post_persist_hook = "method_name"` → defaults error to `sqlx::Error`
    fn from_string(value: &str) -> darling::Result<Self> {
        Ok(PostPersistHookConfig {
            method: syn::Ident::new(value, Span::call_site()),
            error: syn::parse_str("sqlx::Error")
                .map_err(|e| darling::Error::custom(format!("invalid error type: {e}")))?,
        })
    }

    /// New syntax: `post_persist_hook(method = "...", error = "...")`
    /// `error` defaults to `sqlx::Error` if omitted
    fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
        let mut method: Option<syn::Ident> = None;
        let mut error: Option<syn::Type> = None;

        for item in items {
            if let darling::ast::NestedMeta::Meta(syn::Meta::NameValue(nv)) = item {
                if nv.path.is_ident("method")
                    && let syn::Expr::Lit(syn::ExprLit {
                        lit: syn::Lit::Str(s),
                        ..
                    }) = &nv.value
                {
                    method = Some(syn::Ident::new(&s.value(), s.span()));
                } else if nv.path.is_ident("error")
                    && let syn::Expr::Lit(syn::ExprLit {
                        lit: syn::Lit::Str(s),
                        ..
                    }) = &nv.value
                {
                    error =
                        Some(syn::parse_str(&s.value()).map_err(|e| {
                            darling::Error::custom(format!("invalid error type: {e}"))
                        })?);
                }
            }
        }

        let error = error
            .unwrap_or_else(|| syn::parse_str("sqlx::Error").expect("sqlx::Error is a valid type"));

        Ok(PostPersistHookConfig {
            method: method
                .ok_or_else(|| darling::Error::custom("missing `method` in post_persist_hook"))?,
            error,
        })
    }
}

#[derive(Debug, Clone)]
pub struct PostHydrateHookConfig {
    pub method: syn::Ident,
    pub error: syn::Type,
}

impl FromMeta for PostHydrateHookConfig {
    fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
        let mut method: Option<syn::Ident> = None;
        let mut error: Option<syn::Type> = None;

        for item in items {
            if let darling::ast::NestedMeta::Meta(syn::Meta::NameValue(nv)) = item {
                if nv.path.is_ident("method") {
                    if let syn::Expr::Lit(syn::ExprLit {
                        lit: syn::Lit::Str(s),
                        ..
                    }) = &nv.value
                    {
                        method = Some(syn::Ident::new(&s.value(), s.span()));
                    }
                } else if nv.path.is_ident("error")
                    && let syn::Expr::Lit(syn::ExprLit {
                        lit: syn::Lit::Str(s),
                        ..
                    }) = &nv.value
                {
                    error =
                        Some(syn::parse_str(&s.value()).map_err(|e| {
                            darling::Error::custom(format!("invalid error type: {e}"))
                        })?);
                }
            }
        }

        Ok(PostHydrateHookConfig {
            method: method
                .ok_or_else(|| darling::Error::custom("missing `method` in post_hydrate_hook"))?,
            error: error
                .ok_or_else(|| darling::Error::custom("missing `error` in post_hydrate_hook"))?,
        })
    }
}

/// Information about the clock field in a repository
#[derive(Debug, Clone)]
pub enum ClockFieldInfo<'a> {
    /// No clock field present
    None,
    /// Clock field is `Option<ClockHandle>` - use if Some, fallback to global
    Optional(&'a syn::Ident),
    /// Clock field is `ClockHandle` - always use it
    Required(&'a syn::Ident),
}

#[derive(FromField)]
#[darling(attributes(es_repo))]
pub struct RepoField {
    pub ident: Option<syn::Ident>,
    pub ty: syn::Type,
    #[darling(default)]
    pub pool: bool,
    #[darling(default)]
    pub clock: bool,
    #[darling(default)]
    pub nested: bool,
    /// For nested fields whose repo type is generic, specify the child entity name
    /// so error types can be referenced concretely (e.g., `entity = "InterestAccrualCycle"`
    /// generates `InterestAccrualCycleCreateError` instead of
    /// `<InterestAccrualRepo<Evt> as EsRepo>::CreateError`).
    #[darling(default)]
    pub entity: Option<syn::Ident>,
}

impl RepoField {
    pub fn ident(&self) -> &syn::Ident {
        self.ident.as_ref().expect("Field must have an identifier")
    }

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

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

    /// Check if the field type is `Option<...>`
    fn is_option_type(&self) -> bool {
        if let syn::Type::Path(type_path) = &self.ty
            && let Some(segment) = type_path.path.segments.last()
        {
            return segment.ident == "Option";
        }
        false
    }

    pub fn create_nested_fn_name(&self) -> syn::Ident {
        syn::Ident::new(
            &format!("create_nested_{}_in_op", self.ident()),
            proc_macro2::Span::call_site(),
        )
    }

    pub fn update_nested_fn_name(&self) -> syn::Ident {
        syn::Ident::new(
            &format!("update_nested_{}_in_op", self.ident()),
            proc_macro2::Span::call_site(),
        )
    }

    pub fn find_nested_fn_name(&self) -> syn::Ident {
        syn::Ident::new(
            &format!("find_nested_{}_in_op", self.ident()),
            proc_macro2::Span::call_site(),
        )
    }

    pub fn find_nested_include_deleted_fn_name(&self) -> syn::Ident {
        syn::Ident::new(
            &format!("find_nested_{}_include_deleted_in_op", self.ident()),
            proc_macro2::Span::call_site(),
        )
    }

    pub fn delete_nested_fn_name(&self) -> syn::Ident {
        syn::Ident::new(
            &format!("delete_nested_{}_in_op", self.ident()),
            proc_macro2::Span::call_site(),
        )
    }

    /// PascalCase variant name derived from field name (e.g. `line_items` -> `LineItems`)
    pub fn nested_variant_name(&self) -> syn::Ident {
        syn::Ident::new(
            &self.ident().to_string().to_case(Case::UpperCamel),
            Span::call_site(),
        )
    }
}

#[derive(FromDeriveInput)]
#[darling(attributes(es_repo), map = "Self::update_defaults")]
pub struct RepositoryOptions {
    pub ident: syn::Ident,
    pub generics: syn::Generics,
    #[darling(default)]
    pub columns: Columns,
    #[darling(default)]
    pub post_persist_hook: Option<PostPersistHookConfig>,
    #[darling(default)]
    pub post_hydrate_hook: Option<PostHydrateHookConfig>,
    #[darling(default)]
    pub delete: DeleteOption,

    data: darling::ast::Data<(), RepoField>,

    #[darling(rename = "entity")]
    entity_ident: syn::Ident,
    #[darling(default, rename = "event")]
    event_ident: Option<syn::Ident>,
    #[darling(default, rename = "id")]
    id_ty: Option<syn::Ident>,
    #[darling(default, rename = "tbl_prefix")]
    prefix: Option<syn::LitStr>,
    #[darling(default, rename = "tbl")]
    table_name: Option<String>,
    #[darling(default, rename = "events_tbl")]
    events_table_name: Option<String>,

    #[darling(default)]
    persist_event_context: Option<bool>,
}

impl RepositoryOptions {
    fn update_defaults(mut self) -> Self {
        let entity_name = self.entity_ident.to_string();
        if self.event_ident.is_none() {
            self.event_ident = Some(syn::Ident::new(
                &format!("{entity_name}Event"),
                proc_macro2::Span::call_site(),
            ));
        }
        if self.id_ty.is_none() {
            self.id_ty = Some(syn::Ident::new(
                &format!("{entity_name}Id"),
                proc_macro2::Span::call_site(),
            ));
        }
        let prefix = if let Some(prefix) = &self.prefix {
            format!("{}_", prefix.value())
        } else {
            String::new()
        };
        if self.table_name.is_none() {
            self.table_name = Some(format!(
                "{prefix}{}",
                pluralizer::pluralize(&entity_name, 2, false).to_case(Case::Snake)
            ));
        }
        if self.events_table_name.is_none() {
            self.events_table_name =
                Some(format!("{prefix}{entity_name}Events").to_case(Case::Snake));
        }

        self.columns
            .set_id_column(self.id_ty.as_ref().expect("Id not set"));

        self
    }

    pub fn entity(&self) -> &syn::Ident {
        &self.entity_ident
    }

    pub fn table_name(&self) -> &str {
        self.table_name.as_ref().expect("Table name is not set")
    }

    pub fn table_prefix(&self) -> Option<&syn::LitStr> {
        self.prefix.as_ref()
    }

    pub fn id(&self) -> &syn::Ident {
        self.id_ty.as_ref().expect("ID identifier is not set")
    }

    pub fn event(&self) -> &syn::Ident {
        self.event_ident
            .as_ref()
            .expect("Event identifier is not set")
    }

    pub fn event_context_enabled(&self) -> bool {
        #[cfg(feature = "event-context-enabled")]
        {
            self.persist_event_context.unwrap_or(true)
        }
        #[cfg(not(feature = "event-context-enabled"))]
        {
            self.persist_event_context.unwrap_or(false)
        }
    }

    pub fn events_table_name(&self) -> &str {
        self.events_table_name
            .as_ref()
            .expect("Events table name is not set")
    }

    pub fn cursor_mod(&self) -> syn::Ident {
        let name = format!("{}Cursor", self.entity_ident).to_case(Case::Snake);
        syn::Ident::new(&name, proc_macro2::Span::call_site())
    }

    pub fn repo_types_mod(&self) -> syn::Ident {
        let name = format!("{}RepoTypes", self.entity_ident).to_case(Case::Snake);
        syn::Ident::new(&name, proc_macro2::Span::call_site())
    }

    #[cfg(feature = "instrument")]
    pub fn repo_name_snake_case(&self) -> String {
        self.ident.to_string().to_case(Case::Snake)
    }

    pub fn pool_field(&self) -> &syn::Ident {
        let field = match &self.data {
            darling::ast::Data::Struct(fields) => fields.iter().find_map(|field| {
                if field.is_pool_field() {
                    Some(field.ident.as_ref().unwrap())
                } else {
                    None
                }
            }),
            _ => None,
        };
        field.expect("Repo must have a field named 'pool' or marked with #[es_repo(pool)]")
    }

    pub fn clock_field(&self) -> ClockFieldInfo<'_> {
        match &self.data {
            darling::ast::Data::Struct(fields) => {
                for field in fields.iter() {
                    if field.is_clock_field() {
                        let ident = field.ident.as_ref().unwrap();
                        return if field.is_option_type() {
                            ClockFieldInfo::Optional(ident)
                        } else {
                            ClockFieldInfo::Required(ident)
                        };
                    }
                }
                ClockFieldInfo::None
            }
            _ => ClockFieldInfo::None,
        }
    }

    pub fn any_nested(&self) -> bool {
        if let darling::ast::Data::Struct(fields) = &self.data {
            fields.iter().any(|f| f.nested)
        } else {
            panic!("Repository must be a struct")
        }
    }

    pub fn all_nested(&self) -> impl Iterator<Item = &RepoField> {
        if let darling::ast::Data::Struct(fields) = &self.data {
            fields.iter().filter(|f| f.nested)
        } else {
            panic!("Repository must be a struct")
        }
    }

    pub fn query_fn_generics(nested: bool) -> proc_macro2::TokenStream {
        if nested {
            quote! {
                <OP>
            }
        } else {
            quote! {
                <'a, OP>
            }
        }
    }

    pub fn query_fn_op_arg(nested: bool) -> proc_macro2::TokenStream {
        if nested {
            quote! {
                op: &mut OP
            }
        } else {
            quote! {
                op: OP
            }
        }
    }

    pub fn query_fn_op_traits(nested: bool) -> proc_macro2::TokenStream {
        if nested {
            quote! {
                es_entity::AtomicOperation
            }
        } else {
            quote! {
                es_entity::IntoOneTimeExecutor<'a>
            }
        }
    }

    pub fn create_error(&self) -> syn::Ident {
        syn::Ident::new(
            &format!("{}CreateError", self.entity_ident),
            Span::call_site(),
        )
    }

    pub fn modify_error(&self) -> syn::Ident {
        syn::Ident::new(
            &format!("{}ModifyError", self.entity_ident),
            Span::call_site(),
        )
    }

    pub fn find_error(&self) -> syn::Ident {
        syn::Ident::new(
            &format!("{}FindError", self.entity_ident),
            Span::call_site(),
        )
    }

    pub fn query_error(&self) -> syn::Ident {
        syn::Ident::new(
            &format!("{}QueryError", self.entity_ident),
            Span::call_site(),
        )
    }

    pub fn column_enum(&self) -> syn::Ident {
        syn::Ident::new(&format!("{}Column", self.entity_ident), Span::call_site())
    }

    pub fn query_fn_get_op(nested: bool) -> proc_macro2::TokenStream {
        if nested {
            quote! {
                &mut self.pool().begin().await?
            }
        } else {
            quote! {
                self.pool()
            }
        }
    }
}