Skip to content

Commit 6894c1d

Browse files
committed
feat: Add basic generics support for enums
1 parent 01f5245 commit 6894c1d

File tree

1 file changed

+18
-4
lines changed
  • crates/stackable-versioned-macros/src/codegen/container

1 file changed

+18
-4
lines changed

crates/stackable-versioned-macros/src/codegen/container/enum.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::Not;
33
use darling::{util::IdentString, FromAttributes, Result};
44
use proc_macro2::TokenStream;
55
use quote::quote;
6-
use syn::ItemEnum;
6+
use syn::{Generics, ItemEnum};
77

88
use crate::{
99
attrs::container::NestedContainerAttributes,
@@ -46,6 +46,7 @@ impl Container {
4646
};
4747

4848
Ok(Self::Enum(Enum {
49+
generics: item_enum.generics,
4950
variants: versioned_variants,
5051
common,
5152
}))
@@ -82,6 +83,7 @@ impl Container {
8283
};
8384

8485
Ok(Self::Enum(Enum {
86+
generics: item_enum.generics,
8587
variants: versioned_variants,
8688
common,
8789
}))
@@ -96,12 +98,16 @@ pub(crate) struct Enum {
9698

9799
/// Common container data which is shared between enums and structs.
98100
pub(crate) common: CommonContainerData,
101+
102+
/// Generic types of the enum
103+
pub generics: Generics,
99104
}
100105

101106
// Common token generation
102107
impl Enum {
103108
/// Generates code for the enum definition.
104109
pub(crate) fn generate_definition(&self, version: &VersionDefinition) -> TokenStream {
110+
let (_, type_generics, where_clause) = self.generics.split_for_impl();
105111
let original_attributes = &self.common.original_attributes;
106112
let ident = &self.common.idents.original;
107113
let version_docs = &version.docs;
@@ -114,7 +120,7 @@ impl Enum {
114120
quote! {
115121
#(#[doc = #version_docs])*
116122
#(#original_attributes)*
117-
pub enum #ident {
123+
pub enum #ident #type_generics #where_clause {
118124
#variants
119125
}
120126
}
@@ -133,6 +139,12 @@ impl Enum {
133139

134140
match next_version {
135141
Some(next_version) => {
142+
// TODO (@Techassi): Support generic types which have been removed in newer versions,
143+
// but need to exist for older versions How do we represent that? Because the
144+
// defined struct always represents the latest version. I guess we could generally
145+
// advise against using generic types, but if you have to, avoid removing it in
146+
// later versions.
147+
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
136148
let enum_ident = &self.common.idents.original;
137149
let from_ident = &self.common.idents.from;
138150

@@ -158,8 +170,10 @@ impl Enum {
158170
Some(quote! {
159171
#automatically_derived
160172
#allow_attribute
161-
impl ::std::convert::From<#version_ident::#enum_ident> for #next_version_ident::#enum_ident {
162-
fn from(#from_ident: #version_ident::#enum_ident) -> Self {
173+
impl #impl_generics ::std::convert::From<#version_ident::#enum_ident #type_generics> for #next_version_ident::#enum_ident #type_generics
174+
#where_clause
175+
{
176+
fn from(#from_ident: #version_ident::#enum_ident #type_generics) -> Self {
163177
match #from_ident {
164178
#variants
165179
}

0 commit comments

Comments
 (0)