(attr: TokenStream, item: TokenStream)
| 11 | use syn::{ItemImpl, parse_macro_input}; |
| 12 | |
| 13 | pub fn feign_impl(attr: TokenStream, item: TokenStream) -> TokenStream { |
| 14 | let url = match parse_url_stream(&attr) { |
| 15 | Ok(url) => url, |
| 16 | Err(err) => return err.into_compile_error().into(), |
| 17 | }; |
| 18 | |
| 19 | let meta_map = parse_exprs(&remove_url_attr(&attr.to_string())); |
| 20 | let item_parse = parse_macro_input!(item as syn::Item); |
| 21 | |
| 22 | match item_parse { |
| 23 | syn::Item::Trait(item_trait) => { |
| 24 | let trait_ident = &item_trait.ident; |
| 25 | let builder_ident = format_ident!("{}Builder", trait_ident); |
| 26 | |
| 27 | let trait_fn_streams = |
| 28 | match fn_to_streams_for_trait(url.clone(), &item_trait.items, meta_map.clone()) { |
| 29 | Ok(streams) => streams, |
| 30 | Err(err) => return err.into_compile_error().into(), |
| 31 | }; |
| 32 | |
| 33 | quote! { |
| 34 | pub struct #builder_ident { |
| 35 | config: Option<::feignhttp::ClientConfig>, |
| 36 | context: Option<Box<dyn ::feignhttp::FeignContext>>, |
| 37 | } |
| 38 | |
| 39 | impl ::feignhttp::FeignClientBuilder for #builder_ident { |
| 40 | type Target = #trait_ident; |
| 41 | |
| 42 | fn new() -> Self { |
| 43 | Self { |
| 44 | config: None, |
| 45 | context: None, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn config(mut self, config: ::feignhttp::ClientConfig) -> Self { |
| 50 | self.config = Some(config); |
| 51 | self |
| 52 | } |
| 53 | |
| 54 | fn context<C>(mut self, context: C) -> Self |
| 55 | where |
| 56 | C: ::feignhttp::FeignContext + 'static, |
| 57 | { |
| 58 | self.context = Some(Box::new(context)); |
| 59 | self |
| 60 | } |
| 61 | |
| 62 | fn build(self) -> ::feignhttp::Result<Self::Target> { |
| 63 | let client = match self.config { |
| 64 | Some(config) => ::feignhttp::HttpClient::with_config(config)?, |
| 65 | None => ::feignhttp::HttpClient::new()?, |
| 66 | }; |
| 67 | Ok(#trait_ident { |
| 68 | client, |
| 69 | context: self.context, |
| 70 | }) |
no test coverage detected