mod create;
mod delete;
mod get;
mod list;
mod patch;
mod update;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use crate::crud::parse::{CrudEntityInfo, CrudFieldInfo};
pub(super) const DEFAULT_PAGE_LIMIT: i64 = 100;
pub(super) fn pk_id_type(pk_fields: &[&CrudFieldInfo]) -> TokenStream2 {
if pk_fields.len() == 1 {
let ty = &pk_fields[0].ty;
quote! { #ty }
} else {
quote! { String }
}
}
pub(super) fn type_is_uuid(ty: &syn::Type) -> bool {
if let syn::Type::Path(tp) = ty {
tp.path.segments.last().is_some_and(|seg| seg.ident == "Uuid")
} else {
false
}
}
pub(super) fn internal_error_response(
core: &TokenStream2,
entity_name: &str,
operation: &str,
) -> TokenStream2 {
quote! {
(axum::http::StatusCode::INTERNAL_SERVER_ERROR,
#core::Json(#core::crud::ErrorResponse::new(
format!("Failed to {} {}: {}", #operation, #entity_name, e)
))).into_response()
}
}
pub(super) fn not_found_response(core: &TokenStream2) -> TokenStream2 {
quote! {
(axum::http::StatusCode::NOT_FOUND,
#core::Json(#core::crud::ErrorResponse::new("Not found"))).into_response()
}
}
pub fn generate_routes(entity: &CrudEntityInfo) -> TokenStream2 {
let core = crate::paths::core_crate();
let pg = crate::paths::pg_crate();
let path = entity.effective_path();
let mut handlers = Vec::new();
let mut registrations = Vec::new();
let (list_handler, list_reg) = list::generate_list_handler(entity, &path, &core, &pg);
handlers.push(list_handler);
registrations.push(list_reg);
let (get_handler, get_reg) = get::generate_get_handler(entity, &path, &core, &pg);
handlers.push(get_handler);
registrations.push(get_reg);
if !entity.config.read_only && !entity.config.skip_create {
let (create_handler, create_reg) = create::generate_create_handler(entity, &path, &core, &pg);
handlers.push(create_handler);
registrations.push(create_reg);
}
if !entity.config.read_only {
let (put_handler, put_reg) = update::generate_put_handler(entity, &path, &core, &pg);
handlers.push(put_handler);
registrations.push(put_reg);
}
if !entity.config.read_only {
let (patch_handler, patch_reg) = patch::generate_patch_handler(entity, &path, &core, &pg);
handlers.push(patch_handler);
registrations.push(patch_reg);
}
if !entity.config.read_only && !entity.config.skip_delete {
let (delete_handler, delete_reg) = delete::generate_delete_handler(entity, &path, &core, &pg);
handlers.push(delete_handler);
registrations.push(delete_reg);
}
quote! {
#(#handlers)*
#(#registrations)*
}
}