From cce3fa26212b32da9f53a631f27f0e83de6ca823 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Sun, 1 Dec 2024 21:33:48 +0100 Subject: [PATCH 1/6] feat: add TypeSpaceSettings::with_attr & TypeSpacePatch.attrs - which do nothing yet -, adjust output_enum --- typify-impl/src/lib.rs | 16 ++++++++++++++-- typify-impl/src/type_entry.rs | 13 +++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/typify-impl/src/lib.rs b/typify-impl/src/lib.rs index a1299cc9..9ab69066 100644 --- a/typify-impl/src/lib.rs +++ b/typify-impl/src/lib.rs @@ -242,6 +242,7 @@ pub(crate) enum DefaultImpl { pub struct TypeSpaceSettings { type_mod: Option, extra_derives: Vec, + extra_attrs: Vec, struct_builder: bool, unknown_crates: UnknownPolicy, @@ -307,8 +308,12 @@ impl CrateVers { /// Contains a set of modifications that may be applied to an existing type. #[derive(Debug, Default, Clone)] pub struct TypeSpacePatch { - rename: Option, - derives: Vec, + /// Rename the type + pub rename: Option, + /// Derives to add + pub derives: Vec, + /// Attributes to add + pub attrs: Vec, } /// Contains the attributes of a replacement of an existing type. @@ -365,6 +370,13 @@ impl TypeSpaceSettings { } self } + /// Add an additional attribute to apply to all defined types. + pub fn with_attr(&mut self, attr: String) -> &mut Self { + if !self.extra_attrs.contains(&attr) { + self.extra_attrs.push(attr); + } + self + } /// For structs, include a "builder" type that can be used to construct it. pub fn with_struct_builder(&mut self, struct_builder: bool) -> &mut Self { diff --git a/typify-impl/src/type_entry.rs b/typify-impl/src/type_entry.rs index 15b03a18..84742e02 100644 --- a/typify-impl/src/type_entry.rs +++ b/typify-impl/src/type_entry.rs @@ -666,6 +666,18 @@ impl TypeEntry { enum_details: &TypeEntryEnum, mut derive_set: BTreeSet<&str>, ) { + let extra_attrs: Vec = { + type_space + .settings + .extra_attrs + .clone() + .into_iter() + .map(|attr| { + let s: proc_macro2::TokenStream = attr.parse().unwrap(); + s + }) + .collect() + }; let TypeEntryEnum { name, rename, @@ -979,6 +991,7 @@ impl TypeEntry { let item = quote! { #doc #[derive(#(#derives),*)] + #(#extra_attrs)* #serde pub enum #type_name { #(#variants_decl)* From bd8fa007890c6da96f51c03e927acf2980c2276b Mon Sep 17 00:00:00 2001 From: elcoosp Date: Sun, 1 Dec 2024 21:37:06 +0100 Subject: [PATCH 2/6] fix: extract attrs_from_type_space fn --- typify-impl/src/type_entry.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/typify-impl/src/type_entry.rs b/typify-impl/src/type_entry.rs index 84742e02..2a38cd6c 100644 --- a/typify-impl/src/type_entry.rs +++ b/typify-impl/src/type_entry.rs @@ -666,18 +666,7 @@ impl TypeEntry { enum_details: &TypeEntryEnum, mut derive_set: BTreeSet<&str>, ) { - let extra_attrs: Vec = { - type_space - .settings - .extra_attrs - .clone() - .into_iter() - .map(|attr| { - let s: proc_macro2::TokenStream = attr.parse().unwrap(); - s - }) - .collect() - }; + let extra_attrs = attrs_from_type_space(type_space); let TypeEntryEnum { name, rename, @@ -1855,6 +1844,22 @@ impl TypeEntry { } } +fn attrs_from_type_space(type_space: &TypeSpace) -> Vec { + let extra_attrs: Vec = { + type_space + .settings + .extra_attrs + .clone() + .into_iter() + .map(|attr| { + let s: proc_macro2::TokenStream = attr.parse().unwrap(); + s + }) + .collect() + }; + extra_attrs +} + fn make_doc(name: &str, description: Option<&String>, schema: &Schema) -> TokenStream { let desc = description.map_or(name, |desc| desc.as_str()); let schema_json = serde_json::to_string_pretty(schema).unwrap(); From b0e42abcaea41b75191d51e0f7976c722346f453 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Sun, 1 Dec 2024 21:40:40 +0100 Subject: [PATCH 3/6] feat: add extra_attrs to output_newtype/struct --- typify-impl/src/type_entry.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/typify-impl/src/type_entry.rs b/typify-impl/src/type_entry.rs index 2a38cd6c..835fa9b3 100644 --- a/typify-impl/src/type_entry.rs +++ b/typify-impl/src/type_entry.rs @@ -1019,6 +1019,7 @@ impl TypeEntry { } = struct_details; let doc = make_doc(name, description.as_ref(), schema); + let extra_attrs = attrs_from_type_space(type_space); // Generate the serde directives as needed. let mut serde_options = Vec::new(); if let Some(old_name) = rename { @@ -1098,6 +1099,7 @@ impl TypeEntry { quote! { #doc #[derive(#(#derives),*)] + #(#extra_attrs)* #serde pub struct #type_name { #( @@ -1228,6 +1230,7 @@ impl TypeEntry { schema: SchemaWrapper(schema), } = newtype_details; let doc = make_doc(name, description.as_ref(), schema); + let extra_attrs = attrs_from_type_space(type_space); let serde = rename.as_ref().map(|old_name| { quote! { @@ -1525,6 +1528,7 @@ impl TypeEntry { let item = quote! { #doc #[derive(#(#derives),*)] + #(#extra_attrs)* #serde pub struct #type_name(#vis #inner_type_name); From bd3575dcd360beceabf1df3846361da086bb1468 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Mon, 2 Dec 2024 15:47:18 +0100 Subject: [PATCH 4/6] fix: mv extra_attrs after serde --- typify-impl/src/type_entry.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/typify-impl/src/type_entry.rs b/typify-impl/src/type_entry.rs index 835fa9b3..7db39ad0 100644 --- a/typify-impl/src/type_entry.rs +++ b/typify-impl/src/type_entry.rs @@ -980,8 +980,8 @@ impl TypeEntry { let item = quote! { #doc #[derive(#(#derives),*)] - #(#extra_attrs)* #serde + #(#extra_attrs)* pub enum #type_name { #(#variants_decl)* } @@ -1099,8 +1099,8 @@ impl TypeEntry { quote! { #doc #[derive(#(#derives),*)] - #(#extra_attrs)* #serde + #(#extra_attrs)* pub struct #type_name { #( #prop_doc @@ -1528,8 +1528,8 @@ impl TypeEntry { let item = quote! { #doc #[derive(#(#derives),*)] - #(#extra_attrs)* #serde + #(#extra_attrs)* pub struct #type_name(#vis #inner_type_name); impl ::std::ops::Deref for #type_name { From 4029dcd522fe656d225fad2e3b389a51bd3402f9 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Mon, 2 Dec 2024 16:23:18 +0100 Subject: [PATCH 5/6] chore: rm variable direct return in attrs_from_type_space map iteration --- typify-impl/src/type_entry.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/typify-impl/src/type_entry.rs b/typify-impl/src/type_entry.rs index 7db39ad0..2d853038 100644 --- a/typify-impl/src/type_entry.rs +++ b/typify-impl/src/type_entry.rs @@ -1855,10 +1855,7 @@ fn attrs_from_type_space(type_space: &TypeSpace) -> Vec { .extra_attrs .clone() .into_iter() - .map(|attr| { - let s: proc_macro2::TokenStream = attr.parse().unwrap(); - s - }) + .map(|attr| attr.parse::().unwrap()) .collect() }; extra_attrs From 80e2e1010463b2d561e50ca9340b10f1fc33cca4 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Mon, 2 Dec 2024 16:38:38 +0100 Subject: [PATCH 6/6] feat: add TypeSpacePatch::with_attr --- typify-impl/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/typify-impl/src/lib.rs b/typify-impl/src/lib.rs index 9ab69066..35e97d71 100644 --- a/typify-impl/src/lib.rs +++ b/typify-impl/src/lib.rs @@ -480,6 +480,11 @@ impl TypeSpacePatch { self.derives.push(derive.to_string()); self } + /// Specify an additional attribute to apply to the patched type. + pub fn with_attr(&mut self, attr: S) -> &mut Self { + self.attrs.push(attr.to_string()); + self + } } impl TypeSpace {