From af4d79fdebce2e5f90e4b304002973b8b6c4a22e Mon Sep 17 00:00:00 2001 From: Ioana Chirca Date: Mon, 22 Jun 2020 16:12:47 +0300 Subject: [PATCH] Versionize enum unit variant Signed-off-by: Ioana Chirca --- CHANGELOG.md | 5 +++++ src/fields/enum_variant.rs | 27 ++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6aa9806..7a20156 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/fields/enum_variant.rs b/src/fields/enum_variant.rs index 5050560..0034958 100644 --- a/src/fields/enum_variant.rs +++ b/src/fields/enum_variant.rs @@ -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); } @@ -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)?; + }, + } + } }