Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add input_derive parameter to #[server] macro (closes #2544) #2545

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server_fn/server_fn_macro_default/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 24 additions & 6 deletions server_fn_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub fn server_macro_impl(
struct_name,
prefix,
input,
input_derive,
output,
fn_path,
builtin_encoding,
Expand Down Expand Up @@ -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! {
Expand Down Expand Up @@ -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>,
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -902,6 +919,7 @@ impl Parse for ServerFnArgs {
struct_name,
prefix,
input,
input_derive,
output,
fn_path,
builtin_encoding,
Expand Down
Loading