From cccca079ba00219417f2837ee44b4c710eb14332 Mon Sep 17 00:00:00 2001 From: Nick Larsen Date: Thu, 28 Nov 2024 15:46:55 +0100 Subject: [PATCH 1/3] refactor(stackable-versioned): Convert Override to enum --- .../src/codegen/container/mod.rs | 84 ++++++++----------- crates/stackable-versioned/CHANGELOG.md | 2 + 2 files changed, 39 insertions(+), 47 deletions(-) diff --git a/crates/stackable-versioned-macros/src/codegen/container/mod.rs b/crates/stackable-versioned-macros/src/codegen/container/mod.rs index 048ba1a0..a68103e6 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/mod.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/mod.rs @@ -306,11 +306,11 @@ pub(crate) struct KubernetesCrateOptions { impl Default for KubernetesCrateOptions { fn default() -> Self { Self { - k8s_openapi: Override::new_default(parse_quote! { ::k8s_openapi }), - serde_json: Override::new_default(parse_quote! { ::serde_json }), - kube_core: Override::new_default(parse_quote! { ::kube::core }), - schemars: Override::new_default(parse_quote! { ::schemars }), - serde: Override::new_default(parse_quote! { ::serde }), + k8s_openapi: Override::Default(parse_quote! { ::k8s_openapi }), + serde_json: Override::Default(parse_quote! { ::serde_json }), + kube_core: Override::Default(parse_quote! { ::kube::core }), + schemars: Override::Default(parse_quote! { ::schemars }), + serde: Override::Default(parse_quote! { ::serde }), } } } @@ -320,23 +320,23 @@ impl From for KubernetesCrateOptions { let mut crate_options = Self::default(); if let Some(k8s_openapi) = args.k8s_openapi { - crate_options.k8s_openapi = Override::new_custom(k8s_openapi); + crate_options.k8s_openapi = Override::Overridden(k8s_openapi); } if let Some(serde_json) = args.serde_json { - crate_options.serde_json = Override::new_custom(serde_json); + crate_options.serde_json = Override::Overridden(serde_json); } if let Some(kube_core) = args.kube_core { - crate_options.kube_core = Override::new_custom(kube_core); + crate_options.kube_core = Override::Overridden(kube_core); } if let Some(schemars) = args.schemars { - crate_options.schemars = Override::new_custom(schemars); + crate_options.schemars = Override::Overridden(schemars); } if let Some(serde) = args.serde { - crate_options.serde = Override::new_custom(serde); + crate_options.serde = Override::Overridden(serde); } crate_options @@ -355,23 +355,23 @@ impl ToTokens for KubernetesCrateOptions { serde, } = self; - if let Some(k8s_openapi) = k8s_openapi.get_if_overridden() { + if let Override::Overridden(k8s_openapi) = k8s_openapi { crate_overrides.extend(quote! { k8s_openapi = #k8s_openapi, }); } - if let Some(serde_json) = serde_json.get_if_overridden() { + if let Override::Overridden(serde_json) = serde_json { crate_overrides.extend(quote! { serde_json = #serde_json, }); } - if let Some(kube_core) = kube_core.get_if_overridden() { + if let Override::Overridden(kube_core) = kube_core { crate_overrides.extend(quote! { kube_core = #kube_core, }); } - if let Some(schemars) = schemars.get_if_overridden() { + if let Override::Overridden(schemars) = schemars { crate_overrides.extend(quote! { schemars = #schemars, }); } - if let Some(serde) = serde.get_if_overridden() { + if let Override::Overridden(serde) = serde { crate_overrides.extend(quote! { serde = #serde, }); } @@ -383,44 +383,34 @@ impl ToTokens for KubernetesCrateOptions { /// Wraps a value to indicate whether it is original or has been overridden. #[derive(Debug)] -pub(crate) struct Override { - is_overridden: bool, - inner: T, +pub(crate) enum Override { + Default(T), + Overridden(T), } -impl Override { - /// Mark a value as a default. - /// - /// This is used to indicate that the value is a default and was not overridden. - pub(crate) fn new_default(inner: T) -> Self { - Override { - is_overridden: false, - inner, - } - } - - /// Mark a value as overridden. - /// - /// This is used to indicate that the value was overridden and not the default. - pub(crate) fn new_custom(inner: T) -> Self { - Override { - is_overridden: true, - inner, - } - } - - pub(crate) fn get_if_overridden(&self) -> Option<&T> { - match &self.is_overridden { - true => Some(&self.inner), - false => None, - } - } -} +// impl Override { +// /// Mark a value as a default. +// /// +// /// This is used to indicate that the value is a default and was not overridden. +// pub(crate) fn new_default(inner: T) -> Self { +// Override::Default(inner) +// } + +// /// Mark a value as overridden. +// /// +// /// This is used to indicate that the value was overridden and not the default. +// pub(crate) fn new_custom(inner: T) -> Self { +// Override::Overridden(inner) +// } +// } impl Deref for Override { type Target = T; fn deref(&self) -> &Self::Target { - &self.inner + match &self { + Override::Default(inner) => inner, + Override::Overridden(inner) => inner, + } } } diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index 85756ea7..38e0a734 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file. - Simplify crate override handling and generation ([#919]). - Bump Rust to 1.82.0 ([#891]). +- Refactor the Override type ([#xxx]). ### Fixed @@ -31,6 +32,7 @@ All notable changes to this project will be documented in this file. [#913]: https://github.com/stackabletech/operator-rs/pull/913 [#914]: https://github.com/stackabletech/operator-rs/pull/914 [#919]: https://github.com/stackabletech/operator-rs/pull/919 +[#xxx]: https://github.com/stackabletech/operator-rs/pull/xxx ## [0.4.1] - 2024-10-23 From e389740e82def124851a1d3ef1687ee234040c3f Mon Sep 17 00:00:00 2001 From: Nick Larsen Date: Thu, 28 Nov 2024 15:48:49 +0100 Subject: [PATCH 2/3] chore(stackable-versioned): Update changelog --- crates/stackable-versioned/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index 38e0a734..2ae594fe 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -20,7 +20,7 @@ All notable changes to this project will be documented in this file. - Simplify crate override handling and generation ([#919]). - Bump Rust to 1.82.0 ([#891]). -- Refactor the Override type ([#xxx]). +- Refactor the Override type ([#922]). ### Fixed @@ -32,7 +32,7 @@ All notable changes to this project will be documented in this file. [#913]: https://github.com/stackabletech/operator-rs/pull/913 [#914]: https://github.com/stackabletech/operator-rs/pull/914 [#919]: https://github.com/stackabletech/operator-rs/pull/919 -[#xxx]: https://github.com/stackabletech/operator-rs/pull/xxx +[#922]: https://github.com/stackabletech/operator-rs/pull/922 ## [0.4.1] - 2024-10-23 From 6ed2ad682bea2cdd7dffad4c4c57a06220927728 Mon Sep 17 00:00:00 2001 From: Nick Larsen Date: Thu, 28 Nov 2024 15:50:21 +0100 Subject: [PATCH 3/3] chore(stackable-versioned): Remove commented code --- .../src/codegen/container/mod.rs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/crates/stackable-versioned-macros/src/codegen/container/mod.rs b/crates/stackable-versioned-macros/src/codegen/container/mod.rs index a68103e6..893f4885 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/mod.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/mod.rs @@ -388,22 +388,6 @@ pub(crate) enum Override { Overridden(T), } -// impl Override { -// /// Mark a value as a default. -// /// -// /// This is used to indicate that the value is a default and was not overridden. -// pub(crate) fn new_default(inner: T) -> Self { -// Override::Default(inner) -// } - -// /// Mark a value as overridden. -// /// -// /// This is used to indicate that the value was overridden and not the default. -// pub(crate) fn new_custom(inner: T) -> Self { -// Override::Overridden(inner) -// } -// } - impl Deref for Override { type Target = T;