Skip to content

Commit

Permalink
support other types of req/res
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj committed Jan 19, 2024
1 parent 1b1e027 commit 366bb1e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions server_fn_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ nightly = []
ssr = []
actix = []
axum = []
reqwest = []
72 changes: 67 additions & 5 deletions server_fn_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ pub fn server_macro_impl(
output,
fn_path,
builtin_encoding,
req_ty,
res_ty,
client,
custom_wrapper,
} = args;
_ = custom_wrapper; // TODO: this should be used to enable custom encodings
let prefix = prefix.unwrap_or_else(|| Literal::string(default_path));
let fn_path = fn_path.unwrap_or_else(|| Literal::string(""));
let input_ident = match &input {
Expand Down Expand Up @@ -380,9 +385,16 @@ pub fn server_macro_impl(
PathInfo::None => quote! {},
};

// TODO reqwest
let client = quote! {
#server_fn_path::client::browser::BrowserClient
let client = if let Some(client) = client {
client.to_token_stream()
} else if cfg!(feature = "reqwest") {
quote! {
#server_fn_path::client::reqwest::BrowserClient
}
} else {
quote! {
#server_fn_path::client::reqwest::ReqwestClient
}
};

let req = if !cfg!(feature = "ssr") {
Expand All @@ -397,11 +409,14 @@ pub fn server_macro_impl(
quote! {
#server_fn_path::request::actix::ActixRequest
}
} else if let Some(req_ty) = req_ty {
req_ty.to_token_stream()
} else {
return Err(syn::Error::new(
Span::call_site(),
"If the `ssr` feature is enabled, either the `actix` or `axum` \
features should also be enabled.",
features should also be enabled, or the `req = ` argument should \
be provided to specify the request type.",
));
};
let res = if !cfg!(feature = "ssr") {
Expand All @@ -416,11 +431,14 @@ pub fn server_macro_impl(
quote! {
#server_fn_path::response::actix::ActixResponse
}
} else if let Some(res_ty) = res_ty {
res_ty.to_token_stream()
} else {
return Err(syn::Error::new(
Span::call_site(),
"If the `ssr` feature is enabled, either the `actix` or `axum` \
features should also be enabled.",
features should also be enabled, or the `res = ` argument should \
be provided to specify the response type.",
));
};

Expand Down Expand Up @@ -614,6 +632,10 @@ struct ServerFnArgs {
input: Option<Type>,
output: Option<Type>,
fn_path: Option<Literal>,
req_ty: Option<Type>,
res_ty: Option<Type>,
client: Option<Type>,
custom_wrapper: Option<Type>,
builtin_encoding: bool,
}

Expand All @@ -628,6 +650,10 @@ impl Parse for ServerFnArgs {
// new arguments: can only be keyed by name
let mut input: Option<Type> = None;
let mut output: Option<Type> = None;
let mut req_ty: Option<Type> = None;
let mut res_ty: Option<Type> = None;
let mut client: Option<Type> = None;
let mut custom_wrapper: Option<Type> = None;

let mut use_key_and_value = false;
let mut arg_pos = 0;
Expand Down Expand Up @@ -703,6 +729,38 @@ impl Parse for ServerFnArgs {
));
}
output = Some(stream.parse()?);
} else if key == "req" {
if req_ty.is_some() {
return Err(syn::Error::new(
key.span(),
"keyword argument repeated: `req`",
));
}
req_ty = Some(stream.parse()?);
} else if key == "res" {
if res_ty.is_some() {
return Err(syn::Error::new(
key.span(),
"keyword argument repeated: `res`",
));
}
res_ty = Some(stream.parse()?);
} else if key == "client" {
if client.is_some() {
return Err(syn::Error::new(
key.span(),
"keyword argument repeated: `client`",
));
}
client = Some(stream.parse()?);
} else if key == "custom" {
if custom_wrapper.is_some() {
return Err(syn::Error::new(
key.span(),
"keyword argument repeated: `custom`",
));
}
custom_wrapper = Some(stream.parse()?);
} else {
return Err(lookahead.error());
}
Expand Down Expand Up @@ -794,6 +852,10 @@ impl Parse for ServerFnArgs {
output,
fn_path,
builtin_encoding,
req_ty,
res_ty,
client,
custom_wrapper,
})
}
}
Expand Down

0 comments on commit 366bb1e

Please sign in to comment.