Skip to content

Commit 05d9152

Browse files
committed
[src/{bin/main,global,lib}.rs] Add new additional_derives vec CLI option ; [src/code.rs] Fix type elision warning
1 parent 2797966 commit 05d9152

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/bin/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ pub struct MainOptions {
136136
/// See `crate::GenerationConfig::diesel_backend` for more details.
137137
#[arg(short = 'b', long = "diesel-backend")]
138138
pub diesel_backend: String,
139+
140+
/// Add these additional derives to each generated `struct`
141+
#[arg(short = 'd', long = "derive")]
142+
pub additional_derives: Option<Vec<String>>,
139143
}
140144

141145
#[derive(Debug, ValueEnum, Clone, PartialEq, Default)]
@@ -265,6 +269,7 @@ fn actual_main() -> dsync::Result<()> {
265269
once_connection_type: args.once_connection_type,
266270
readonly_prefixes: args.readonly_prefixes,
267271
readonly_suffixes: args.readonly_suffixes,
272+
additional_derives: args.additional_derives
268273
},
269274
},
270275
)?;

src/code.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub struct StructField {
8686

8787
impl StructField {
8888
/// Assemble the current options into a rust type, like `base_type: String, is_optional: true` to `Option<String>`
89-
pub fn to_rust_type(&self) -> Cow<str> {
89+
pub fn to_rust_type(&self) -> Cow<'_, str> {
9090
let mut rust_type = self.base_type.clone();
9191

9292
// order matters!
@@ -193,7 +193,15 @@ impl<'a> Struct<'a> {
193193

194194
/// Assemble the `derive` attribute for the struct
195195
fn attr_derive(&self) -> String {
196-
let mut derives_vec = Vec::with_capacity(10);
196+
let mut derives_vec = match &self.config.options.additional_derives {
197+
Some(v) => {
198+
let mut _derives_vec = Vec::<&str>::with_capacity(10 + v.len());
199+
// _derives_vec.extend(v.iter().map(AsRef::as_ref).iter())
200+
_derives_vec.extend(v.iter().map(|s| -> &str { s.as_ref() }));
201+
_derives_vec
202+
},
203+
None => Vec::with_capacity(10)
204+
};
197205
// Default derives that exist on every struct
198206
derives_vec.extend_from_slice(&[derives::DEBUG, derives::CLONE]);
199207

src/global.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ pub struct GenerationConfigOpts<'a> {
328328
pub readonly_prefixes: Vec<String>,
329329
/// Suffixes to treat tables as readonly
330330
pub readonly_suffixes: Vec<String>,
331+
332+
/// Add these additional derives to each generated `struct`
333+
pub additional_derives: Option<Vec<String>>
331334
}
332335

333336
impl GenerationConfigOpts<'_> {
@@ -363,6 +366,7 @@ impl Default for GenerationConfigOpts<'_> {
363366
once_connection_type: false,
364367
readonly_prefixes: Vec::default(),
365368
readonly_suffixes: Vec::default(),
369+
additional_derives: None,
366370
}
367371
}
368372
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(iter_collect_into)]
12
//! dsync library
23
//!
34
//! The dsync library allows creating a custom binary for dsync

0 commit comments

Comments
 (0)