Skip to content

Commit a922c60

Browse files
committed
feat: Add support for modules
This commit contains a major rework of most parts of the attribute macro to support attaching it to modules. Most of the previously existing features are already in place. There are only two missing features which will be added in follow-up commits: version specific doc comments and merged CRD YAML support. Because of that, some snapshot tests are failing.
1 parent df643d9 commit a922c60

File tree

34 files changed

+2289
-2732
lines changed

34 files changed

+2289
-2732
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use darling::{
2+
util::{Flag, SpannedValue},
3+
Error, FromMeta, Result,
4+
};
5+
use itertools::Itertools;
6+
use k8s_version::Version;
7+
8+
#[derive(Debug, FromMeta)]
9+
#[darling(and_then = CommonRootArguments::validate)]
10+
pub(crate) struct CommonRootArguments {
11+
#[darling(default)]
12+
pub(crate) options: RootOptions,
13+
14+
#[darling(multiple, rename = "version")]
15+
pub(crate) versions: SpannedValue<Vec<VersionArguments>>,
16+
}
17+
18+
impl CommonRootArguments {
19+
fn validate(mut self) -> Result<Self> {
20+
let mut errors = Error::accumulator();
21+
22+
if self.versions.is_empty() {
23+
errors.push(
24+
Error::custom("at least one or more `version`s must be defined")
25+
.with_span(&self.versions.span()),
26+
);
27+
}
28+
29+
let is_sorted = self.versions.iter().is_sorted_by_key(|v| v.name);
30+
31+
// It needs to be sorted, even tho the definition could be unsorted (if allow_unsorted is
32+
// set).
33+
self.versions.sort_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
34+
35+
if !self.options.allow_unsorted.is_present() && !is_sorted {
36+
let versions = self.versions.iter().map(|v| v.name).join(", ");
37+
38+
errors.push(Error::custom(format!(
39+
"versions must be defined in ascending order: {versions}",
40+
)));
41+
}
42+
43+
let duplicate_versions: Vec<_> = self
44+
.versions
45+
.iter()
46+
.duplicates_by(|v| v.name)
47+
.map(|v| v.name)
48+
.collect();
49+
50+
if !duplicate_versions.is_empty() {
51+
let versions = duplicate_versions.iter().join(", ");
52+
53+
errors.push(Error::custom(format!(
54+
"contains duplicate versions: {versions}",
55+
)));
56+
}
57+
58+
errors.finish_with(self)
59+
}
60+
}
61+
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+
68+
/// This struct contains supported version arguments.
69+
///
70+
/// Supported arguments are:
71+
///
72+
/// - `name` of the version, like `v1alpha1`.
73+
/// - `deprecated` flag to mark that version as deprecated.
74+
/// - `skip` option to skip generating various pieces of code.
75+
/// - `doc` option to add version-specific documentation.
76+
#[derive(Clone, Debug, FromMeta)]
77+
pub(crate) struct VersionArguments {
78+
pub(crate) deprecated: Flag,
79+
pub(crate) name: Version,
80+
pub(crate) skip: Option<SkipArguments>,
81+
pub(crate) doc: Option<String>,
82+
}
83+
84+
/// This struct contains supported common skip arguments.
85+
///
86+
/// Supported arguments are:
87+
///
88+
/// - `from` flag, which skips generating [`From`] implementations when provided.
89+
#[derive(Clone, Debug, Default, FromMeta)]
90+
pub(crate) struct SkipArguments {
91+
/// Whether the [`From`] implementation generation should be skipped for all versions of this
92+
/// container.
93+
pub(crate) from: Flag,
94+
}

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

Lines changed: 0 additions & 154 deletions
This file was deleted.

0 commit comments

Comments
 (0)