Skip to content

WIP: extra_attrs #707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ pub(crate) enum DefaultImpl {
pub struct TypeSpaceSettings {
type_mod: Option<String>,
extra_derives: Vec<String>,
extra_attrs: Vec<String>,
struct_builder: bool,

unknown_crates: UnknownPolicy,
Expand Down Expand Up @@ -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<String>,
derives: Vec<String>,
/// Rename the type
pub rename: Option<String>,
/// Derives to add
pub derives: Vec<String>,
/// Attributes to add
pub attrs: Vec<String>,
}

/// Contains the attributes of a replacement of an existing type.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -468,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<S: ToString>(&mut self, attr: S) -> &mut Self {
self.attrs.push(attr.to_string());
self
}
}

impl TypeSpace {
Expand Down
19 changes: 19 additions & 0 deletions typify-impl/src/type_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ impl TypeEntry {
enum_details: &TypeEntryEnum,
mut derive_set: BTreeSet<&str>,
) {
let extra_attrs = attrs_from_type_space(type_space);
let TypeEntryEnum {
name,
rename,
Expand Down Expand Up @@ -980,6 +981,7 @@ impl TypeEntry {
#doc
#[derive(#(#derives),*)]
#serde
#(#extra_attrs)*
pub enum #type_name {
#(#variants_decl)*
}
Expand Down Expand Up @@ -1017,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 {
Expand Down Expand Up @@ -1097,6 +1100,7 @@ impl TypeEntry {
#doc
#[derive(#(#derives),*)]
#serde
#(#extra_attrs)*
pub struct #type_name {
#(
#prop_doc
Expand Down Expand Up @@ -1226,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! {
Expand Down Expand Up @@ -1524,6 +1529,7 @@ impl TypeEntry {
#doc
#[derive(#(#derives),*)]
#serde
#(#extra_attrs)*
pub struct #type_name(#vis #inner_type_name);

impl ::std::ops::Deref for #type_name {
Expand Down Expand Up @@ -1842,6 +1848,19 @@ impl TypeEntry {
}
}

fn attrs_from_type_space(type_space: &TypeSpace) -> Vec<TokenStream> {
let extra_attrs: Vec<TokenStream> = {
type_space
.settings
.extra_attrs
.clone()
.into_iter()
.map(|attr| attr.parse::<proc_macro2::TokenStream>().unwrap())
.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();
Expand Down