rustorm_macros/lib.rs
1use proc_macro::TokenStream;
2use syn::{parse_macro_input, DeriveInput};
3
4mod derive_model;
5mod derive_validate;
6
7/// Главный derive-макрос. Генерирует:
8/// - `impl Model for T`
9/// - `struct NewT { ... }` (без PK и auto-полей)
10/// - Методы `find`, `create`, `save`, `delete`, `all`, `query`, ...
11/// - Структуру `TColumns` для DSL-фильтров
12/// - `impl HasColumns for T`
13///
14/// # Пример
15/// ```rust
16/// #[derive(Model, Debug, Clone, Serialize, Deserialize)]
17/// #[orm(table = "users")]
18/// pub struct User {
19/// #[orm(primary_key, auto_increment)]
20/// pub id: i64,
21///
22/// #[orm(unique, index)]
23/// pub email: Email,
24///
25/// pub username: String,
26///
27/// #[orm(default = true)]
28/// pub is_active: bool,
29///
30/// #[orm(auto_now_add)]
31/// pub created_at: DateTime<Utc>,
32///
33/// #[orm(auto_now)]
34/// pub updated_at: DateTime<Utc>,
35/// }
36/// ```
37#[proc_macro_derive(Model, attributes(orm))]
38pub fn derive_model(input: TokenStream) -> TokenStream {
39 let input = parse_macro_input!(input as DeriveInput);
40 derive_model::expand(input).unwrap_or_else(|e| e.to_compile_error().into())
41}
42
43/// Derive-макрос для валидации по атрибутам `#[validate(...)]`.
44///
45/// # Пример
46/// ```rust
47/// #[derive(Model, Validate)]
48/// pub struct User {
49/// #[validate(email, max_length = 255)]
50/// pub email: String,
51///
52/// #[validate(min_length = 8, max_length = 128)]
53/// pub password: String,
54/// }
55/// ```
56#[proc_macro_derive(Validate, attributes(validate))]
57pub fn derive_validate(input: TokenStream) -> TokenStream {
58 let input = parse_macro_input!(input as DeriveInput);
59 derive_validate::expand(input).unwrap_or_else(|e| e.to_compile_error().into())
60}