2021-05-31 16:15:00 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
|
|
|
|
use devise::{DeriveGenerator, FromMeta, MapperBuild, Support, ValidatorBuild};
|
|
|
|
use devise::proc_macro2_diagnostics::SpanDiagnosticExt;
|
2021-07-09 23:34:54 +00:00
|
|
|
use devise::syn::{self, spanned::Spanned};
|
|
|
|
|
|
|
|
const ONE_DATABASE_ATTR: &str = "missing `#[database(\"name\")]` attribute";
|
|
|
|
const ONE_UNNAMED_FIELD: &str = "struct must have exactly one unnamed field";
|
2021-05-31 16:15:00 +00:00
|
|
|
|
|
|
|
#[derive(Debug, FromMeta)]
|
|
|
|
struct DatabaseAttribute {
|
|
|
|
#[meta(naked)]
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn derive_database(input: TokenStream) -> TokenStream {
|
|
|
|
DeriveGenerator::build_for(input, quote!(impl rocket_db_pools::Database))
|
|
|
|
.support(Support::TupleStruct)
|
|
|
|
.validator(ValidatorBuild::new()
|
2021-07-09 23:34:54 +00:00
|
|
|
.struct_validate(|_, s| {
|
|
|
|
if s.fields.len() == 1 {
|
2021-05-31 16:15:00 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2021-07-18 20:14:13 +00:00
|
|
|
Err(s.span().error(ONE_UNNAMED_FIELD))
|
2021-05-31 16:15:00 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2021-07-09 23:34:54 +00:00
|
|
|
.outer_mapper(MapperBuild::new()
|
|
|
|
.struct_map(|_, s| {
|
|
|
|
let pool_type = match &s.fields {
|
|
|
|
syn::Fields::Unnamed(f) => &f.unnamed[0].ty,
|
2021-05-31 16:15:00 +00:00
|
|
|
_ => unreachable!("Support::TupleStruct"),
|
|
|
|
};
|
|
|
|
|
2021-07-18 20:14:13 +00:00
|
|
|
let decorated_type = &s.ident;
|
|
|
|
let db_ty = quote_spanned!(decorated_type.span() =>
|
|
|
|
<#decorated_type as rocket_db_pools::Database>
|
|
|
|
);
|
|
|
|
|
|
|
|
quote_spanned! { decorated_type.span() =>
|
2021-07-09 23:34:54 +00:00
|
|
|
impl From<#pool_type> for #decorated_type {
|
|
|
|
fn from(pool: #pool_type) -> Self {
|
|
|
|
Self(pool)
|
|
|
|
}
|
2021-05-31 16:15:00 +00:00
|
|
|
}
|
2021-07-09 23:34:54 +00:00
|
|
|
|
|
|
|
impl std::ops::Deref for #decorated_type {
|
|
|
|
type Target = #pool_type;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::DerefMut for #decorated_type {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::async_trait]
|
|
|
|
impl<'r> rocket::request::FromRequest<'r> for &'r #decorated_type {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
async fn from_request(
|
|
|
|
req: &'r rocket::request::Request<'_>
|
|
|
|
) -> rocket::request::Outcome<Self, Self::Error> {
|
2021-07-18 20:14:13 +00:00
|
|
|
match #db_ty::fetch(req.rocket()) {
|
2021-07-09 23:34:54 +00:00
|
|
|
Some(db) => rocket::outcome::Outcome::Success(db),
|
2023-10-31 17:11:03 +00:00
|
|
|
None => rocket::outcome::Outcome::Error((
|
2021-07-09 23:34:54 +00:00
|
|
|
rocket::http::Status::InternalServerError, ()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl rocket::Sentinel for &#decorated_type {
|
|
|
|
fn abort(rocket: &rocket::Rocket<rocket::Ignite>) -> bool {
|
2021-07-18 20:14:13 +00:00
|
|
|
#db_ty::fetch(rocket).is_none()
|
2021-07-09 23:34:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-31 16:15:00 +00:00
|
|
|
})
|
|
|
|
)
|
2021-07-09 23:34:54 +00:00
|
|
|
.outer_mapper(quote!(#[rocket::async_trait]))
|
|
|
|
.inner_mapper(MapperBuild::new()
|
|
|
|
.try_struct_map(|_, s| {
|
|
|
|
let db_name = DatabaseAttribute::one_from_attrs("database", &s.attrs)?
|
|
|
|
.map(|attr| attr.name)
|
|
|
|
.ok_or_else(|| s.span().error(ONE_DATABASE_ATTR))?;
|
|
|
|
|
|
|
|
let fairing_name = format!("'{}' Database Pool", db_name);
|
|
|
|
|
|
|
|
let pool_type = match &s.fields {
|
|
|
|
syn::Fields::Unnamed(f) => &f.unnamed[0].ty,
|
2021-05-31 16:15:00 +00:00
|
|
|
_ => unreachable!("Support::TupleStruct"),
|
|
|
|
};
|
|
|
|
|
2021-07-18 20:14:13 +00:00
|
|
|
Ok(quote_spanned! { pool_type.span() =>
|
2021-07-09 23:34:54 +00:00
|
|
|
type Pool = #pool_type;
|
|
|
|
|
|
|
|
const NAME: &'static str = #db_name;
|
|
|
|
|
|
|
|
fn init() -> rocket_db_pools::Initializer<Self> {
|
|
|
|
rocket_db_pools::Initializer::with_name(#fairing_name)
|
2021-05-31 16:15:00 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.to_tokens()
|
|
|
|
}
|