133 lines
6.6 KiB
Rust
133 lines
6.6 KiB
Rust
extern crate proc_macro2;
|
|
extern crate quote;
|
|
extern crate syn;
|
|
|
|
extern crate proc_macro;
|
|
|
|
use proc_macro::TokenStream; // 用于处理宏输入
|
|
use quote::quote; // 用于生成代码
|
|
use syn::{Data, DeriveInput, Fields}; // 用于解析宏输入
|
|
|
|
pub fn gen_responsable(input: DeriveInput) -> TokenStream {
|
|
let name = &input.ident; // 获取结构体名称
|
|
let gen = match input.data {
|
|
Data::Struct(ref data) => match data.fields {
|
|
Fields::Named(ref _fields) => {
|
|
// // 处理命名字段
|
|
// let field_names = fields.named.iter().map(|f| &f.ident); // 获取所有字段名称
|
|
// let field_types = fields.named.iter().map(|f| &f.ty); // 获取所有字段类型
|
|
|
|
// // 获取`status`属性
|
|
// let status = match input.attrs.iter().find(|attr| attr.path().is_ident("status")) {
|
|
// Some(attr) => {
|
|
// let status_lit = attr.parse_args::<Ident>().unwrap(); // 解析`status`属性值
|
|
// quote! { #status_lit } // 生成状态码
|
|
// },
|
|
// None => quote! { hyper::StatusCode::OK }, // 默认状态码为`OK`
|
|
// };
|
|
|
|
// // 获取`headers`属性
|
|
// let headers = match input.attrs.iter().find(|attr| attr.path().is_ident("headers")) {
|
|
// Some(attr) => {
|
|
// let headers_lit = attr.parse_args::<Ident>().unwrap(); // 解析`headers`属性值
|
|
// quote! { #headers_lit } // 生成头部信息
|
|
// },
|
|
// None => quote! { hyper::HeaderMap::new() }, // 默认头部信息为空
|
|
// };
|
|
|
|
// 生成实现`IntoResponse` trait的代码
|
|
quote! {
|
|
impl axum::response::IntoResponse for #name {
|
|
fn into_response(self) -> axum::http::Response<axum::body::Body> {
|
|
let mut builder = hyper::Response::builder();
|
|
// .status(#status) // 设置状态码
|
|
// .headers(#headers); // 设置头部信息
|
|
|
|
let body = serde_json::to_string(&self).expect("Failed to serialize response"); // 序列化结构体为JSON字符串
|
|
|
|
axum::http::Response::new(axum::body::Body::from(body)) // 构建响应
|
|
}
|
|
}
|
|
}
|
|
},
|
|
Fields::Unnamed(ref _fields) => {
|
|
// // 处理未命名字段
|
|
// let field_names = fields.unnamed.iter().enumerate().map(|(i, _)| i); // 获取所有字段索引
|
|
// let field_types = fields.unnamed.iter().map(|f| &f.ty); // 获取所有字段类型
|
|
|
|
// // 获取`status`属性
|
|
// let status = match input.attrs.iter().find(|attr| attr.path().is_ident("status")) {
|
|
// Some(attr) => {
|
|
// let status_lit = attr.parse_args::<Ident>().unwrap(); // 解析`status`属性值
|
|
// quote! { #status_lit } // 生成状态码
|
|
// },
|
|
// None => quote! { hyper::StatusCode::OK }, // 默认状态码为`OK`
|
|
// };
|
|
|
|
// // 获取`headers`属性
|
|
// let headers = match input.attrs.iter().find(|attr| attr.path().is_ident("headers")) {
|
|
// Some(attr) => {
|
|
// let headers_lit = attr.parse_args::<Ident>().unwrap(); // 解析`headers`属性值
|
|
// quote! { #headers_lit } // 生成头部信息
|
|
// },
|
|
// None => quote! { hyper::HeaderMap::new() }, // 默认头部信息为空
|
|
// };
|
|
|
|
// 生成实现`IntoResponse` trait的代码
|
|
quote! {
|
|
impl axum::response::IntoResponse for #name {
|
|
fn into_response(self) -> axum::http::Response<axum::body::Body> {
|
|
// let mut builder = hyper::Response::builder();
|
|
// .status(#status) // 设置状态码
|
|
// .headers(#headers); // 设置头部信息
|
|
|
|
let body = serde_json::to_string(&self).expect("Failed to serialize response"); // 序列化结构体为JSON字符串
|
|
|
|
axum::http::Response::new(axum::body::Body::from(body)) // 构建响应
|
|
}
|
|
}
|
|
}
|
|
},
|
|
Fields::Unit => {
|
|
// // 处理无字段的结构体
|
|
// // 获取`status`属性
|
|
// let status = match input.attrs.iter().find(|attr| attr.path().is_ident("status")) {
|
|
// Some(attr) => {
|
|
// let status_lit = attr.parse_args::<Ident>().unwrap(); // 解析`status`属性值
|
|
// quote! { #status_lit } // 生成状态码
|
|
// },
|
|
// None => quote! { hyper::StatusCode::OK }, // 默认状态码为`OK`
|
|
// };
|
|
|
|
// // 获取`headers`属性
|
|
// let headers = match input.attrs.iter().find(|attr| attr.path().is_ident("headers")) {
|
|
// Some(attr) => {
|
|
// let headers_lit = attr.parse_args::<Ident>().unwrap(); // 解析`headers`属性值
|
|
// quote! { #headers_lit } // 生成头部信息
|
|
// },
|
|
// None => quote! { hyper::HeaderMap::new() }, // 默认头部信息为空
|
|
// };
|
|
|
|
// 生成实现`IntoResponse` trait的代码
|
|
quote! {
|
|
impl axum::response::IntoResponse for #name {
|
|
fn into_response(self) -> axum::http::Response<axum::body::Body> {
|
|
// let mut builder = hyper::Response::builder();
|
|
// .status(#status) // 设置状态码
|
|
// .headers(#headers); // 设置头部信息
|
|
|
|
let body = serde_json::to_string(&self).expect("Failed to serialize response"); // 序列化结构体为JSON字符串
|
|
|
|
axum::http::Response::new(hyper::body::Body::from(body)) // 构建响应
|
|
}
|
|
}
|
|
}
|
|
},
|
|
},
|
|
Data::Enum(_) | Data::Union(_) => {
|
|
// 不支持枚举或联合体
|
|
unimplemented!("IntoResponse derive is not implemented for enums or unions.")
|
|
},
|
|
};
|
|
gen.into()
|
|
} |