Skip to content
5 changes: 5 additions & 0 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>>,
}

#[derive(Debug, ValueEnum, Clone, PartialEq, Default)]
Expand Down Expand Up @@ -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,
},
},
)?;
Expand Down
11 changes: 9 additions & 2 deletions src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>`
pub fn to_rust_type(&self) -> Cow<str> {
pub fn to_rust_type(&self) -> Cow<'_, str> {
let mut rust_type = self.base_type.clone();

// order matters!
Expand Down Expand Up @@ -193,7 +193,14 @@ 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(|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]);

Expand Down
4 changes: 4 additions & 0 deletions src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ pub struct GenerationConfigOpts<'a> {
pub readonly_prefixes: Vec<String>,
/// Suffixes to treat tables as readonly
pub readonly_suffixes: Vec<String>,

/// Add these additional derives to each generated `struct`
pub additional_derives: Option<Vec<String>>,
}

impl GenerationConfigOpts<'_> {
Expand Down Expand Up @@ -363,6 +366,7 @@ impl Default for GenerationConfigOpts<'_> {
once_connection_type: false,
readonly_prefixes: Vec::default(),
readonly_suffixes: Vec::default(),
additional_derives: None,
}
}
}
Expand Down