From 05d91522c070f1c1d04c8f25eac6007e61b4b032 Mon Sep 17 00:00:00 2001 From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com> Date: Fri, 5 Sep 2025 11:17:09 -0500 Subject: [PATCH 1/6] [src/{bin/main,global,lib}.rs] Add new `additional_derives` vec CLI option ; [src/code.rs] Fix type elision warning --- src/bin/main.rs | 5 +++++ src/code.rs | 12 ++++++++++-- src/global.rs | 4 ++++ src/lib.rs | 1 + 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 731cad8e..13541d2b 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -136,6 +136,10 @@ pub struct MainOptions { /// See `crate::GenerationConfig::diesel_backend` for more details. #[arg(short = 'b', long = "diesel-backend")] pub diesel_backend: String, + + /// Add these additional derives to each generated `struct` + #[arg(short = 'd', long = "derive")] + pub additional_derives: Option>, } #[derive(Debug, ValueEnum, Clone, PartialEq, Default)] @@ -265,6 +269,7 @@ fn actual_main() -> dsync::Result<()> { once_connection_type: args.once_connection_type, readonly_prefixes: args.readonly_prefixes, readonly_suffixes: args.readonly_suffixes, + additional_derives: args.additional_derives }, }, )?; diff --git a/src/code.rs b/src/code.rs index 29367b5c..1d64d523 100644 --- a/src/code.rs +++ b/src/code.rs @@ -86,7 +86,7 @@ pub struct StructField { impl StructField { /// Assemble the current options into a rust type, like `base_type: String, is_optional: true` to `Option` - pub fn to_rust_type(&self) -> Cow { + pub fn to_rust_type(&self) -> Cow<'_, str> { let mut rust_type = self.base_type.clone(); // order matters! @@ -193,7 +193,15 @@ impl<'a> Struct<'a> { /// Assemble the `derive` attribute for the struct fn attr_derive(&self) -> String { - let mut derives_vec = Vec::with_capacity(10); + let mut derives_vec = match &self.config.options.additional_derives { + Some(v) => { + let mut _derives_vec = Vec::<&str>::with_capacity(10 + v.len()); + // _derives_vec.extend(v.iter().map(AsRef::as_ref).iter()) + _derives_vec.extend(v.iter().map(|s| -> &str { s.as_ref() })); + _derives_vec + }, + None => Vec::with_capacity(10) + }; // Default derives that exist on every struct derives_vec.extend_from_slice(&[derives::DEBUG, derives::CLONE]); diff --git a/src/global.rs b/src/global.rs index ad1eaa0c..30f2d1cd 100644 --- a/src/global.rs +++ b/src/global.rs @@ -328,6 +328,9 @@ pub struct GenerationConfigOpts<'a> { pub readonly_prefixes: Vec, /// Suffixes to treat tables as readonly pub readonly_suffixes: Vec, + + /// Add these additional derives to each generated `struct` + pub additional_derives: Option> } impl GenerationConfigOpts<'_> { @@ -363,6 +366,7 @@ impl Default for GenerationConfigOpts<'_> { once_connection_type: false, readonly_prefixes: Vec::default(), readonly_suffixes: Vec::default(), + additional_derives: None, } } } diff --git a/src/lib.rs b/src/lib.rs index 9274e2f9..5ed8ae1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(iter_collect_into)] //! dsync library //! //! The dsync library allows creating a custom binary for dsync From bcd16841cad8c35a9e577f03feb82b295beb018a Mon Sep 17 00:00:00 2001 From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com> Date: Fri, 5 Sep 2025 11:24:27 -0500 Subject: [PATCH 2/6] [src/code.rs] Remove comment ; [src/lib.rs] Remove `#![feature(iter_collect_into)]` --- src/code.rs | 1 - src/lib.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/code.rs b/src/code.rs index 1d64d523..2fe35cca 100644 --- a/src/code.rs +++ b/src/code.rs @@ -196,7 +196,6 @@ impl<'a> Struct<'a> { let mut derives_vec = match &self.config.options.additional_derives { Some(v) => { let mut _derives_vec = Vec::<&str>::with_capacity(10 + v.len()); - // _derives_vec.extend(v.iter().map(AsRef::as_ref).iter()) _derives_vec.extend(v.iter().map(|s| -> &str { s.as_ref() })); _derives_vec }, diff --git a/src/lib.rs b/src/lib.rs index 5ed8ae1f..9274e2f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -#![feature(iter_collect_into)] //! dsync library //! //! The dsync library allows creating a custom binary for dsync From 998f4d74a5f372757bb0ea293da4eff00d268702 Mon Sep 17 00:00:00 2001 From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com> Date: Fri, 5 Sep 2025 11:26:10 -0500 Subject: [PATCH 3/6] [src/{bin/main,global}.rs] `cargo fmt` --- src/bin/main.rs | 2 +- src/code.rs | 4 ++-- src/global.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 13541d2b..199daac1 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -269,7 +269,7 @@ fn actual_main() -> dsync::Result<()> { once_connection_type: args.once_connection_type, readonly_prefixes: args.readonly_prefixes, readonly_suffixes: args.readonly_suffixes, - additional_derives: args.additional_derives + additional_derives: args.additional_derives, }, }, )?; diff --git a/src/code.rs b/src/code.rs index 2fe35cca..3ba05d81 100644 --- a/src/code.rs +++ b/src/code.rs @@ -198,8 +198,8 @@ impl<'a> Struct<'a> { let mut _derives_vec = Vec::<&str>::with_capacity(10 + v.len()); _derives_vec.extend(v.iter().map(|s| -> &str { s.as_ref() })); _derives_vec - }, - None => Vec::with_capacity(10) + } + None => Vec::with_capacity(10), }; // Default derives that exist on every struct derives_vec.extend_from_slice(&[derives::DEBUG, derives::CLONE]); diff --git a/src/global.rs b/src/global.rs index 30f2d1cd..bf5c4506 100644 --- a/src/global.rs +++ b/src/global.rs @@ -330,7 +330,7 @@ pub struct GenerationConfigOpts<'a> { pub readonly_suffixes: Vec, /// Add these additional derives to each generated `struct` - pub additional_derives: Option> + pub additional_derives: Option>, } impl GenerationConfigOpts<'_> { From b07fb7b4120adf426d523d7aa6b833aaa31ecbec Mon Sep 17 00:00:00 2001 From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:54:12 -0500 Subject: [PATCH 4/6] [src/{bin/main,global}.rs] Use vec over option --- src/bin/main.rs | 2 +- src/code.rs | 20 +++++++++++++------- src/global.rs | 4 ++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 199daac1..34f05a2b 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -269,7 +269,7 @@ fn actual_main() -> dsync::Result<()> { once_connection_type: args.once_connection_type, readonly_prefixes: args.readonly_prefixes, readonly_suffixes: args.readonly_suffixes, - additional_derives: args.additional_derives, + additional_derives: args.additional_derives.unwrap_or(Default::default()), }, }, )?; diff --git a/src/code.rs b/src/code.rs index 3ba05d81..ffa040f2 100644 --- a/src/code.rs +++ b/src/code.rs @@ -193,13 +193,19 @@ impl<'a> Struct<'a> { /// Assemble the `derive` attribute for the struct fn attr_derive(&self) -> String { - let mut derives_vec = match &self.config.options.additional_derives { - Some(v) => { - let mut _derives_vec = Vec::<&str>::with_capacity(10 + v.len()); - _derives_vec.extend(v.iter().map(|s| -> &str { s.as_ref() })); - _derives_vec - } - None => Vec::with_capacity(10), + let mut derives_vec = if self.config.options.additional_derives.is_empty() { + Vec::with_capacity(10) + } else { + let mut _derives_vec = + Vec::<&str>::with_capacity(10 + self.config.options.additional_derives.len()); + _derives_vec.extend( + self.config + .options + .additional_derives + .iter() + .map(|s| -> &str { s.as_ref() }), + ); + _derives_vec }; // Default derives that exist on every struct derives_vec.extend_from_slice(&[derives::DEBUG, derives::CLONE]); diff --git a/src/global.rs b/src/global.rs index bf5c4506..d7c6fe77 100644 --- a/src/global.rs +++ b/src/global.rs @@ -330,7 +330,7 @@ pub struct GenerationConfigOpts<'a> { pub readonly_suffixes: Vec, /// Add these additional derives to each generated `struct` - pub additional_derives: Option>, + pub additional_derives: Vec, } impl GenerationConfigOpts<'_> { @@ -366,7 +366,7 @@ impl Default for GenerationConfigOpts<'_> { once_connection_type: false, readonly_prefixes: Vec::default(), readonly_suffixes: Vec::default(), - additional_derives: None, + additional_derives: Vec::default(), } } } From fc936a717e48bcd0cc14008cb4b955d2a62b92f3 Mon Sep 17 00:00:00 2001 From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com> Date: Sat, 6 Sep 2025 12:28:23 -0500 Subject: [PATCH 5/6] [src/code.rs] Reshuffle vec construction --- src/code.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/code.rs b/src/code.rs index ffa040f2..6b1d8267 100644 --- a/src/code.rs +++ b/src/code.rs @@ -193,20 +193,17 @@ impl<'a> Struct<'a> { /// Assemble the `derive` attribute for the struct fn attr_derive(&self) -> String { - let mut derives_vec = if self.config.options.additional_derives.is_empty() { - Vec::with_capacity(10) - } else { - let mut _derives_vec = - Vec::<&str>::with_capacity(10 + self.config.options.additional_derives.len()); - _derives_vec.extend( + let mut derives_vec = + Vec::<&str>::with_capacity(10 + self.config.options.additional_derives.len()); + if !self.config.options.additional_derives.is_empty() { + derives_vec.extend( self.config .options .additional_derives .iter() .map(|s| -> &str { s.as_ref() }), ); - _derives_vec - }; + } // Default derives that exist on every struct derives_vec.extend_from_slice(&[derives::DEBUG, derives::CLONE]); From e2aecd9959adb781bd8196f587a5fbdb683941d8 Mon Sep 17 00:00:00 2001 From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com> Date: Sat, 6 Sep 2025 12:40:58 -0500 Subject: [PATCH 6/6] [src/bin/main.rs] Remove `Option` on `additional_derives` --- src/bin/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 34f05a2b..03a8b72d 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -139,7 +139,7 @@ pub struct MainOptions { /// Add these additional derives to each generated `struct` #[arg(short = 'd', long = "derive")] - pub additional_derives: Option>, + pub additional_derives: Vec, } #[derive(Debug, ValueEnum, Clone, PartialEq, Default)] @@ -269,7 +269,7 @@ fn actual_main() -> dsync::Result<()> { once_connection_type: args.once_connection_type, readonly_prefixes: args.readonly_prefixes, readonly_suffixes: args.readonly_suffixes, - additional_derives: args.additional_derives.unwrap_or(Default::default()), + additional_derives: args.additional_derives, }, }, )?;