Rocket/contrib/sync_db_pools/codegen/src/database.rs

131 lines
4.8 KiB
Rust
Raw Normal View History

use proc_macro::TokenStream;
2019-09-21 20:36:57 +00:00
use devise::{Spanned, Result, ext::SpanDiagnosticExt};
use crate::syn;
#[derive(Debug)]
struct DatabaseInvocation {
/// The attributes on the attributed structure.
attrs: Vec<syn::Attribute>,
/// The name of the structure on which `#[database(..)] struct This(..)` was invoked.
type_name: syn::Ident,
/// The visibility of the structure on which `#[database(..)] struct This(..)` was invoked.
visibility: syn::Visibility,
/// The database name as passed in via #[database('database name')].
db_name: String,
/// The type inside the structure: struct MyDb(ThisType).
connection_type: syn::Type,
}
const EXAMPLE: &str = "example: `struct MyDatabase(diesel::SqliteConnection);`";
const ONLY_ON_STRUCTS_MSG: &str = "`database` attribute can only be used on structs";
const ONLY_UNNAMED_FIELDS: &str = "`database` attribute can only be applied to \
structs with exactly one unnamed field";
2018-12-12 08:00:10 +00:00
const NO_GENERIC_STRUCTS: &str = "`database` attribute cannot be applied to structs \
with generics";
fn parse_invocation(attr: TokenStream, input: TokenStream) -> Result<DatabaseInvocation> {
2019-06-13 02:17:59 +00:00
let attr_stream2 = crate::proc_macro2::TokenStream::from(attr);
let string_lit = crate::syn::parse2::<syn::LitStr>(attr_stream2)?;
let input = crate::syn::parse::<syn::DeriveInput>(input).unwrap();
if !input.generics.params.is_empty() {
2018-12-12 08:00:10 +00:00
return Err(input.generics.span().error(NO_GENERIC_STRUCTS));
}
let structure = match input.data {
syn::Data::Struct(s) => s,
_ => return Err(input.span().error(ONLY_ON_STRUCTS_MSG))
};
let inner_type = match structure.fields {
syn::Fields::Unnamed(ref fields) if fields.unnamed.len() == 1 => {
let first = fields.unnamed.first().expect("checked length");
first.ty.clone()
}
_ => return Err(structure.fields.span().error(ONLY_UNNAMED_FIELDS).help(EXAMPLE))
};
Ok(DatabaseInvocation {
attrs: input.attrs,
type_name: input.ident,
visibility: input.vis,
db_name: string_lit.value(),
connection_type: inner_type,
})
}
2018-12-12 08:00:10 +00:00
#[allow(non_snake_case)]
pub fn database_attr(attr: TokenStream, input: TokenStream) -> Result<TokenStream> {
let invocation = parse_invocation(attr, input)?;
// Store everything we're going to need to generate code.
2018-12-12 08:00:10 +00:00
let conn_type = &invocation.connection_type;
let name = &invocation.db_name;
let attrs = &invocation.attrs;
2018-12-12 08:00:10 +00:00
let guard_type = &invocation.type_name;
let vis = &invocation.visibility;
let fairing_name = format!("'{}' Database Pool", name);
2021-06-30 20:36:23 +00:00
let span = conn_type.span();
// A few useful paths.
let root = quote_spanned!(span => ::rocket_sync_db_pools);
let rocket = quote!(#root::rocket);
Revamp configuration. This commit completely overhauls Rocket's configuration systems, basing it on the new Figment library. It includes many breaking changes pertaining to configuration. They are: * "Environments" are replaced by "profiles". * 'ROCKET_PROFILE' takes the place of 'ROCKET_ENV'. * Profile names are now arbitrary, but 'debug' and 'release' are given special treatment as default profiles for the debug and release compilation profiles. * A 'default' profile now sits along-side the meta 'global' profile. * The concept of "extras" is no longer present; users can extract any values they want from the configured 'Figment'. * The 'Poolable' trait takes an '&Config'. * The 'secrets' feature is disabled by default. * It is a hard error if 'secrets' is enabled under the 'release' profile and no 'secret_key' is configured. * 'ConfigBuilder' no longer exists: all fields of 'Config' are public with public constructors for each type. * 'keep_alive' is disabled with '0', not 'false' or 'off'. * Inlined error variants into the 'Error' structure. * 'LoggingLevel' is now 'LogLevel'. * Limits can now be specified in SI units: "1 MiB". The summary of other changes are: * The default config file can be configured with 'ROCKET_CONFIG'. * HTTP/1 and HTTP/2 keep-alive configuration is restored. * 'ctrlc' is now a recognized config option. * 'serde' is now a core dependency. * TLS misconfiguration errors are improved. * Several example use '_' as the return type of '#[launch]' fns. * 'AdHoc::config()' was added for simple config extraction. * Added more documentation for using 'Limits'. * Launch information is no longer treated specially. * The configuration guide was rewritten. Resolves #852. Resolves #209. Closes #1404. Closes #652.
2020-09-03 05:41:31 +00:00
let request_guard_type = quote_spanned! { span =>
#(#attrs)* #vis struct #guard_type(#[allow(dead_code)] #root::Connection<Self, #conn_type>);
2018-12-12 08:00:10 +00:00
};
let pool = quote_spanned!(span => #root::ConnectionPool<Self, #conn_type>);
let conn = quote_spanned!(span => #root::Connection<Self, #conn_type>);
Ok(quote_spanned! { span =>
Revamp configuration. This commit completely overhauls Rocket's configuration systems, basing it on the new Figment library. It includes many breaking changes pertaining to configuration. They are: * "Environments" are replaced by "profiles". * 'ROCKET_PROFILE' takes the place of 'ROCKET_ENV'. * Profile names are now arbitrary, but 'debug' and 'release' are given special treatment as default profiles for the debug and release compilation profiles. * A 'default' profile now sits along-side the meta 'global' profile. * The concept of "extras" is no longer present; users can extract any values they want from the configured 'Figment'. * The 'Poolable' trait takes an '&Config'. * The 'secrets' feature is disabled by default. * It is a hard error if 'secrets' is enabled under the 'release' profile and no 'secret_key' is configured. * 'ConfigBuilder' no longer exists: all fields of 'Config' are public with public constructors for each type. * 'keep_alive' is disabled with '0', not 'false' or 'off'. * Inlined error variants into the 'Error' structure. * 'LoggingLevel' is now 'LogLevel'. * Limits can now be specified in SI units: "1 MiB". The summary of other changes are: * The default config file can be configured with 'ROCKET_CONFIG'. * HTTP/1 and HTTP/2 keep-alive configuration is restored. * 'ctrlc' is now a recognized config option. * 'serde' is now a core dependency. * TLS misconfiguration errors are improved. * Several example use '_' as the return type of '#[launch]' fns. * 'AdHoc::config()' was added for simple config extraction. * Added more documentation for using 'Limits'. * Launch information is no longer treated specially. * The configuration guide was rewritten. Resolves #852. Resolves #209. Closes #1404. Closes #652.
2020-09-03 05:41:31 +00:00
#request_guard_type
#[allow(dead_code)]
2018-12-12 08:00:10 +00:00
impl #guard_type {
/// Returns a fairing that initializes the database connection pool.
pub fn fairing() -> impl #rocket::fairing::Fairing {
<#pool>::fairing(#fairing_name, #name)
}
/// Returns an opaque type that represents the connection pool
/// backing connections of type `Self`.
pub fn pool<P: #rocket::Phase>(__rocket: &#rocket::Rocket<P>) -> Option<&#pool> {
<#pool>::pool(&__rocket)
}
/// Runs the provided function `__f` in an async-safe blocking
/// thread.
pub async fn run<F, R>(&self, __f: F) -> R
where F: FnOnce(&mut #conn_type) -> R + Send + 'static,
R: Send + 'static,
{
self.0.run(__f).await
}
/// Retrieves a connection of type `Self` from the `rocket` instance.
pub async fn get_one<P: #rocket::Phase>(__rocket: &#rocket::Rocket<P>) -> Option<Self> {
<#pool>::get_one(&__rocket).await.map(Self)
}
}
#[#rocket::async_trait]
impl<'r> #rocket::request::FromRequest<'r> for #guard_type {
type Error = ();
async fn from_request(
__r: &'r #rocket::request::Request<'_>
) -> #rocket::request::Outcome<Self, ()> {
<#conn>::from_request(__r).await.map(Self)
}
}
impl #rocket::Sentinel for #guard_type {
fn abort(__r: &#rocket::Rocket<#rocket::Ignite>) -> bool {
<#conn>::abort(__r)
}
}
}.into())
}