Skip to main content

rust_ef_macros/
lib.rs

1//! Procedural macros for Rust Entity Framework (rust-ef).
2
3mod column_macro;
4mod entity;
5mod entity_config;
6mod linq;
7
8use proc_macro::TokenStream;
9
10#[proc_macro_derive(
11    EntityType,
12    attributes(
13        table,
14        primary_key,
15        auto_increment,
16        sequence,
17        required,
18        max_length,
19        column,
20        foreign_key,
21        navigation,
22        not_mapped,
23        index,
24        unique,
25        through,
26        concurrency_check,
27        on_delete,
28        context
29    )
30)]
31pub fn derive_entity_type(input: TokenStream) -> TokenStream {
32    entity::expand_entity_type(input)
33}
34
35#[proc_macro]
36pub fn column(input: TokenStream) -> TokenStream {
37    column_macro::expand_column(input)
38}
39
40/// Compile-time LINQ-to-SQL.
41///
42/// ```ignore
43/// linq!(ctx.set::<Blog>(), |b: Blog| b.rating > 5).to_list().await?;
44///
45/// let expr = linq!(|b: Blog| b.rating > min);
46/// ctx.set::<Blog>().filter(expr).to_list().await?;
47/// ```
48#[proc_macro]
49pub fn linq(input: TokenStream) -> TokenStream {
50    linq::expand_linq(input)
51}
52
53/// Attribute macro for `impl IEntityTypeConfiguration<T>` blocks.
54///
55/// Emits an `inventory::submit!` registering the configuration for automatic
56/// discovery by `DbContext::from_options()`. The first argument is the entity
57/// type `T`; the config type is taken from the `impl`'s `Self` type. An
58/// optional second argument specifies the DbContext key for multi-database
59/// scenarios.
60///
61/// # Examples
62///
63/// ```ignore
64/// #[derive(Default)]
65/// pub struct BlogConfig;
66///
67/// // Default context
68/// #[entity(Blog)]
69/// impl IEntityTypeConfiguration<Blog> for BlogConfig {
70///     fn configure(&self, entity: &mut EntityTypeBuilder<'_, Blog>) {
71///         entity.to_table("blogs_renamed");
72///     }
73/// }
74///
75/// // Keyed context ("logs" database)
76/// #[entity(LogEntry, "logs")]
77/// impl LogEntryConfig for IEntityTypeConfiguration<LogEntry> {
78///     fn configure(&self, entity: &mut EntityTypeBuilder<'_, LogEntry>) {
79///         entity.to_table("app_logs");
80///     }
81/// }
82/// ```
83#[proc_macro_attribute]
84pub fn entity(args: TokenStream, input: TokenStream) -> TokenStream {
85    entity_config::expand_entity_config(args, input)
86}