Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions crates/stackable-versioned-macros/src/attrs/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ pub struct ModuleSkipArguments {

/// This struct contains crate overrides to be passed to `#[kube]`.
#[derive(Clone, Debug, FromMeta)]
#[darling(and_then = "CrateArguments::dynamic_default")]
pub struct CrateArguments {
#[darling(default = default_kube)]
pub kube: Override<Path>,

#[darling(default = default_kube_core)]
pub kube_core: Override<Path>,

Expand Down Expand Up @@ -124,6 +128,7 @@ pub struct CrateArguments {
impl Default for CrateArguments {
fn default() -> Self {
Self {
kube: default_kube(),
kube_core: default_kube_core(),
kube_client: default_kube_client(),
k8s_openapi: default_k8s_openapi(),
Expand All @@ -136,6 +141,30 @@ impl Default for CrateArguments {
}
}

impl CrateArguments {
fn dynamic_default(mut self) -> Result<Self> {
// Adjust the kube_core and kube_client paths automatically if only the kube path is
// overridden.
if let Override::Explicit(kube_path) = &self.kube {
if self.kube_core.is_explicit() || self.kube_client.is_explicit() {
return Err(
Error::custom("the `kube` crate override is mutually exclusive with `kube_core` and `kube_client`")
.with_span(&kube_path)
);
}

self.kube_core = Override::Explicit(parse_quote! { #kube_path::core });
self.kube_client = Override::Explicit(parse_quote! { #kube_path::client });
}

Ok(self)
}
}

fn default_kube() -> Override<Path> {
Override::Default(parse_quote! { ::kube })
}

fn default_kube_core() -> Override<Path> {
Override::Default(parse_quote! { ::kube::core })
}
Expand Down Expand Up @@ -262,3 +291,12 @@ impl<T> Deref for Override<T> {
}
}
}

impl<T> Override<T> {
pub fn is_explicit(&self) -> bool {
match self {
Override::Default(_) => false,
Override::Explicit(_) => true,
}
}
}
4 changes: 3 additions & 1 deletion crates/stackable-versioned-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ mod utils;
/// the crates are brought into scope through re-exports. The following code
/// block depicts supported overrides and their default values.
///
/// ```
/// ```ignore
/// # use stackable_versioned_macros::versioned;
/// #[versioned(
/// version(name = "v1alpha1"),
Expand All @@ -213,6 +213,8 @@ mod utils;
/// kube_core = "::kube::core",
/// schemars = "::schemars",
/// serde = "::serde",
/// // Mutually exclusive with kube_core and kube_client
/// kube = "::kube",
/// )
/// )]
/// mod versioned {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use stackable_versioned::versioned;
// ---
#[versioned(
version(name = "v1alpha1"),
version(name = "v1beta1"),
version(name = "v1"),
crates(
kube = ::kube,
schemars = ::schemars
)
)]
// ---
pub mod versioned {
#[versioned(crd(group = "foo.example.org"))]
#[derive(
Clone,
Debug,
serde::Deserialize,
serde::Serialize,
schemars::JsonSchema,
kube::CustomResource,
)]
pub struct FooSpec {
#[versioned(added(since = "v1beta1"), changed(since = "v1", from_name = "bah"))]
bar: usize,
baz: bool,
}
}
// ---
fn main() {}
Loading
Loading