rocket_db_pools_codegen_community/
lib.rs

1#![recursion_limit = "256"]
2#![warn(rust_2018_idioms)]
3
4//! # `rocket_db_pool` - Code Generation
5//!
6//! Implements the code generation portion of the `rocket_db_pool` crate. This
7//! is an implementation detail. This create should never be depended on
8//! directly.
9
10#[macro_use]
11extern crate quote;
12
13mod database;
14
15/// Automatic derive for the [`Database`] trait.
16///
17/// ```rust
18/// use rocket_db_pools::Database;
19/// # type PoolType = rocket_db_pools::deadpool_postgres::Pool;
20///
21/// #[derive(Database)]
22/// #[database("database_name")]
23/// struct Db(PoolType);
24/// ```
25///
26/// The derive generates an implementation of [`Database`] as follows:
27///
28/// * [`Database::NAME`] is set to the value in the `#[database("name")]`
29///   attribute.
30///
31///   This names the database, providing an anchor to configure the database via
32///   `Rocket.toml` or any other configuration source. Specifically, the
33///   configuration in `databases.name` is used to configure the driver.
34///
35/// * [`Database::Pool`] is set to the wrapped type: `PoolType` above. The type
36///   must implement [`Pool`].
37///
38/// To meet the required [`Database`] supertrait bounds, this derive also
39/// generates implementations for:
40///
41/// * `From<Db::Pool>`
42///
43/// * `Deref<Target = Db::Pool>`
44///
45/// * `DerefMut<Target = Db::Pool>`
46///
47/// * `FromRequest<'_> for &Db`
48///
49/// * `Sentinel for &Db`
50///
51/// The `Deref` impls enable accessing the database pool directly from
52/// references `&Db` or `&mut Db`. To force a dereference to the underlying
53/// type, use `&db.0` or `&**db` or their `&mut` variants.
54///
55/// [`Database`]: ../rocket_db_pools/trait.Database.html
56/// [`Database::NAME`]: ../rocket_db_pools/trait.Database.html#associatedconstant.NAME
57/// [`Database::Pool`]: ../rocket_db_pools/trait.Database.html#associatedtype.Pool
58/// [`Pool`]: ../rocket_db_pools/trait.Pool.html
59#[proc_macro_derive(Database, attributes(database))]
60pub fn derive_database(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
61    crate::database::derive_database(input)
62}