Skip to content

Commit 6b6f802

Browse files
authored
refactor(stackable-versioned): Move preserve_module into options() (#961)
* refactor: Move preserve_module into options() * chore: Update changelog
1 parent 6636d94 commit 6b6f802

File tree

10 files changed

+63
-24
lines changed

10 files changed

+63
-24
lines changed

crates/stackable-versioned-macros/fixtures/inputs/default/module_preserve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
version(name = "v1alpha1"),
33
version(name = "v1"),
44
version(name = "v2alpha1"),
5-
preserve_module
5+
options(preserve_module)
66
)]
77
// ---
88
pub(crate) mod versioned {

crates/stackable-versioned-macros/fixtures/inputs/k8s/module_preserve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
version(name = "v1alpha1"),
33
version(name = "v1"),
44
version(name = "v2alpha1"),
5-
preserve_module
5+
options(preserve_module)
66
)]
77
// ---
88
pub(crate) mod versioned {

crates/stackable-versioned-macros/src/attrs/common.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@ use darling::{
55
use itertools::Itertools;
66
use k8s_version::Version;
77

8+
pub trait CommonOptions {
9+
fn allow_unsorted(&self) -> Flag;
10+
}
11+
812
#[derive(Debug, FromMeta)]
913
#[darling(and_then = CommonRootArguments::validate)]
10-
pub(crate) struct CommonRootArguments {
14+
pub(crate) struct CommonRootArguments<T>
15+
where
16+
T: CommonOptions + Default,
17+
{
1118
#[darling(default)]
12-
pub(crate) options: RootOptions,
19+
pub(crate) options: T,
1320

1421
#[darling(multiple, rename = "version")]
1522
pub(crate) versions: SpannedValue<Vec<VersionArguments>>,
1623
}
1724

18-
impl CommonRootArguments {
25+
impl<T> CommonRootArguments<T>
26+
where
27+
T: CommonOptions + Default,
28+
{
1929
fn validate(mut self) -> Result<Self> {
2030
let mut errors = Error::accumulator();
2131

@@ -32,7 +42,7 @@ impl CommonRootArguments {
3242
// (if allow_unsorted is set).
3343
self.versions.sort_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
3444

35-
if !self.options.allow_unsorted.is_present() && !is_sorted {
45+
if !self.options.allow_unsorted().is_present() && !is_sorted {
3646
let versions = self.versions.iter().map(|v| v.name).join(", ");
3747

3848
errors.push(Error::custom(format!(
@@ -59,12 +69,6 @@ impl CommonRootArguments {
5969
}
6070
}
6171

62-
#[derive(Clone, Debug, Default, FromMeta)]
63-
pub(crate) struct RootOptions {
64-
pub(crate) allow_unsorted: Flag,
65-
pub(crate) skip: Option<SkipArguments>,
66-
}
67-
6872
/// This struct contains supported version arguments.
6973
///
7074
/// Supported arguments are:

crates/stackable-versioned-macros/src/attrs/container.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use darling::{Error, FromAttributes, FromMeta, Result};
1+
use darling::{util::Flag, Error, FromAttributes, FromMeta, Result};
22

33
use crate::attrs::{
4-
common::{CommonRootArguments, SkipArguments},
4+
common::{CommonOptions, CommonRootArguments, SkipArguments},
55
k8s::KubernetesArguments,
66
};
77

@@ -12,7 +12,7 @@ pub(crate) struct StandaloneContainerAttributes {
1212
pub(crate) kubernetes_arguments: Option<KubernetesArguments>,
1313

1414
#[darling(flatten)]
15-
pub(crate) common_root_arguments: CommonRootArguments,
15+
pub(crate) common: CommonRootArguments<StandaloneContainerOptions>,
1616
}
1717

1818
impl StandaloneContainerAttributes {
@@ -25,6 +25,18 @@ impl StandaloneContainerAttributes {
2525
}
2626
}
2727

28+
#[derive(Debug, FromMeta, Default)]
29+
pub(crate) struct StandaloneContainerOptions {
30+
pub(crate) allow_unsorted: Flag,
31+
pub(crate) skip: Option<SkipArguments>,
32+
}
33+
34+
impl CommonOptions for StandaloneContainerOptions {
35+
fn allow_unsorted(&self) -> Flag {
36+
self.allow_unsorted
37+
}
38+
}
39+
2840
#[derive(Debug, FromAttributes)]
2941
#[darling(
3042
attributes(versioned),
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
use darling::{util::Flag, FromMeta};
22

3-
use crate::attrs::common::CommonRootArguments;
3+
use crate::attrs::common::{CommonOptions, CommonRootArguments, SkipArguments};
44

55
#[derive(Debug, FromMeta)]
66
pub(crate) struct ModuleAttributes {
77
#[darling(flatten)]
8-
pub(crate) common_root_arguments: CommonRootArguments,
8+
pub(crate) common: CommonRootArguments<ModuleOptions>,
9+
}
10+
11+
#[derive(Debug, FromMeta, Default)]
12+
pub(crate) struct ModuleOptions {
13+
pub(crate) allow_unsorted: Flag,
14+
pub(crate) skip: Option<SkipArguments>,
915
pub(crate) preserve_module: Flag,
1016
}
17+
18+
impl CommonOptions for ModuleOptions {
19+
fn allow_unsorted(&self) -> Flag {
20+
self.allow_unsorted
21+
}
22+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Container {
3131
let options = ContainerOptions {
3232
kubernetes_options: None,
3333
skip_from: attributes
34-
.common_root_arguments
34+
.common
3535
.options
3636
.skip
3737
.map_or(false, |s| s.from.is_present()),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Container {
4444

4545
let options = ContainerOptions {
4646
skip_from: attributes
47-
.common_root_arguments
47+
.common
4848
.options
4949
.skip
5050
.map_or(false, |s| s.from.is_present()),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) struct VersionDefinition {
3232
impl From<&StandaloneContainerAttributes> for Vec<VersionDefinition> {
3333
fn from(attributes: &StandaloneContainerAttributes) -> Self {
3434
attributes
35-
.common_root_arguments
35+
.common
3636
.versions
3737
.iter()
3838
.map(|v| VersionDefinition {
@@ -53,7 +53,7 @@ impl From<&StandaloneContainerAttributes> for Vec<VersionDefinition> {
5353
impl From<&ModuleAttributes> for Vec<VersionDefinition> {
5454
fn from(attributes: &ModuleAttributes) -> Self {
5555
attributes
56-
.common_root_arguments
56+
.common
5757
.versions
5858
.iter()
5959
.map(|v| VersionDefinition {

crates/stackable-versioned-macros/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ mod utils;
239239
/// #[versioned(
240240
/// version(name = "v1alpha1"),
241241
/// version(name = "v1"),
242-
/// preserve_module
242+
/// options(preserve_module)
243243
/// )]
244244
/// mod versioned {
245245
/// struct Foo {
@@ -732,9 +732,14 @@ fn versioned_impl(attrs: proc_macro2::TokenStream, input: Item) -> proc_macro2::
732732
};
733733

734734
let versions: Vec<VersionDefinition> = (&module_attributes).into();
735-
let preserve_modules = module_attributes.preserve_module.is_present();
735+
let preserve_modules = module_attributes
736+
.common
737+
.options
738+
.preserve_module
739+
.is_present();
740+
736741
let skip_from = module_attributes
737-
.common_root_arguments
742+
.common
738743
.options
739744
.skip
740745
.as_ref()

crates/stackable-versioned/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Changed
8+
9+
- BREAKING: Move `preserve_module` option into `options` to unify option interface ([#961]).
10+
11+
[#961]: https://github.com/stackabletech/operator-rs/pull/961
12+
713
## [0.5.1] - 2025-02-14
814

915
### Added

0 commit comments

Comments
 (0)