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
//! # Tardis-Macros
//!
//! Tardis-Macros is a macro support library for the Tardis framework, providing additional macros to simplify code generation for DTO (Data Transfer Object) with sea_orm.
//!
//! ## Main Macros
//!
//! - [`TardisCreateEntity`]: Generates code to create entities, combining `TardisCreateIndex` and `TardisCreateTable`.
//!
//! ## Features
//!
//! Tardis-Macros supports the following features, which enable the usage of specific macros:
//!
//! | Feature | Macro |
//! |-------------------------------------------------|-------------------------|
//! | `reldb-postgres`or`reldb-mysql`or`reldb-sqlite` | `TardisCreateTable` |
//! | `reldb-postgres`or`reldb-mysql`or`reldb-sqlite` | `TardisCreateIndex` |
//! | `reldb-postgres`or`reldb-mysql`or`reldb-sqlite` | `TardisCreateEntity` |
//! | `reldb-postgres`or`reldb-mysql`or`reldb-sqlite` | `TardisEmptyBehavior` |
//! | `reldb-postgres`or`reldb-mysql`or`reldb-sqlite` | `TardisEmptyRelation` |
//!
//!
//! Please note that the availability of each macro depends on the enabled features. Make sure to enable the corresponding feature to use the desired macro.
//!
//! ## How to Use
//!
//! ### Best Practices
//!
//! Add the `TardisCreateEntity` macro to your struct definition, along with other necessary derive macros like `DeriveEntityModel`, `TardisEmptyBehavior`, and `TardisEmptyRelation`.
//!
//! Example usage:
//!
//! ```rust ignore
//! #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, TardisCreateEntity, TardisEmptyBehavior, TardisEmptyRelation)]
//! #[sea_orm(table_name = "examples")]
//! pub struct Model {
//! #[sea_orm(primary_key, auto_increment = false)]
//! pub id: String,
//! #[index]
//! #[fill_ctx(own_paths)]
//! pub aaa: String,
//! }
//! ```
//! You also can refer to the example code and test cases for the best practices on using the Tardis-Macros library.
//!
//!
//! For more examples and detailed usage, please refer to the documentation of each specific macro.
//!
//! [TardisCreateEntity]
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
use proc_macro::TokenStream;
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
use syn::{parse_macro_input, DeriveInput};
/// # TardisCreateTable
/// Generate table creation statement, compatible with `tardis_entity`.
/// see [TardisActiveModel::create_table_statement](https://docs.rs/tardis/latest/tardis/db/reldb_client/trait.TardisActiveModel.html#method.create_table_statement).
///
/// According to tardis_entity automatically generates `tardis_create_table_statement(db: DbBackend)`
/// method, you can be directly called in the `TardisActiveModel::create_table_statement` method. \
/// example see [macros_examples::example_for_derive_create_tabled].
///
/// ## tardis_entity attribute
///
/// - `custom_len`: Custom length for the table columns. (optional)
///
/// [`sea-query::tabled::column::ColumnDef`]: https://docs.rs/sea-query/latest/sea_query/table/struct.ColumnDef.html
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
#[proc_macro_derive(TardisCreateTable, attributes(tardis_entity))]
pub fn tardis_create_table(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, attrs, .. } = parse_macro_input!(input as DeriveInput);
match tardis_create_table::create_table(ident, data, attrs) {
Ok(stream) => stream.into(),
Err(err) => err.to_compile_error().into(),
}
}
/// # TardisCreateIndex
/// Generate index creation statement, compatible with `tardis_entity`.
/// see [create_index_statement](https://docs.rs/tardis/latest/tardis/db/reldb_client/trait.TardisActiveModel.html#method.create_index_statement).
///
/// According to tardis_entity automatically generates `tardis_create_index_statement()` method,
/// you can be directly called in the `TardisActiveModel::create_index_statement` method. \
/// example see [macros_examples::example_for_derive_create_index].
///
/// ## index attribute
///
/// - `index_id`: ID of the index. (default: Random id)
/// - `name`: Name of the index. (optional)
/// - `primary`: Specifies if the index is a primary index. (default: `false`)
/// - `unique`: Specifies if the index is a unique index. (default: `false`)
/// - `full_text`: Specifies if the index is a full-text index. (default: `false`)
/// - `if_not_exists`: Specifies if the index should be created if it doesn't exist. (default: `false`)
/// - `index_type`: Type of the index. See "Index Types" section for possible values. (optional)
///
/// ### index_id parameter
///
/// Each default index will be a separate index statement. \
/// If you want to add multiple properties to a single index statement, you must set the `index_id` to the same value.
///
/// Same index_id, if there are different variable assignments, only the first one will take effect. \
/// For example,the name of the generated statement is name1 instead of name2.
/// ```ignore
/// #[index(index_id="1",name="name1")]
/// name1:String,
/// #[index(index_id="1",name="name2")]
/// name2:String,
/// ```
///
/// ### Index Types
///
/// The `index_type` value needs to be one of the following:
///
/// - BTree
/// - FullText
/// - Gin
/// - Hash
/// - Custom
///
/// Example for custom:
/// ```ignore
/// #[derive(Clone, Debug, DeriveEntityModel, TardisCreateIndex)]
/// #[sea_orm(table_name = "examples")]
/// pub struct Model {
/// #[sea_orm(primary_key)]
/// #[tardis_entity(primary_key)]
/// pub id: String,
/// #[index(index_id = "index_id_1", index_type = "Custom(Test)")]
/// pub custom_index_col: String,
/// }
///
/// struct Test;
/// impl Iden for Test {
/// todo!()
/// }
/// ```
///
///
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
#[proc_macro_derive(TardisCreateIndex, attributes(index))]
pub fn tardis_create_index(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, attrs, .. } = parse_macro_input!(input as DeriveInput);
match tardis_create_index::create_index(ident, data, attrs) {
Ok(stream) => stream.into(),
Err(err) => err.to_compile_error().into(),
}
}
/// # TardisCreateEntity
/// The functionality of `TardisCreateEntity` is equivalent to `TardisCreateIndex` combined with `TardisCreateTable`.
/// Additionally, it introduces a new attribute called fill_ctx, and automatically implements `ActiveModelBehavior`. \
/// see [TardisCreateIndex] and [TardisCreateTable]
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
#[proc_macro_derive(TardisCreateEntity, attributes(tardis_entity, index, fill_ctx))]
pub fn tardis_create_entity(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input as DeriveInput);
match tardis_create_entity::create_entity(ident, data) {
Ok(stream) => stream.into(),
Err(err) => err.to_compile_error().into(),
}
}
/// # TardisEmptyBehavior
/// Generates an empty implementation of `ActiveModelBehavior` for `ActiveModel`.
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
#[proc_macro_derive(TardisEmptyBehavior)]
pub fn tardis_empty_behavior(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input as DeriveInput);
match tardis_empty_impl::create_empty_behavior(ident, data) {
Ok(stream) => stream.into(),
Err(err) => err.to_compile_error().into(),
}
}
/// #TardisEmptyRelation
/// Generates an empty `Relation`.
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
#[proc_macro_derive(TardisEmptyRelation)]
pub fn tardis_empty_relation(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input as DeriveInput);
match tardis_empty_impl::create_empty_relation(ident, data) {
Ok(stream) => stream.into(),
Err(err) => err.to_compile_error().into(),
}
}
#[allow(dead_code)]
pub(crate) mod macro_helpers;
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
mod tardis_create_entity;
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
mod tardis_create_index;
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
mod tardis_create_table;
#[cfg(any(feature = "reldb-postgres", feature = "reldb-mysql", feature = "reldb-sqlite"))]
mod tardis_empty_impl;