Skip to content

Commit 8253c2b

Browse files
committed
refactor(stackable-versioned): Rework version enum for merged_crd
1 parent 34c6c4f commit 8253c2b

File tree

2 files changed

+25
-35
lines changed
  • crates/stackable-versioned-macros

2 files changed

+25
-35
lines changed

crates/stackable-versioned-macros/src/codegen/vstruct/mod.rs

+24-34
Original file line numberDiff line numberDiff line change
@@ -319,22 +319,40 @@ impl VersionedStruct {
319319
crd_fn_calls: Vec<TokenStream>,
320320
enum_variants: Vec<(Ident, String)>,
321321
) -> TokenStream {
322-
let ident = &self.idents.kubernetes;
322+
let enum_ident = &self.idents.kubernetes;
323+
let enum_vis = &self.visibility;
323324

324-
let version_enum_definition = self.generate_kubernetes_version_enum(enum_variants);
325+
let mut enum_variant_matches = TokenStream::new();
326+
let mut enum_variant_idents = TokenStream::new();
327+
328+
for (enum_variant_ident, enum_variant_display) in enum_variants {
329+
enum_variant_idents.extend(quote! {#enum_variant_ident,});
330+
enum_variant_matches.extend(quote! {
331+
#enum_ident::#enum_variant_ident => f.write_str(#enum_variant_display),
332+
});
333+
}
325334

326335
quote! {
327336
#[automatically_derived]
328-
pub struct #ident;
337+
#enum_vis enum #enum_ident {
338+
#enum_variant_idents
339+
}
329340

330-
#version_enum_definition
341+
#[automatically_derived]
342+
impl ::std::fmt::Display for #enum_ident {
343+
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::result::Result<(), ::std::fmt::Error> {
344+
match self {
345+
#enum_variant_matches
346+
}
347+
}
348+
}
331349

332350
#[automatically_derived]
333-
impl #ident {
351+
impl #enum_ident {
334352
/// Generates a merged CRD which contains all versions defined using the
335353
/// `#[versioned()]` macro.
336354
pub fn merged_crd(
337-
stored_apiversion: Version
355+
stored_apiversion: Self
338356
) -> ::std::result::Result<::k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition, ::kube::core::crd::MergeError> {
339357
::kube::core::crd::merge_crds(vec![#(#crd_fn_calls),*], &stored_apiversion.to_string())
340358
}
@@ -353,32 +371,4 @@ impl VersionedStruct {
353371
<#path as ::kube::CustomResourceExt>::crd()
354372
}
355373
}
356-
357-
fn generate_kubernetes_version_enum(&self, enum_variants: Vec<(Ident, String)>) -> TokenStream {
358-
let mut enum_variant_matches = TokenStream::new();
359-
let mut enum_variant_idents = TokenStream::new();
360-
361-
for (enum_variant_ident, enum_variant_display) in enum_variants {
362-
enum_variant_idents.extend(quote! {#enum_variant_ident,});
363-
enum_variant_matches.extend(quote! {
364-
Version::#enum_variant_ident => f.write_str(#enum_variant_display),
365-
});
366-
}
367-
368-
quote! {
369-
#[automatically_derived]
370-
pub enum Version {
371-
#enum_variant_idents
372-
}
373-
374-
#[automatically_derived]
375-
impl ::std::fmt::Display for Version {
376-
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::result::Result<(), ::std::fmt::Error> {
377-
match self {
378-
#enum_variant_matches
379-
}
380-
}
381-
}
382-
}
383-
}
384374
}

crates/stackable-versioned-macros/tests/k8s/pass/crd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ fn main() {
2626
baz: bool,
2727
}
2828

29-
let merged_crd = Foo::merged_crd(Version::V1).unwrap();
29+
let merged_crd = Foo::merged_crd(Foo::V1).unwrap();
3030
println!("{}", serde_yaml::to_string(&merged_crd).unwrap());
3131
}

0 commit comments

Comments
 (0)