Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
58 changes: 24 additions & 34 deletions crates/stackable-versioned-macros/src/codegen/vstruct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,22 +319,40 @@ impl VersionedStruct {
crd_fn_calls: Vec<TokenStream>,
enum_variants: Vec<(Ident, String)>,
) -> TokenStream {
let ident = &self.idents.kubernetes;
let enum_ident = &self.idents.kubernetes;
let enum_vis = &self.visibility;

let version_enum_definition = self.generate_kubernetes_version_enum(enum_variants);
let mut enum_variant_matches = TokenStream::new();
let mut enum_variant_idents = TokenStream::new();

for (enum_variant_ident, enum_variant_display) in enum_variants {
enum_variant_idents.extend(quote! {#enum_variant_ident,});
enum_variant_matches.extend(quote! {
#enum_ident::#enum_variant_ident => f.write_str(#enum_variant_display),
});
}

quote! {
#[automatically_derived]
pub struct #ident;
#enum_vis enum #enum_ident {
#enum_variant_idents
}

#version_enum_definition
#[automatically_derived]
impl ::std::fmt::Display for #enum_ident {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::result::Result<(), ::std::fmt::Error> {
match self {
#enum_variant_matches
}
}
}

#[automatically_derived]
impl #ident {
impl #enum_ident {
/// Generates a merged CRD which contains all versions defined using the
/// `#[versioned()]` macro.
pub fn merged_crd(
stored_apiversion: Version
stored_apiversion: Self
) -> ::std::result::Result<::k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition, ::kube::core::crd::MergeError> {
::kube::core::crd::merge_crds(vec![#(#crd_fn_calls),*], &stored_apiversion.to_string())
}
Expand All @@ -353,32 +371,4 @@ impl VersionedStruct {
<#path as ::kube::CustomResourceExt>::crd()
}
}

fn generate_kubernetes_version_enum(&self, enum_variants: Vec<(Ident, String)>) -> TokenStream {
let mut enum_variant_matches = TokenStream::new();
let mut enum_variant_idents = TokenStream::new();

for (enum_variant_ident, enum_variant_display) in enum_variants {
enum_variant_idents.extend(quote! {#enum_variant_ident,});
enum_variant_matches.extend(quote! {
Version::#enum_variant_ident => f.write_str(#enum_variant_display),
});
}

quote! {
#[automatically_derived]
pub enum Version {
#enum_variant_idents
}

#[automatically_derived]
impl ::std::fmt::Display for Version {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::result::Result<(), ::std::fmt::Error> {
match self {
#enum_variant_matches
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion crates/stackable-versioned-macros/tests/k8s/pass/crd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ fn main() {
baz: bool,
}

let merged_crd = Foo::merged_crd(Version::V1).unwrap();
let merged_crd = Foo::merged_crd(Foo::V1).unwrap();
println!("{}", serde_yaml::to_string(&merged_crd).unwrap());
}
3 changes: 3 additions & 0 deletions crates/stackable-versioned/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ All notable changes to this project will be documented in this file.

### Changed

- BREAKING: The `merged_crd` function now accepts `Self` instead of a dedicated
`Version` enu ([#875]).
- The `merged_crd` associated function now takes `Version` instead of `&str` as
input ([#872]).

[#872]: https://github.com/stackabletech/operator-rs/pull/872
[#873]: https://github.com/stackabletech/operator-rs/pull/873
[#875]: https://github.com/stackabletech/operator-rs/pull/875

## [0.2.0] - 2024-09-19

Expand Down