Skip to content
Closed
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
2 changes: 2 additions & 0 deletions rust/cbork-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ workspace = true
[dependencies]
minicbor = { version = "0.25.1", features = ["std"] }

catalyst-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250124-00" }

[dev-dependencies]
proptest = { version = "1.5.0" }
# Potentially it could be replaced with using `proptest::property_test` attribute macro,
Expand Down
31 changes: 31 additions & 0 deletions rust/cbork-utils/src/decode_helper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! CBOR decoding helper functions.

use std::fmt::Debug;

use catalyst_types::problem_report::ProblemReport;
use minicbor::{data::Tag, decode, Decoder};

/// Generic helper function for decoding different types.
Expand Down Expand Up @@ -92,6 +95,34 @@ pub fn decode_any<'d>(d: &mut Decoder<'d>, from: &str) -> Result<&'d [u8], decod
Ok(bytes)
}

/// Adds a "duplicated field" entry to the report and returns true if the field is already
/// present in the given found keys list.
pub fn report_duplicated_key<T: Debug + PartialEq>(
found_keys: &[T], key: &T, index: u64, context: &str, report: &ProblemReport,
) -> bool {
if found_keys.contains(key) {
report.duplicate_field(
format!("{key:?}").as_str(),
format!("Redundant key found in item {} in RBAC map", index + 1).as_str(),
context,
);
return true;
}
false
}

/// Adds a "missing field" entry to the report for every required key that isn't present
/// in the found keys list.
pub fn report_missing_keys<T: Debug + PartialEq>(
found_keys: &[T], required_keys: &[T], context: &str, report: &ProblemReport,
) {
for key in required_keys {
if !found_keys.contains(key) {
report.missing_field(&format!("{key:?}"), context);
}
}
}

#[cfg(test)]
mod tests {
use minicbor::Encoder;
Expand Down
Loading