From 7284a2573b500e0457243e9cc1e870d3c338f3b0 Mon Sep 17 00:00:00 2001 From: slim Date: Thu, 24 Oct 2024 15:56:38 +0100 Subject: [PATCH 1/8] feat: add extra derives option --- README.md | 1 + src/gen.rs | 11 +++++++++++ src/main.rs | 5 +++++ src/templates.rs | 7 ++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c2f37e5..2e37c8d 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Options: --chrono-dates Use chrono::NaiveDateTime for date/timestamps logical types --derive-builders Derive builders for generated record structs --derive-schemas Derive AvroSchema for generated record structs + --extra_derives Append extra derive macros to the generated record structs -h, --help Print help -V, --version Print version ``` diff --git a/src/gen.rs b/src/gen.rs index 01c70dc..65f7a59 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -285,6 +285,7 @@ pub struct GeneratorBuilder { use_chrono_dates: bool, derive_builders: bool, derive_schemas: bool, + extra_derives: Vec, } impl Default for GeneratorBuilder { @@ -296,6 +297,7 @@ impl Default for GeneratorBuilder { use_chrono_dates: false, derive_builders: false, derive_schemas: false, + extra_derives: vec![], } } } @@ -351,6 +353,14 @@ impl GeneratorBuilder { self } + /// Adds support to derive custom macros. + /// + /// Applies to record structs. + pub fn extra_derives(mut self, extra_derives: Vec) -> GeneratorBuilder { + self.extra_derives = extra_derives; + self + } + /// Create a [`Generator`](Generator) with the builder parameters. pub fn build(self) -> Result { let mut templater = Templater::new()?; @@ -360,6 +370,7 @@ impl GeneratorBuilder { templater.use_chrono_dates = self.use_chrono_dates; templater.derive_builders = self.derive_builders; templater.derive_schemas = self.derive_schemas; + templater.extra_derives = self.extra_derives; Ok(Generator { templater }) } } diff --git a/src/main.rs b/src/main.rs index 1283c69..487da15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,10 @@ struct Args { /// Derive AvroSchema for generated record structs #[clap(long)] pub derive_schemas: bool, + + /// Extract Derives for generated record structs + #[clap(long)] + pub extra_derives: Vec, } fn run() -> Result<(), Box> { @@ -70,6 +74,7 @@ fn run() -> Result<(), Box> { .use_chrono_dates(args.chrono_dates) .derive_builders(args.derive_builders) .derive_schemas(args.derive_schemas) + .extra_derives(args.extra_derives) .build()?; g.gen(&source, &mut out)?; diff --git a/src/templates.rs b/src/templates.rs index 7861269..3d1b604 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -25,7 +25,7 @@ pub const RECORD_TEMPLATE: &str = r#" /// {{ doc_line }} {%- endfor %} {%- endif %} -#[derive(Debug, PartialEq{%- if is_eq_derivable %}, Eq{%- endif %}, Clone, serde::Deserialize, serde::Serialize{%- if derive_builders %}, derive_builder::Builder {%- endif %}{%- if derive_schemas %}, apache_avro::AvroSchema {%- endif %})] +#[derive(Debug, PartialEq{%- if is_eq_derivable %}, Eq{%- endif %}, Clone, serde::Deserialize, serde::Serialize{%- if derive_builders %}, derive_builder::Builder {%- endif %}{%- if derive_schemas %}, apache_avro::AvroSchema {%- endif %} {%- if extra_derives %},{{ extra_derives}} {%- endif %})] {%- if derive_builders %} #[builder(setter(into))] {%- endif %} @@ -509,6 +509,7 @@ pub struct Templater { pub use_chrono_dates: bool, pub derive_builders: bool, pub derive_schemas: bool, + pub extra_derives: Vec, } impl Templater { @@ -530,6 +531,7 @@ impl Templater { use_chrono_dates: false, derive_builders: false, derive_schemas: false, + extra_derives: vec![], }) } @@ -599,6 +601,9 @@ impl Templater { ctx.insert("doc", doc); ctx.insert("derive_builders", &self.derive_builders); ctx.insert("derive_schemas", &self.derive_schemas); + if !self.extra_derives.is_empty() { + ctx.insert("extra_derives", &self.extra_derives.join(", ")); + } let mut f = Vec::new(); // field names; let mut t = HashMap::new(); // field name -> field type From 292faa54c5fd0a4e8fc13f8d252e2b4098bc9d20 Mon Sep 17 00:00:00 2001 From: slim Date: Thu, 24 Oct 2024 18:42:01 +0100 Subject: [PATCH 2/8] feat: add tests --- tests/generation.rs | 25 +++++++++++++++++++++++++ tests/schemas/one_extra_derive.avsc | 8 ++++++++ tests/schemas/one_extra_derive.rs | 10 ++++++++++ tests/schemas/two_extra_derives.avsc | 8 ++++++++ tests/schemas/two_extra_derives.rs | 10 ++++++++++ 5 files changed, 61 insertions(+) create mode 100644 tests/schemas/one_extra_derive.avsc create mode 100644 tests/schemas/one_extra_derive.rs create mode 100644 tests/schemas/two_extra_derives.avsc create mode 100644 tests/schemas/two_extra_derives.rs diff --git a/tests/generation.rs b/tests/generation.rs index 87f6e0b..3cacc86 100644 --- a/tests/generation.rs +++ b/tests/generation.rs @@ -230,3 +230,28 @@ fn gen_recursive() { fn gen_interop() { validate_generation("interop", Generator::new().unwrap()); } + +#[test] +fn gen_one_extra_derives() { + validate_generation( + "one_extra_derive", + Generator::builder() + .extra_derives(vec!["std::fmt::Display".to_string()]) + .build() + .unwrap(), + ); +} + +#[test] +fn gen_two_extra_derives() { + validate_generation( + "two_extra_derives", + Generator::builder() + .extra_derives(vec![ + "std::fmt::Display".to_string(), + "std::string::ToString".to_string(), + ]) + .build() + .unwrap(), + ); +} diff --git a/tests/schemas/one_extra_derive.avsc b/tests/schemas/one_extra_derive.avsc new file mode 100644 index 0000000..133fb5c --- /dev/null +++ b/tests/schemas/one_extra_derive.avsc @@ -0,0 +1,8 @@ +{ + "type": "record", + "name": "test", + "fields": [ + {"name": "a", "type": "long", "default": 42}, + {"name": "b", "type": "string"} + ] +} diff --git a/tests/schemas/one_extra_derive.rs b/tests/schemas/one_extra_derive.rs new file mode 100644 index 0000000..f526200 --- /dev/null +++ b/tests/schemas/one_extra_derive.rs @@ -0,0 +1,10 @@ + +#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize,std::fmt::Display)] +pub struct Test { + #[serde(default = "default_test_a")] + pub a: i64, + pub b: String, +} + +#[inline(always)] +fn default_test_a() -> i64 { 42 } diff --git a/tests/schemas/two_extra_derives.avsc b/tests/schemas/two_extra_derives.avsc new file mode 100644 index 0000000..133fb5c --- /dev/null +++ b/tests/schemas/two_extra_derives.avsc @@ -0,0 +1,8 @@ +{ + "type": "record", + "name": "test", + "fields": [ + {"name": "a", "type": "long", "default": 42}, + {"name": "b", "type": "string"} + ] +} diff --git a/tests/schemas/two_extra_derives.rs b/tests/schemas/two_extra_derives.rs new file mode 100644 index 0000000..0bb9fd5 --- /dev/null +++ b/tests/schemas/two_extra_derives.rs @@ -0,0 +1,10 @@ + +#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize,std::fmt::Display, std::string::ToString)] +pub struct Test { + #[serde(default = "default_test_a")] + pub a: i64, + pub b: String, +} + +#[inline(always)] +fn default_test_a() -> i64 { 42 } From 1c4fa157505437b04d1f077def1fe6a0105b1d8d Mon Sep 17 00:00:00 2001 From: slim Date: Thu, 24 Oct 2024 18:46:55 +0100 Subject: [PATCH 3/8] feat: add tests --- src/gen.rs | 4 ++-- src/templates.rs | 2 +- tests/generation.rs | 7 ++----- tests/schemas/one_extra_derive.rs | 2 +- tests/schemas/two_extra_derives.rs | 2 +- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/gen.rs b/src/gen.rs index 65f7a59..6d59002 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -356,8 +356,8 @@ impl GeneratorBuilder { /// Adds support to derive custom macros. /// /// Applies to record structs. - pub fn extra_derives(mut self, extra_derives: Vec) -> GeneratorBuilder { - self.extra_derives = extra_derives; + pub fn extra_derives(mut self, extra_derives: Vec<&str>) -> GeneratorBuilder { + self.extra_derives = extra_derives.into_iter().map(String::from).collect(); self } diff --git a/src/templates.rs b/src/templates.rs index 3d1b604..998f745 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -25,7 +25,7 @@ pub const RECORD_TEMPLATE: &str = r#" /// {{ doc_line }} {%- endfor %} {%- endif %} -#[derive(Debug, PartialEq{%- if is_eq_derivable %}, Eq{%- endif %}, Clone, serde::Deserialize, serde::Serialize{%- if derive_builders %}, derive_builder::Builder {%- endif %}{%- if derive_schemas %}, apache_avro::AvroSchema {%- endif %} {%- if extra_derives %},{{ extra_derives}} {%- endif %})] +#[derive(Debug, PartialEq{%- if is_eq_derivable %}, Eq{%- endif %}, Clone, serde::Deserialize, serde::Serialize{%- if derive_builders %}, derive_builder::Builder {%- endif %}{%- if derive_schemas %}, apache_avro::AvroSchema {%- endif %} {%- if extra_derives %}, {{ extra_derives}} {%- endif %})] {%- if derive_builders %} #[builder(setter(into))] {%- endif %} diff --git a/tests/generation.rs b/tests/generation.rs index 3cacc86..71826aa 100644 --- a/tests/generation.rs +++ b/tests/generation.rs @@ -236,7 +236,7 @@ fn gen_one_extra_derives() { validate_generation( "one_extra_derive", Generator::builder() - .extra_derives(vec!["std::fmt::Display".to_string()]) + .extra_derives(vec!["std::fmt::Display"]) .build() .unwrap(), ); @@ -247,10 +247,7 @@ fn gen_two_extra_derives() { validate_generation( "two_extra_derives", Generator::builder() - .extra_derives(vec![ - "std::fmt::Display".to_string(), - "std::string::ToString".to_string(), - ]) + .extra_derives(vec!["std::fmt::Display", "std::string::ToString"]) .build() .unwrap(), ); diff --git a/tests/schemas/one_extra_derive.rs b/tests/schemas/one_extra_derive.rs index f526200..40a0aa4 100644 --- a/tests/schemas/one_extra_derive.rs +++ b/tests/schemas/one_extra_derive.rs @@ -1,5 +1,5 @@ -#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize,std::fmt::Display)] +#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, std::fmt::Display)] pub struct Test { #[serde(default = "default_test_a")] pub a: i64, diff --git a/tests/schemas/two_extra_derives.rs b/tests/schemas/two_extra_derives.rs index 0bb9fd5..612998c 100644 --- a/tests/schemas/two_extra_derives.rs +++ b/tests/schemas/two_extra_derives.rs @@ -1,5 +1,5 @@ -#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize,std::fmt::Display, std::string::ToString)] +#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, std::fmt::Display, std::string::ToString)] pub struct Test { #[serde(default = "default_test_a")] pub a: i64, From d5d795675f1645ed34e4d5f667f244c370620185 Mon Sep 17 00:00:00 2001 From: slim Date: Thu, 24 Oct 2024 18:52:19 +0100 Subject: [PATCH 4/8] feat: add tests --- src/gen.rs | 4 ++-- tests/generation.rs | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/gen.rs b/src/gen.rs index 6d59002..65f7a59 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -356,8 +356,8 @@ impl GeneratorBuilder { /// Adds support to derive custom macros. /// /// Applies to record structs. - pub fn extra_derives(mut self, extra_derives: Vec<&str>) -> GeneratorBuilder { - self.extra_derives = extra_derives.into_iter().map(String::from).collect(); + pub fn extra_derives(mut self, extra_derives: Vec) -> GeneratorBuilder { + self.extra_derives = extra_derives; self } diff --git a/tests/generation.rs b/tests/generation.rs index 71826aa..3cacc86 100644 --- a/tests/generation.rs +++ b/tests/generation.rs @@ -236,7 +236,7 @@ fn gen_one_extra_derives() { validate_generation( "one_extra_derive", Generator::builder() - .extra_derives(vec!["std::fmt::Display"]) + .extra_derives(vec!["std::fmt::Display".to_string()]) .build() .unwrap(), ); @@ -247,7 +247,10 @@ fn gen_two_extra_derives() { validate_generation( "two_extra_derives", Generator::builder() - .extra_derives(vec!["std::fmt::Display", "std::string::ToString"]) + .extra_derives(vec![ + "std::fmt::Display".to_string(), + "std::string::ToString".to_string(), + ]) .build() .unwrap(), ); From f6c08e0e0cc2b18c12e2fca8646001aaa46273de Mon Sep 17 00:00:00 2001 From: slim Date: Thu, 24 Oct 2024 19:16:58 +0100 Subject: [PATCH 5/8] feat: add tests --- README.md | 3 ++- src/gen.rs | 8 ++++++++ tests/generation.rs | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2e37c8d..3a1f24b 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ Options: --chrono-dates Use chrono::NaiveDateTime for date/timestamps logical types --derive-builders Derive builders for generated record structs --derive-schemas Derive AvroSchema for generated record structs - --extra_derives Append extra derive macros to the generated record structs + --extra_derive Append one extra derive macros to the generated record structs + --extra_derives Append extra derive macros list to the generated record structs -h, --help Print help -V, --version Print version ``` diff --git a/src/gen.rs b/src/gen.rs index 65f7a59..1bce0f9 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -361,6 +361,14 @@ impl GeneratorBuilder { self } + /// Adds support to derive custom macros. + /// + /// Applies to record structs. + pub fn extra_derive(mut self, extra_derive: &str) -> GeneratorBuilder { + self.extra_derives.push(extra_derive.to_string()); + self + } + /// Create a [`Generator`](Generator) with the builder parameters. pub fn build(self) -> Result { let mut templater = Templater::new()?; diff --git a/tests/generation.rs b/tests/generation.rs index 3cacc86..daad2e0 100644 --- a/tests/generation.rs +++ b/tests/generation.rs @@ -236,7 +236,7 @@ fn gen_one_extra_derives() { validate_generation( "one_extra_derive", Generator::builder() - .extra_derives(vec!["std::fmt::Display".to_string()]) + .extra_derive("std::fmt::Display") .build() .unwrap(), ); From bacc475a4774b15f4fd44e97bbbcae2639165f2c Mon Sep 17 00:00:00 2001 From: slim Date: Fri, 25 Oct 2024 08:54:17 +0100 Subject: [PATCH 6/8] feat: add extra derives --- README.md | 1 - src/gen.rs | 8 -------- tests/generation.rs | 2 +- 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/README.md b/README.md index 3a1f24b..29dec78 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,6 @@ Options: --chrono-dates Use chrono::NaiveDateTime for date/timestamps logical types --derive-builders Derive builders for generated record structs --derive-schemas Derive AvroSchema for generated record structs - --extra_derive Append one extra derive macros to the generated record structs --extra_derives Append extra derive macros list to the generated record structs -h, --help Print help -V, --version Print version diff --git a/src/gen.rs b/src/gen.rs index 1bce0f9..65f7a59 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -361,14 +361,6 @@ impl GeneratorBuilder { self } - /// Adds support to derive custom macros. - /// - /// Applies to record structs. - pub fn extra_derive(mut self, extra_derive: &str) -> GeneratorBuilder { - self.extra_derives.push(extra_derive.to_string()); - self - } - /// Create a [`Generator`](Generator) with the builder parameters. pub fn build(self) -> Result { let mut templater = Templater::new()?; diff --git a/tests/generation.rs b/tests/generation.rs index daad2e0..3cacc86 100644 --- a/tests/generation.rs +++ b/tests/generation.rs @@ -236,7 +236,7 @@ fn gen_one_extra_derives() { validate_generation( "one_extra_derive", Generator::builder() - .extra_derive("std::fmt::Display") + .extra_derives(vec!["std::fmt::Display".to_string()]) .build() .unwrap(), ); From f7447c4f316eec5bed29aa94c2685f7b888b2bb4 Mon Sep 17 00:00:00 2001 From: slim Date: Fri, 25 Oct 2024 10:24:15 +0200 Subject: [PATCH 7/8] Update src/main.rs Co-authored-by: Martin Grigorov --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 487da15..16d9fad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,7 +45,7 @@ struct Args { #[clap(long)] pub derive_schemas: bool, - /// Extract Derives for generated record structs + /// Extract Derives for generated record structs, comma separated, e.g. `...` #[clap(long)] pub extra_derives: Vec, } From 57ca634e5bac4ac30194ba78bbced4aa627d2d38 Mon Sep 17 00:00:00 2001 From: slim Date: Fri, 25 Oct 2024 09:28:19 +0100 Subject: [PATCH 8/8] feat: add extra derives --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 16d9fad..7400d2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,8 +45,8 @@ struct Args { #[clap(long)] pub derive_schemas: bool, - /// Extract Derives for generated record structs, comma separated, e.g. `...` - #[clap(long)] + /// Extract Derives for generated record structs, comma separated, e.g. `std::fmt::Display,std::string::ToString` + #[clap(long, value_delimiter = ',')] pub extra_derives: Vec, }