feat: allow customize derives for serverfn input struct (#2545)

This commit is contained in:
Luxalpa 2024-05-06 14:54:29 +02:00 committed by GitHub
parent 47bcee0ef4
commit 20f4323e50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 6 deletions

View File

@ -33,6 +33,7 @@ use syn::__private::ToTokens;
/// - `endpoint`: specifies the exact path at which the server function handler will be mounted,
/// relative to the prefix (defaults to the function name followed by unique hash)
/// - `input`: the encoding for the arguments (defaults to `PostUrl`)
/// - `input_derive`: a list of derives to be added on the generated input struct (defaults to `(Clone, serde::Serialize, serde::Deserialize)` if `input` is set to a custom struct, won't have an effect otherwise)
/// - `output`: the encoding for the response (defaults to `Json`)
/// - `client`: a custom `Client` implementation that will be used for this server fn
/// - `encoding`: (legacy, may be deprecated in future) specifies the encoding, which may be one

View File

@ -112,6 +112,7 @@ pub fn server_macro_impl(
struct_name,
prefix,
input,
input_derive,
output,
fn_path,
builtin_encoding,
@ -402,12 +403,18 @@ pub fn server_macro_impl(
Clone, #server_fn_path::serde_lite::Serialize, #server_fn_path::serde_lite::Deserialize
},
),
_ => (
PathInfo::Serde,
quote! {
Clone, #server_fn_path::serde::Serialize, #server_fn_path::serde::Deserialize
},
),
_ => match input_derive {
Some(derives) => {
let d = derives.elems;
(PathInfo::None, quote! { #d })
}
None => (
PathInfo::Serde,
quote! {
Clone, #server_fn_path::serde::Serialize, #server_fn_path::serde::Deserialize
},
),
},
};
let addl_path = match path {
PathInfo::Serde => quote! {
@ -673,6 +680,7 @@ struct ServerFnArgs {
struct_name: Option<Ident>,
prefix: Option<Literal>,
input: Option<Type>,
input_derive: Option<ExprTuple>,
output: Option<Type>,
fn_path: Option<Literal>,
req_ty: Option<Type>,
@ -693,6 +701,7 @@ impl Parse for ServerFnArgs {
// new arguments: can only be keyed by name
let mut input: Option<Type> = None;
let mut input_derive: Option<ExprTuple> = None;
let mut output: Option<Type> = None;
let mut req_ty: Option<Type> = None;
let mut res_ty: Option<Type> = None;
@ -760,6 +769,14 @@ impl Parse for ServerFnArgs {
));
}
input = Some(stream.parse()?);
} else if key == "input_derive" {
if input_derive.is_some() {
return Err(syn::Error::new(
key.span(),
"keyword argument repeated: `input_derive`",
));
}
input_derive = Some(stream.parse()?);
} else if key == "output" {
if encoding.is_some() {
return Err(syn::Error::new(
@ -902,6 +919,7 @@ impl Parse for ServerFnArgs {
struct_name,
prefix,
input,
input_derive,
output,
fn_path,
builtin_encoding,