Skip to content

Commit

Permalink
Versionize enum unit variant
Browse files Browse the repository at this point in the history
Signed-off-by: Ioana Chirca <[email protected]>
  • Loading branch information
ioanachirca committed Jun 25, 2020
1 parent 8012e26 commit af4d79f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v0.1.1

- Removed "versionize" dependency.
- Added support for (de)serializing enum variants with zero or more fields.

# v0.1.0

- "versionize_derive" v0.1.0 first release.
27 changes: 20 additions & 7 deletions src/fields/enum_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ impl EnumVariant {

if !self.exists_at(target_version) {
if let Some(default_fn_ident) = get_ident_attr(&self.attrs, DEFAULT_FN) {
return quote! {
Self::#field_ident(..) => {
// Call user defined fn to provide a variant that exists in target version.
let new_variant = self.#default_fn_ident(version)?;
// The new_variant will serialize it's index and data.
new_variant.serialize(writer, version_map, app_version)?;
},
let field_type_ident = if self.ty.is_empty() {
quote! { Self::#field_ident => }
} else {
quote! { Self::#field_ident(..) => }
};

let mut serializer = proc_macro2::TokenStream::new();
serializer.extend(field_type_ident);
serializer.extend(self.default_fn_serializer(default_fn_ident));
return serializer;
} else {
panic!("Variant {} does not exist in version {}, please implement a default_fn function that provides a default value for this variant.", field_ident.to_string(), target_version);
}
Expand Down Expand Up @@ -136,4 +138,15 @@ impl EnumVariant {
},
};
}

fn default_fn_serializer(&self, default_fn_ident: syn::Ident) -> proc_macro2::TokenStream {
quote! {
{
// Call user defined fn to provide a variant that exists in target version.
let new_variant = self.#default_fn_ident(version)?;
// The new_variant will serialize its index and data.
new_variant.serialize(writer, version_map, app_version)?;
},
}
}
}

0 comments on commit af4d79f

Please sign in to comment.