Skip to content

Commit 9e53840

Browse files
committed
feat: Make YAML helpers fallible
1 parent c4fe882 commit 9e53840

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -391,33 +391,41 @@ impl VersionedStruct {
391391

392392
/// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]`
393393
/// macro to a file located at `path`.
394-
pub fn write_merged_crd<P>(path: P, stored_apiversion: Self, operator_version: &str)
394+
pub fn write_merged_crd<P>(path: P, stored_apiversion: Self, operator_version: &str) -> Result<(), ::stackable_versioned::Error>
395395
where P: AsRef<::std::path::Path>
396396
{
397397
use ::stackable_shared::yaml::{YamlSchema, SerializeOptions};
398398

399-
let merged_crd = Self::merged_crd(stored_apiversion).unwrap();
399+
let merged_crd = Self::merged_crd(stored_apiversion).map_err(|err| ::stackable_versioned::Error::MergeCrd {
400+
source: err,
401+
})?;
400402

401403
YamlSchema::write_yaml_schema(
402404
&merged_crd,
403405
path,
404406
operator_version,
405407
SerializeOptions::default()
406-
).unwrap();
408+
).map_err(|err| ::stackable_versioned::Error::SerializeYaml {
409+
source: err,
410+
})
407411
}
408412

409413
/// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]`
410414
/// macro to stdout.
411-
pub fn print_merged_crd(stored_apiversion: Self, operator_version: &str) {
415+
pub fn print_merged_crd(stored_apiversion: Self, operator_version: &str) -> Result<(), ::stackable_versioned::Error> {
412416
use ::stackable_shared::yaml::{YamlSchema, SerializeOptions};
413417

414-
let merged_crd = Self::merged_crd(stored_apiversion).unwrap();
418+
let merged_crd = Self::merged_crd(stored_apiversion).map_err(|err| ::stackable_versioned::Error::MergeCrd {
419+
source: err,
420+
})?;
415421

416422
YamlSchema::print_yaml_schema(
417423
&merged_crd,
418424
operator_version,
419425
SerializeOptions::default()
420-
).unwrap();
426+
).map_err(|err| ::stackable_versioned::Error::SerializeYaml {
427+
source: err,
428+
})
421429
}
422430
}
423431
}

crates/stackable-versioned/Cargo.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ all-features = true
1212

1313
[features]
1414
full = ["k8s"]
15-
# Forward the k8s feature to the underlying macro crate
16-
k8s = ["stackable-versioned-macros/k8s"]
15+
k8s = [
16+
"stackable-versioned-macros/k8s", # Forward the k8s feature to the underlying macro crate
17+
"dep:stackable-shared",
18+
"dep:snafu",
19+
"dep:kube",
20+
]
1721

1822
[dependencies]
23+
stackable-shared = { path = "../stackable-shared", version = "0.0.1", optional = true }
1924
stackable-versioned-macros = { path = "../stackable-versioned-macros" }
25+
26+
kube = { workspace = true, optional = true }
27+
snafu = { workspace = true, optional = true }

crates/stackable-versioned/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,21 @@
1212
//! See [`versioned`] for an in-depth usage guide and a list of supported
1313
//! parameters.
1414
15+
// Re-export macro
1516
pub use stackable_versioned_macros::*;
1617

18+
#[cfg(feature = "k8s")]
19+
#[derive(Debug, snafu::Snafu)]
20+
pub enum Error {
21+
#[snafu(display("failed to merge CRDs"))]
22+
MergeCrd { source: kube::core::crd::MergeError },
23+
24+
#[snafu(display("failed to serialize YAML"))]
25+
SerializeYaml {
26+
source: stackable_shared::yaml::Error,
27+
},
28+
}
29+
1730
// Unused for now, might get picked up again in the future.
1831
#[doc(hidden)]
1932
pub trait AsVersionStr {

0 commit comments

Comments
 (0)