#[cfg(test)]
mod tests;
#[allow(non_snake_case)]
pub mod http;
mod file;
pub use file::{ContentDisposition, FileResponse, FileResult};
use http::HttpResponse;
#[allow(clippy::result_large_err)]
pub type Result<T> = std::result::Result<T, HttpResponse>;
#[macro_export]
macro_rules! response {
($status:expr) => {{
let status = ::axum::http::StatusCode::from_u16($status)
.expect("Invalid status code");
$crate::http::HttpResponse::builder(status)
.message(status.canonical_reason().unwrap_or("No reason"))
}};
($status:expr, { $value:ident }) => {{
let status = ::axum::http::StatusCode::from_u16($status)
.expect("Invalid status code");
let json = ::serde_json::to_value(&$value).unwrap_or_else(|err| {
eprintln!("Warning: Failed to serialize response data: {err}");
json!({ "error": "Serialization failed" })
});
$crate::http::HttpResponse::builder(status)
.message(status.canonical_reason().unwrap_or("No reason"))
.data(json)
}};
($status:expr, { $($json:tt)* }) => {{
let status = ::axum::http::StatusCode::from_u16($status)
.expect("Invalid status code");
let mut json = ::serde_json::json!({ $($json)* });
let message = json.get("message")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
if let Some(obj) = json.as_object_mut() {
if message.is_some() {
obj.remove("message");
}
}
if let ::serde_json::Value::Object(obj) = &json {
if obj.is_empty() {
json = ::serde_json::Value::Null;
}
}
let response = $crate::http::HttpResponse::builder(status)
.message(message.unwrap_or_else(|| {
status.canonical_reason().unwrap_or("No reason").to_string()
}))
.data(json);
response
}};
}