remove the extract macro

This commit is contained in:
Evan Almloff 2023-07-12 16:20:44 -06:00
parent 0636e94f37
commit c6ddee6f21
4 changed files with 9 additions and 68 deletions

View file

@ -121,23 +121,22 @@ fn app(cx: Scope) -> Element {
} }
#[server(GetUserName)] #[server(GetUserName)]
pub async fn get_user_name( pub async fn get_user_name() -> Result<String, ServerFnError> {
#[extract] session: crate::auth::Session, let session: crate::auth::Session = extract().await?;
) -> Result<String, ServerFnError> {
Ok(session.0.current_user.unwrap().username.to_string()) Ok(session.0.current_user.unwrap().username.to_string())
} }
#[server(Login)] #[server(Login)]
pub async fn login(#[extract] auth: crate::auth::Session) -> Result<(), ServerFnError> { pub async fn login() -> Result<(), ServerFnError> {
let auth: crate::auth::Session = extract().await?;
auth.login_user(2); auth.login_user(2);
Ok(()) Ok(())
} }
#[server(Permissions)] #[server(Permissions)]
pub async fn get_permissions( pub async fn get_permissions() -> Result<String, ServerFnError> {
#[extract] method: axum::http::Method, let method: axum::http::Method = extract().await?;
#[extract] auth: crate::auth::Session, let auth: crate::auth::Session = extract().await?;
) -> Result<String, ServerFnError> {
let current_user = auth.current_user.clone().unwrap_or_default(); let current_user = auth.current_user.clone().unwrap_or_default();
// lets check permissions only and not worry about if they are anon or not // lets check permissions only and not worry about if they are anon or not

View file

@ -5,7 +5,6 @@ use quote::{ToTokens, __private::TokenStream as TokenStream2};
use server_fn_macro::*; use server_fn_macro::*;
use syn::{ use syn::{
parse::{Parse, ParseStream}, parse::{Parse, ParseStream},
spanned::Spanned,
Ident, ItemFn, Token, Ident, ItemFn, Token,
}; };
@ -67,22 +66,6 @@ pub fn server(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
Err(e) => return e.into(), Err(e) => return e.into(),
}; };
// find all arguments with the #[extract] attribute
let mut extractors: Vec<Extractor> = vec![];
function.sig.inputs = function
.sig
.inputs
.into_iter()
.filter(|arg| {
if let Ok(extractor) = syn::parse2(arg.clone().into_token_stream()) {
extractors.push(extractor);
false
} else {
true
}
})
.collect();
// extract all #[middleware] attributes // extract all #[middleware] attributes
let mut middlewares: Vec<Middleware> = vec![]; let mut middlewares: Vec<Middleware> = vec![];
function.attrs.retain(|attr| { function.attrs.retain(|attr| {
@ -107,7 +90,6 @@ pub fn server(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
let mapped_body = quote::quote! { let mapped_body = quote::quote! {
#(#attrs)* #(#attrs)*
#vis #sig { #vis #sig {
#(#extractors)*
#block #block
} }
}; };
@ -155,45 +137,6 @@ pub fn server(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
} }
} }
struct Extractor {
pat: syn::PatType,
}
impl ToTokens for Extractor {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let pat = &self.pat;
tokens.extend(quote::quote! {
let #pat = ::dioxus_fullstack::prelude::extract_server_context().await?;
});
}
}
impl Parse for Extractor {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let arg: syn::FnArg = input.parse()?;
match arg {
syn::FnArg::Typed(mut pat_type) => {
let mut contains_extract = false;
pat_type.attrs.retain(|attr| {
let is_extract = attr.path().is_ident("extract");
if is_extract {
contains_extract = true;
}
!is_extract
});
if !contains_extract {
return Err(syn::Error::new(
pat_type.span(),
"expected an argument with the #[extract] attribute",
));
}
Ok(Extractor { pat: pat_type })
}
_ => Err(syn::Error::new(arg.span(), "expected a typed argument")),
}
}
}
#[derive(Debug)] #[derive(Debug)]
struct Middleware { struct Middleware {
expr: syn::Expr, expr: syn::Expr,

View file

@ -47,8 +47,7 @@ pub mod prelude {
pub use crate::server_context::Axum; pub use crate::server_context::Axum;
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
pub use crate::server_context::{ pub use crate::server_context::{
extract_server_context, server_context, DioxusServerContext, FromServerContext, extract, server_context, DioxusServerContext, FromServerContext, ProvideServerContext,
ProvideServerContext,
}; };
pub use crate::server_fn::DioxusServerFn; pub use crate::server_fn::DioxusServerFn;
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]

View file

@ -117,7 +117,7 @@ pub fn server_context() -> DioxusServerContext {
/// Extract some part from the current server request. /// Extract some part from the current server request.
/// ///
/// This function will only provide the current server context if it is called from a server function or on the server rendering a request. /// This function will only provide the current server context if it is called from a server function or on the server rendering a request.
pub async fn extract_server_context<E: FromServerContext<I>, I>() -> Result<E, E::Rejection> { pub async fn extract<E: FromServerContext<I>, I>() -> Result<E, E::Rejection> {
E::from_request(&server_context()).await E::from_request(&server_context()).await
} }