From a9d510cddf9fd804f2983f71bd4e70bc3638275c Mon Sep 17 00:00:00 2001
From: lazulit3 <128615656+lazulit3@users.noreply.github.com>
Date: Tue, 17 Dec 2024 15:05:07 -0800
Subject: [PATCH] fix: remove derive block expr to fix LSP type inference
This updates `rustify_derive` to remove the block expression that the `impl Endpoint` was wrapped in.
This resolves an issue where `rust-analyzer` is unable to infer the type of `Endpoint::Response` in some cases.
---
rustify_derive/src/lib.rs | 54 +++++++++++++++----------------------
rustify_derive/src/parse.rs | 2 +-
2 files changed, 22 insertions(+), 34 deletions(-)
diff --git a/rustify_derive/src/lib.rs b/rustify_derive/src/lib.rs
index b0acead..2cd8b2e 100644
--- a/rustify_derive/src/lib.rs
+++ b/rustify_derive/src/lib.rs
@@ -117,10 +117,10 @@ fn gen_query(
// Construct query function
let temp = parse::fields_to_struct(v, serde_attrs);
quote! {
- fn query(&self) -> Result, ClientError> {
+ fn query(&self) -> Result , rustify::errors::ClientError> {
#temp
- Ok(Some(build_query(&__temp)?))
+ Ok(Some(rustify::http::build_query(&__temp)?))
}
}
} else {
@@ -156,7 +156,7 @@ fn gen_body(
let id = v[0].ident.clone().unwrap();
Ok(quote! {
- fn body(&self) -> Result >, ClientError>{
+ fn body(&self) -> Result >, rustify::errors::ClientError>{
Ok(Some(self.#id.clone()))
}
})
@@ -164,20 +164,20 @@ fn gen_body(
} else if let Some(v) = fields.get(&EndpointAttribute::Body) {
let temp = parse::fields_to_struct(v, serde_attrs);
Ok(quote! {
- fn body(&self) -> Result >, ClientError> {
+ fn body(&self) -> Result >, rustify::errors::ClientError> {
#temp
- Ok(Some(build_body(&__temp, Self::REQUEST_BODY_TYPE)?))
+ Ok(Some(rustify::http::build_body(&__temp, Self::REQUEST_BODY_TYPE)?))
}
})
// Then for any untagged fields
} else if let Some(v) = fields.get(&EndpointAttribute::Untagged) {
let temp = parse::fields_to_struct(v, serde_attrs);
Ok(quote! {
- fn body(&self) -> Result >, ClientError> {
+ fn body(&self) -> Result >, rustify::errors::ClientError> {
#temp
- Ok(Some(build_body(&__temp, Self::REQUEST_BODY_TYPE)?))
+ Ok(Some(rustify::http::build_body(&__temp, Self::REQUEST_BODY_TYPE)?))
}
})
// Leave it undefined if no body fields found
@@ -298,39 +298,27 @@ fn endpoint_derive(s: synstructure::Structure) -> proc_macro2::TokenStream {
let (impl_generics, ty_generics, where_clause) = s.ast().generics.split_for_impl();
// Generate Endpoint implementation
- let const_name = format!("_DERIVE_Endpoint_FOR_{}", id);
- let const_ident = Ident::new(const_name.as_str(), Span::call_site());
quote! {
- #[allow(non_local_definitions)]
- const #const_ident: () = {
- use rustify::__private::serde::Serialize;
- use rustify::http::{build_body, build_query};
- use rustify::client::Client;
- use rustify::endpoint::Endpoint;
- use rustify::enums::{RequestMethod, RequestType, ResponseType};
- use rustify::errors::ClientError;
+ impl #impl_generics rustify::endpoint::Endpoint for #id #ty_generics #where_clause {
+ type Response = #response;
+ const REQUEST_BODY_TYPE: rustify::enums::RequestType = rustify::enums::RequestType::#request_type;
+ const RESPONSE_BODY_TYPE: rustify::enums::ResponseType = rustify::enums::ResponseType::#response_type;
- impl #impl_generics Endpoint for #id #ty_generics #where_clause {
- type Response = #response;
- const REQUEST_BODY_TYPE: RequestType = RequestType::#request_type;
- const RESPONSE_BODY_TYPE: ResponseType = ResponseType::#response_type;
-
- fn path(&self) -> String {
- #path
- }
+ fn path(&self) -> String {
+ #path
+ }
- fn method(&self) -> RequestMethod {
- RequestMethod::#method
- }
+ fn method(&self) -> rustify::enums::RequestMethod {
+ rustify::enums::RequestMethod::#method
+ }
- #query
+ #query
- #body
- }
+ #body
+ }
- #builder
- };
+ #builder
}
}
diff --git a/rustify_derive/src/parse.rs b/rustify_derive/src/parse.rs
index ec42952..255f913 100644
--- a/rustify_derive/src/parse.rs
+++ b/rustify_derive/src/parse.rs
@@ -219,7 +219,7 @@ pub(crate) fn fields_to_struct(fields: &[Field], attrs: &[Meta]) -> proc_macro2:
.collect::>();
quote! {
- #[derive(Serialize)]
+ #[derive(serde::Serialize)]
#(#attrs)*
struct __Temp<'a> {
#(#def)*