Skip to content

Commit c43f275

Browse files
Move report_duplicated_key and report_missing_keys to the cbork-utils crate
1 parent ffe23e3 commit c43f275

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

rust/cbork-utils/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ workspace = true
1313
[dependencies]
1414
minicbor = { version = "0.25.1", features = ["std"] }
1515

16+
catalyst-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250124-00" }
17+
1618
[dev-dependencies]
1719
proptest = { version = "1.5.0" }
1820
# Potentially it could be replaced with using `proptest::property_test` attribute macro,

rust/cbork-utils/src/decode_helper.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! CBOR decoding helper functions.
22
3+
use std::fmt::Debug;
4+
5+
use catalyst_types::problem_report::ProblemReport;
36
use minicbor::{data::Tag, decode, Decoder};
47

58
/// 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
9295
Ok(bytes)
9396
}
9497

98+
/// Adds a "duplicated field" entry to the report and returns true if the field is already
99+
/// present in the given found keys list.
100+
pub fn report_duplicated_key<T: Debug + PartialEq>(
101+
found_keys: &[T], key: &T, index: u64, context: &str, report: &ProblemReport,
102+
) -> bool {
103+
if found_keys.contains(key) {
104+
report.duplicate_field(
105+
format!("{key:?}").as_str(),
106+
format!("Redundant key found in item {} in RBAC map", index + 1).as_str(),
107+
context,
108+
);
109+
return true;
110+
}
111+
false
112+
}
113+
114+
/// Adds a "missing field" entry to the report for every required key that isn't present
115+
/// in the found keys list.
116+
pub fn report_missing_keys<T: Debug + PartialEq>(
117+
found_keys: &[T], required_keys: &[T], context: &str, report: &ProblemReport,
118+
) {
119+
for key in required_keys {
120+
if !found_keys.contains(key) {
121+
report.missing_field(&format!("{key:?}"), context);
122+
}
123+
}
124+
}
125+
95126
#[cfg(test)]
96127
mod tests {
97128
use minicbor::Encoder;

0 commit comments

Comments
 (0)