mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 04:56:49 +00:00
feat(api): implement PKError in rust-api
This commit is contained in:
parent
a49dbefe83
commit
214f164fbc
9 changed files with 157 additions and 63 deletions
|
|
@ -10,4 +10,5 @@ proc-macro = true
|
|||
quote = "1.0"
|
||||
proc-macro2 = "1.0"
|
||||
syn = "2.0"
|
||||
prettyplease = "0.2.36"
|
||||
|
||||
|
|
|
|||
52
crates/macros/src/api.rs
Normal file
52
crates/macros/src/api.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
use quote::quote;
|
||||
use syn::{parse_macro_input, FnArg, ItemFn, Pat};
|
||||
|
||||
fn pretty_print(ts: &proc_macro2::TokenStream) -> String {
|
||||
let file = syn::parse_file(&ts.to_string()).unwrap();
|
||||
prettyplease::unparse(&file)
|
||||
}
|
||||
|
||||
pub fn macro_impl(
|
||||
_args: proc_macro::TokenStream,
|
||||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as ItemFn);
|
||||
|
||||
let fn_name = &input.sig.ident;
|
||||
let fn_params = &input.sig.inputs;
|
||||
let fn_body = &input.block;
|
||||
let syn::ReturnType::Type(_, fn_return_type) = &input.sig.output else {
|
||||
panic!("handler return type must not be nothing");
|
||||
};
|
||||
let pms: Vec<proc_macro2::TokenStream> = fn_params
|
||||
.iter()
|
||||
.map(|v| {
|
||||
let FnArg::Typed(pat) = v else {
|
||||
panic!("must not have self param in handler");
|
||||
};
|
||||
let mut pat = pat.pat.clone();
|
||||
if let Pat::Ident(ident) = *pat {
|
||||
let mut ident = ident.clone();
|
||||
ident.mutability = None;
|
||||
pat = Box::new(Pat::Ident(ident));
|
||||
}
|
||||
quote! { #pat }
|
||||
})
|
||||
.collect();
|
||||
|
||||
let res = quote! {
|
||||
#[allow(unused_mut)]
|
||||
pub async fn #fn_name(#fn_params) -> axum::response::Response {
|
||||
async fn inner(#fn_params) -> Result<#fn_return_type, crate::error::PKError> {
|
||||
#fn_body
|
||||
}
|
||||
|
||||
match inner(#(#pms),*).await {
|
||||
Ok(res) => res.into_response(),
|
||||
Err(err) => err.into_response(),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
res.into()
|
||||
}
|
||||
|
|
@ -1,8 +1,14 @@
|
|||
use proc_macro::TokenStream;
|
||||
|
||||
mod api;
|
||||
mod entrypoint;
|
||||
mod model;
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn api_endpoint(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
api::macro_impl(args, input)
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn main(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
entrypoint::macro_impl(args, input)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue