Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rust/cbork): Add report_duplicated_key and report_missing_keys functions to the cbork-utils crate #178

Closed
wants to merge 1 commit into from
Closed
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
Move report_duplicated_key and report_missing_keys to the cbork-utils…
… crate
  • Loading branch information
stanislav-tkach committed Jan 25, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit c43f275f7e94f0d74afed2c63a078afa29feb74a
2 changes: 2 additions & 0 deletions rust/cbork-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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,
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.
@@ -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;
Loading