Skip to content

Commit

Permalink
remove list of magic identifiers, use rust-analyzer to help with impo…
Browse files Browse the repository at this point in the history
…rts instead
  • Loading branch information
gbj committed Jan 9, 2024
1 parent 33e88e0 commit 095769e
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions server_fn_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,20 @@ pub fn server_macro_impl(
} = args;
let prefix = prefix.unwrap_or_else(|| Literal::string(default_path));
let fn_path = fn_path.unwrap_or_else(|| Literal::string(""));
let input_ident = input.unwrap_or_else(|| syn::parse_quote!(PostUrl));
let input = codec_ident(server_fn_path.as_ref(), input_ident.clone());
let output = output.unwrap_or_else(|| syn::parse_quote!(Json));
let output = codec_ident(server_fn_path.as_ref(), output);
let input_ident = input
.as_ref()
.map(ToString::to_string)
.unwrap_or_else(|| "PostUrl".to_string());
let input = input.map(|n| n.to_token_stream()).unwrap_or_else(|| {
quote! {
#server_fn_path::codec::PostUrl
}
});
let output = output.map(|n| n.to_token_stream()).unwrap_or_else(|| {
quote! {
#server_fn_path::codec::Json
}
});
// default to PascalCase version of function name if no struct name given
let struct_name = struct_name.unwrap_or_else(|| {
let upper_camel_case_name = Converter::new()
Expand Down Expand Up @@ -306,7 +316,7 @@ pub fn server_macro_impl(
}
};

let (is_serde, derives) = match input_ident.to_string().as_str() {
let (is_serde, derives) = match input_ident.as_str() {
"Rkyv" => todo!("implement derives for Rkyv"),
"MultipartFormData" => (false, quote! {}),
"SerdeLite" => (
Expand Down Expand Up @@ -828,29 +838,3 @@ impl ServerFnBody {
}
}
}

/// Returns either the path of the codec (if it's a builtin) or the
/// original ident.
fn codec_ident(server_fn_path: Option<&Path>, ident: Ident) -> TokenStream2 {
if let Some(server_fn_path) = server_fn_path {
let str = ident.to_string();
if [
"GetUrl",
"PostUrl",
"Cbor",
"Json",
"Rkyv",
"Streaming",
"StreamingText",
"MultipartFormData",
]
.contains(&str.as_str())
{
return quote! {
#server_fn_path::codec::#ident
};
}
}

ident.into_token_stream()
}

0 comments on commit 095769e

Please sign in to comment.