| 3 | use syn::{Error, ItemImpl, Type, spanned::Spanned}; |
| 4 | |
| 5 | pub(crate) fn error(_: TokenStream, item: ItemImpl) -> Result<TokenStream, Error> { |
| 6 | let Type::Path(ref err_ty) = *item.self_ty else { |
| 7 | return Err(Error::new(item.self_ty.span(), "expect Struct or Enum")); |
| 8 | }; |
| 9 | |
| 10 | let call_impl = crate::find_async_method(&item.items, "call") |
| 11 | .ok_or_else(|| Error::new(err_ty.span(), "expect 'async fn call' method"))??; |
| 12 | |
| 13 | let call_stmts = &call_impl.block.stmts; |
| 14 | |
| 15 | Ok(quote! { |
| 16 | impl<'r> ::xitca_web::service::Service<WebContext<'r, ::xitca_web::error::Request<'r>>> for #err_ty { |
| 17 | type Response = WebResponse; |
| 18 | type Error = ::core::convert::Infallible; |
| 19 | |
| 20 | async fn call(&self, ctx: WebContext<'r, ::xitca_web::error::Request<'r>>) -> Result<Self::Response, Self::Error> { |
| 21 | Ok({#(#call_stmts)*}) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl From<#err_ty> for ::xitca_web::error::Error { |
| 26 | fn from(e: #err_ty) -> Self { |
| 27 | Self::from_service(e) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | .into()) |
| 32 | } |