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