@@ -21,7 +21,7 @@ mod r#enum;
2121mod r#struct;
2222
2323/// Contains common container data shared between structs and enums.
24- pub ( crate ) struct CommonContainerData {
24+ pub struct CommonContainerData {
2525 /// Original attributes placed on the container, like `#[derive()]` or `#[cfg()]`.
2626 pub ( crate ) original_attributes : Vec < Attribute > ,
2727
@@ -37,7 +37,7 @@ pub(crate) struct CommonContainerData {
3737/// Abstracting away with kind of container is generated makes it possible to create a list of
3838/// containers when the macro is used on modules. This enum provides functions to generate code
3939/// which then internally call the appropriate function based on the variant.
40- pub ( crate ) enum Container {
40+ pub enum Container {
4141 Struct ( Struct ) ,
4242 Enum ( Enum ) ,
4343}
@@ -51,16 +51,37 @@ impl Container {
5151 }
5252 }
5353
54- /// Generates the container `From<Version> for NextVersion` implementation.
55- pub ( crate ) fn generate_from_impl (
54+ /// Generates the `From<Version> for NextVersion` implementation for the container .
55+ pub fn generate_upgrade_from_impl (
5656 & self ,
5757 version : & VersionDefinition ,
5858 next_version : Option < & VersionDefinition > ,
5959 add_attributes : bool ,
6060 ) -> Option < TokenStream > {
6161 match self {
62- Container :: Struct ( s) => s. generate_from_impl ( version, next_version, add_attributes) ,
63- Container :: Enum ( e) => e. generate_from_impl ( version, next_version, add_attributes) ,
62+ Container :: Struct ( s) => {
63+ s. generate_upgrade_from_impl ( version, next_version, add_attributes)
64+ }
65+ Container :: Enum ( e) => {
66+ e. generate_upgrade_from_impl ( version, next_version, add_attributes)
67+ }
68+ }
69+ }
70+
71+ /// Generates the `From<NextVersion> for Version` implementation for the container.
72+ pub fn generate_downgrade_from_impl (
73+ & self ,
74+ version : & VersionDefinition ,
75+ next_version : Option < & VersionDefinition > ,
76+ add_attributes : bool ,
77+ ) -> Option < TokenStream > {
78+ match self {
79+ Container :: Struct ( s) => {
80+ s. generate_downgrade_from_impl ( version, next_version, add_attributes)
81+ }
82+ Container :: Enum ( e) => {
83+ e. generate_downgrade_from_impl ( version, next_version, add_attributes)
84+ }
6485 }
6586 }
6687
@@ -180,9 +201,17 @@ impl StandaloneContainer {
180201
181202 // NOTE (@Techassi): Using '.copied()' here does not copy or clone the data, but instead
182203 // removes one level of indirection of the double reference '&&'.
183- let from_impl =
204+ let next_version = versions. peek ( ) . copied ( ) ;
205+
206+ // Generate the From impl for upgrading the CRD.
207+ let upgrade_from_impl =
208+ self . container
209+ . generate_upgrade_from_impl ( version, next_version, false ) ;
210+
211+ // Generate the From impl for downgrading the CRD.
212+ let downgrade_from_impl =
184213 self . container
185- . generate_from_impl ( version, versions . peek ( ) . copied ( ) , false ) ;
214+ . generate_downgrade_from_impl ( version, next_version , false ) ;
186215
187216 // Add the #[deprecated] attribute when the version is marked as deprecated.
188217 let deprecated_attribute = version
@@ -210,7 +239,8 @@ impl StandaloneContainer {
210239 #container_definition
211240 }
212241
213- #from_impl
242+ #upgrade_from_impl
243+ #downgrade_from_impl
214244 } ) ;
215245 }
216246
@@ -231,13 +261,13 @@ impl StandaloneContainer {
231261pub ( crate ) struct ContainerIdents {
232262 /// The ident used in the context of Kubernetes specific code. This ident
233263 /// removes the 'Spec' suffix present in the definition container.
234- pub ( crate ) kubernetes : IdentString ,
264+ pub kubernetes : IdentString ,
235265
236266 /// The original ident, or name, of the versioned container.
237- pub ( crate ) original : IdentString ,
267+ pub original : IdentString ,
238268
239269 /// The ident used in the [`From`] impl.
240- pub ( crate ) from : IdentString ,
270+ pub from : IdentString ,
241271}
242272
243273impl ContainerIdents {
@@ -261,32 +291,32 @@ impl ContainerIdents {
261291}
262292
263293#[ derive( Debug ) ]
264- pub ( crate ) struct ContainerOptions {
265- pub ( crate ) kubernetes_options : Option < KubernetesOptions > ,
266- pub ( crate ) skip_from : bool ,
294+ pub struct ContainerOptions {
295+ pub kubernetes_options : Option < KubernetesOptions > ,
296+ pub skip_from : bool ,
267297}
268298
269299#[ derive( Debug ) ]
270- pub ( crate ) struct KubernetesOptions {
271- pub ( crate ) group : String ,
272- pub ( crate ) kind : Option < String > ,
273- pub ( crate ) singular : Option < String > ,
274- pub ( crate ) plural : Option < String > ,
275- pub ( crate ) namespaced : bool ,
300+ pub struct KubernetesOptions {
301+ pub group : String ,
302+ pub kind : Option < String > ,
303+ pub singular : Option < String > ,
304+ pub plural : Option < String > ,
305+ pub namespaced : bool ,
276306 // root
277- pub ( crate ) crates : KubernetesCrateOptions ,
278- pub ( crate ) status : Option < Path > ,
307+ pub crates : KubernetesCrateOptions ,
308+ pub status : Option < Path > ,
279309 // derive
280310 // schema
281311 // scale
282312 // printcolumn
283- pub ( crate ) shortnames : Vec < String > ,
313+ pub shortnames : Vec < String > ,
284314 // category
285315 // selectable
286316 // doc
287317 // annotation
288318 // label
289- pub ( crate ) skip_merged_crd : bool ,
319+ pub skip_merged_crd : bool ,
290320}
291321
292322impl From < KubernetesArguments > for KubernetesOptions {
@@ -308,12 +338,12 @@ impl From<KubernetesArguments> for KubernetesOptions {
308338}
309339
310340#[ derive( Debug ) ]
311- pub ( crate ) struct KubernetesCrateOptions {
312- pub ( crate ) kube_core : Override < Path > ,
313- pub ( crate ) k8s_openapi : Override < Path > ,
314- pub ( crate ) schemars : Override < Path > ,
315- pub ( crate ) serde : Override < Path > ,
316- pub ( crate ) serde_json : Override < Path > ,
341+ pub struct KubernetesCrateOptions {
342+ pub kube_core : Override < Path > ,
343+ pub k8s_openapi : Override < Path > ,
344+ pub schemars : Override < Path > ,
345+ pub serde : Override < Path > ,
346+ pub serde_json : Override < Path > ,
317347}
318348
319349impl Default for KubernetesCrateOptions {
@@ -396,7 +426,7 @@ impl ToTokens for KubernetesCrateOptions {
396426
397427/// Wraps a value to indicate whether it is original or has been overridden.
398428#[ derive( Debug ) ]
399- pub ( crate ) enum Override < T > {
429+ pub enum Override < T > {
400430 Default ( T ) ,
401431 Overridden ( T ) ,
402432}
0 commit comments