From 1f7b4838f1e889d76c79b6a2e6ba279f3d290506 Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Wed, 23 Apr 2025 23:06:19 +0700 Subject: [PATCH 1/6] adjust some docs --- flake.nix | 2 +- lazybe-macros/src/lib.rs | 6 +++--- lazybe/Cargo.toml | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/flake.nix b/flake.nix index 27a0965..dc8eeea 100644 --- a/flake.nix +++ b/flake.nix @@ -145,8 +145,8 @@ watchexec which # rust + cargo-edit cargo-expand - cargo-outdated rust ]) ++ (builtins.attrValues scripts); diff --git a/lazybe-macros/src/lib.rs b/lazybe-macros/src/lib.rs index dcd3a23..9bd7bf2 100644 --- a/lazybe-macros/src/lib.rs +++ b/lazybe-macros/src/lib.rs @@ -39,9 +39,9 @@ pub fn derive_newtype(input: TokenStream) -> TokenStream { /// /// # Field attributes /// - `primary_key` - Specify the field to be used as primary key. -/// - `generate_with = "..."` - A function use for generating an ID. If omitted, ID should be generate by the database. -/// - `created_at` - Speicify the field for created_at timestamp. The time is stamped once a record is created. -/// - `updated_at` - Speicify the field for updated_at timestamp. The time is stamped once a record is updated. +/// - `generate_with = "..."` - A function use for generating an ID. If omitted, ID should be generated by the database. +/// - `created_at` - Specify the field for created_at timestamp. The time is stamped once a record is created. +/// - `updated_at` - Specify the field for updated_at timestamp. The time is stamped once a record is updated. /// - `json` - The field should be encoded as JSON column. pub fn derive_entity(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); diff --git a/lazybe/Cargo.toml b/lazybe/Cargo.toml index 148afed..bcd52fa 100644 --- a/lazybe/Cargo.toml +++ b/lazybe/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lazybe" -description = "Derive boring CRUD API boilerplate for Rust backend" +description = "Derive boring CRUD boilerplate for Rust backend" version.workspace = true edition.workspace = true authors.workspace = true From c52f24d24d2c1def7f1e688453bdb5e5191ffd67 Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Thu, 24 Apr 2025 00:01:33 +0700 Subject: [PATCH 2/6] shorten some lenghty macro --- lazybe-macros/src/entity.rs | 44 ++++++++++++++----------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/lazybe-macros/src/entity.rs b/lazybe-macros/src/entity.rs index a869392..692ba84 100644 --- a/lazybe-macros/src/entity.rs +++ b/lazybe-macros/src/entity.rs @@ -172,22 +172,15 @@ fn expand_struct(input: &DeriveInput, data_struct: &DataStruct) -> syn::Result { let entity_meta = EntityMeta::try_parse(input, fields_named)?; - let entity_types_impl = entity_types_impl(&entity_meta); - let entity_row_impl = entity_row_impl(&entity_meta); - let entity_entity_trait_impl = entity_entity_trait_impl(&entity_meta); - let entity_query_trait_impl = entity_query_trait_impl(&entity_meta); - let entity_route_trait_impl = entity_route_trait_impl(&entity_meta); - let entity_collection_api_trait_impl = entity_collection_api_trait_impl(&entity_meta); - let entity_validation_hook_trait_impl = entity_validation_hook_trait_impl(&entity_meta); - Ok(quote! { - #entity_types_impl - #entity_row_impl - #entity_entity_trait_impl - #entity_query_trait_impl - #entity_route_trait_impl - #entity_collection_api_trait_impl - #entity_validation_hook_trait_impl - }) + let mut ts = TokenStream::new(); + ts.extend(entity_types_impl(&entity_meta)); + ts.extend(entity_row_impl(&entity_meta)); + ts.extend(entity_entity_trait_impl(&entity_meta)); + ts.extend(entity_query_trait_impl(&entity_meta)); + ts.extend(entity_route_trait_impl(&entity_meta)); + ts.extend(entity_collection_api_trait_impl(&entity_meta)); + ts.extend(entity_validation_hook_trait_impl(&entity_meta)); + Ok(ts) } Fields::Unnamed(_) => Err(syn::Error::new_spanned( &input.ident, @@ -401,18 +394,13 @@ fn entity_entity_trait_impl(entity_meta: &EntityMeta) -> TokenStream { } fn entity_query_trait_impl(entity_meta: &EntityMeta) -> TokenStream { - let get_query_trait_impl = get_query_trait_impl(entity_meta); - let list_query_trait_impl = list_query_trait_impl(entity_meta); - let create_query_trait_impl = create_query_trait_impl(entity_meta); - let update_query_trait_impl = update_query_trait_impl(entity_meta); - let delete_query_trait_impl = delete_query_trait_impl(entity_meta); - quote! { - #get_query_trait_impl - #list_query_trait_impl - #create_query_trait_impl - #update_query_trait_impl - #delete_query_trait_impl - } + let mut ts = TokenStream::new(); + ts.extend(get_query_trait_impl(entity_meta)); + ts.extend(list_query_trait_impl(entity_meta)); + ts.extend(create_query_trait_impl(entity_meta)); + ts.extend(update_query_trait_impl(entity_meta)); + ts.extend(delete_query_trait_impl(entity_meta)); + ts } fn create_query_trait_impl(entity_meta: &EntityMeta) -> TokenStream { From 8cd68dac2229132d6d9beb93a54990935c43ef02 Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Thu, 24 Apr 2025 11:24:31 +0700 Subject: [PATCH 3/6] use link to top-level axum crate --- lazybe/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lazybe/src/lib.rs b/lazybe/src/lib.rs index 2b25cf1..64c6f72 100644 --- a/lazybe/src/lib.rs +++ b/lazybe/src/lib.rs @@ -7,7 +7,7 @@ //! Much of this work tends to be boilerplate, with little domain logic or custom code. //! //! If you are already using: -//! - [`axum`] for HTTP interface +//! - [`axum`](::axum) for HTTP interface //! - [`sqlx`] for database interactions //! - [`utoipa`] for OpenAPI docuementation //! From 7df1c37b50fcb55d410e157c20be01ab7c35b058 Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Thu, 24 Apr 2025 23:03:56 +0700 Subject: [PATCH 4/6] Make page use all u32 --- examples/kitchen-sink/examples/api_pagination.rs | 4 ++-- lazybe/src/entity/ops.rs | 8 ++++++-- lazybe/src/page.rs | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/kitchen-sink/examples/api_pagination.rs b/examples/kitchen-sink/examples/api_pagination.rs index 71003df..f2b9a5c 100644 --- a/examples/kitchen-sink/examples/api_pagination.rs +++ b/examples/kitchen-sink/examples/api_pagination.rs @@ -78,8 +78,8 @@ pub struct Book { #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct PaginatedBook { pub page: u32, - pub page_size: u64, - pub count: u64, + pub page_size: u32, + pub count: u32, pub data: Vec, } diff --git a/lazybe/src/entity/ops.rs b/lazybe/src/entity/ops.rs index 3edbe72..6064c93 100644 --- a/lazybe/src/entity/ops.rs +++ b/lazybe/src/entity/ops.rs @@ -153,8 +153,12 @@ where let count_result: CountResult = sqlx::query_as(&count_query).fetch_one(tx.deref_mut()).await?; + let total_records: u32 = count_result + .count + .try_into() + .expect("record count does not fit in u32"); let mut page: u32 = 0; - let mut page_size: u64 = count_result.count.unsigned_abs(); + let mut page_size: u32 = total_records; if let Some(p) = pagination { page = p.page; page_size = p.limit.into(); @@ -163,7 +167,7 @@ where let result = Page { page, page_size, - total_records: count_result.count.unsigned_abs(), + total_records, data: data_result.into_iter().map(|i| i.into()).collect(), }; Ok(result) diff --git a/lazybe/src/page.rs b/lazybe/src/page.rs index 6e61525..c8c769b 100644 --- a/lazybe/src/page.rs +++ b/lazybe/src/page.rs @@ -21,7 +21,7 @@ impl PaginationInput { pub struct Page { /// 0-index page number (0 is the first page) pub page: u32, - pub page_size: u64, - pub total_records: u64, + pub page_size: u32, + pub total_records: u32, pub data: Vec, } From 73964f5884f4b0378317be65a394e5b4c2b2fd3f Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Thu, 24 Apr 2025 23:04:15 +0700 Subject: [PATCH 5/6] fmt --- lazybe/src/entity/ops.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lazybe/src/entity/ops.rs b/lazybe/src/entity/ops.rs index 6064c93..99c28b3 100644 --- a/lazybe/src/entity/ops.rs +++ b/lazybe/src/entity/ops.rs @@ -153,10 +153,7 @@ where let count_result: CountResult = sqlx::query_as(&count_query).fetch_one(tx.deref_mut()).await?; - let total_records: u32 = count_result - .count - .try_into() - .expect("record count does not fit in u32"); + let total_records: u32 = count_result.count.try_into().expect("record count does not fit in u32"); let mut page: u32 = 0; let mut page_size: u32 = total_records; if let Some(p) = pagination { From 655ad723cba45c6dfb9535a7b1b082b5f5106b4e Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Thu, 24 Apr 2025 23:06:56 +0700 Subject: [PATCH 6/6] fix typo --- lazybe/src/db.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lazybe/src/db.rs b/lazybe/src/db.rs index ebcfc41..8fb2d94 100644 --- a/lazybe/src/db.rs +++ b/lazybe/src/db.rs @@ -13,7 +13,7 @@ pub trait DbCtx { fn query_builder(&self) -> Self::Qb; } -/// Integraion with sqlite database +/// Integration with sqlite database #[cfg(feature = "sqlite")] #[doc(cfg(feature = "sqlite"))] pub mod sqlite { @@ -34,7 +34,7 @@ pub mod sqlite { } } -/// Integraion with postgres database +/// Integration with postgres database #[cfg(feature = "postgres")] #[doc(cfg(feature = "postgres"))] pub mod postgres {