ormx_macros/
lib.rs

1#![cfg(any(
2    feature = "mysql",
3    feature = "postgres",
4    feature = "sqlite",
5    feature = "mariadb"
6))]
7
8mod attrs;
9mod backend;
10mod patch;
11mod table;
12mod utils;
13
14/// Derives [Table](trait.Table.html) and generates a struct for inserting rows and accessors
15/// to certain fields.
16///
17/// # Example
18/// ```rust,ignore  
19/// #[derive(ormx::Table)]
20/// #[ormx(table = "users", id = user_id, insertable)]
21/// struct User {
22///     #[ormx(column = "id")]
23///     user_id: u32,
24///     first_name: String,
25///     last_name: String,
26///     #[ormx(get_optional(&str))]
27///     email: String,
28///     #[ormx(default, set)]
29///     last_login: Option<NaiveDateTime>,
30/// }
31/// ```
32///
33/// # The ID
34/// It is required that every table contains an ID column, which uniquely
35/// identifies a row.  
36/// Probably, you would want to use an auto-incrementing integer for this.  
37/// This is a central requirement of ormx, and if your table does not fulfill this requirement, ormx
38/// is not what you are looking for.
39///
40/// # CRUD
41/// See the documentation of [Table](trait.Table.html)
42///
43/// # Insertable
44/// ormx will generate a helper struct for inserting rows into the database when using
45/// `#[ormx(insertable)]`.  
46/// This struct will contain all fields of the struct, except
47/// - the ID
48/// - fields annotated with `#[ormx(default)]`
49///
50/// since the value of these fields will be generated by the database.
51/// By default, this struct will be named `Insert{struct_name}`, though this can be changed by
52/// supplying a custom name: `#[ormx(insertable = CreateUser)]`.
53/// The generated struct can be used by [Table::insert](trait.Table.html) or
54/// [Insert::insert](trait.Insert.html).
55///
56/// # Deletable
57/// ormx will implement [Delete](trait.Delete.html) for your struct when using
58/// `#[ormx(deletable)].
59///
60/// # Accessors: Getters
61/// ormx will generate accessor functions for fields annotated with `#[ormx(get_one)]`,
62/// `#[ormx(get_optional)]` and `#[ormx(get_many)]`.
63/// These functions can be used to query a row by the value of the annotated field.
64///
65/// The generated function will have these signature:  
66/// **`#[ormx(get_one)]`**:  
67/// `{pub} async fn get_by_{field_name}(&{field_type}) -> Result<Self>`
68///
69/// **`#[ormx(get_optional)]`**:  
70/// `{pub} async fn get_by_{field_name}(&{field_type}) -> Result<Option<Self>>`
71///
72/// **`#[ormx(get_many)]`**:  
73/// `{pub} async fn get_by_{field_name}(&{field_type}) -> Result<Vec<Self>>`
74///
75/// By default, the function will be named `get_by_{field_name)`, though this can be changed by
76/// supplying a custom name: `#[ormx(get_one = by_id)]`.
77/// By default, the function will take a reference to the type of the annotated field as an argument,
78/// though this can be changed by supplying a custom type: `#[ormx(get_one(&str)]`.
79///
80/// # Accessors: Setters
81/// ormx will generate accessor functions for fields annotated with `#[ormx(set)]`.
82/// These functions can be used to update a single field of an entity.
83///
84/// The generated function will have these signature:
85/// **`#[ormx(set)]`**:
86/// `{pub} async fn set_{field_name}(&mut self, {field_type}) -> Result<Self>`
87///
88/// By default, the function will be named `set_{field_name)`, though this can be changed by
89/// supplying a custom name: `#[ormx(set = set_name)]`.
90///
91/// # Custom types
92/// When using custom types (which implement `sqlx::Type`), the field has to annotated with
93/// `#[ormx(custom_type)]`.
94/// This will use a column type override for querying this field
95/// (see [the sqlx docs on this](https://docs.rs/sqlx/0.4.0-beta.1/sqlx/macro.query_as.html#column-type-override-infer-from-struct-field)).
96#[proc_macro_error::proc_macro_error]
97#[proc_macro_derive(Table, attributes(ormx))]
98pub fn derive_table(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
99    let input = syn::parse_macro_input!(input as syn::DeriveInput);
100    match table::derive(input) {
101        Ok(ok) => ok,
102        Err(err) => err.to_compile_error(),
103    }
104    .into()
105}
106
107/// Derives [Patch](trait.Patch.html).
108#[proc_macro_error::proc_macro_error]
109#[proc_macro_derive(Patch, attributes(ormx))]
110pub fn derive_patch(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
111    let input = syn::parse_macro_input!(input as syn::DeriveInput);
112    match patch::derive(input) {
113        Ok(ok) => ok,
114        Err(err) => err.to_compile_error(),
115    }
116    .into()
117}