Skip to content

Commit 6d962b2

Browse files
feat(rust/catalyst-types): Add report_duplicated_key and report_missing_keys functions to catalyst-types (#180)
* Add report_duplicated_key and report_missing_keys functions to catalyst-types * Update rust/catalyst-types/src/cbor_utils.rs Co-authored-by: bkioshn <[email protected]> * Revert changes * fix --------- Co-authored-by: bkioshn <[email protected]>
1 parent 367b812 commit 6d962b2

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

rust/catalyst-types/src/cbor_utils.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//! CBOR utilities that are highly tied to Catalyst types, so they don't belong to the
2+
//! `cbork-utils` crate.
3+
4+
use std::fmt::Debug;
5+
6+
use crate::problem_report::ProblemReport;
7+
8+
/// Adds a "duplicated field" entry to the report and returns true if the field is already
9+
/// present in the given found keys list.
10+
pub fn report_duplicated_key<T: Debug + PartialEq>(
11+
found_keys: &[T], key: &T, index: u64, context: &str, report: &ProblemReport,
12+
) -> bool {
13+
if found_keys.contains(key) {
14+
report.duplicate_field(
15+
format!("{key:?}").as_str(),
16+
format!("Redundant key found in item {}", index + 1).as_str(),
17+
context,
18+
);
19+
return true;
20+
}
21+
false
22+
}
23+
24+
/// Adds a "missing field" entry to the report for every required key that isn't present
25+
/// in the found keys list.
26+
pub fn report_missing_keys<T: Debug + PartialEq>(
27+
found_keys: &[T], required_keys: &[T], context: &str, report: &ProblemReport,
28+
) {
29+
for key in required_keys {
30+
if !found_keys.contains(key) {
31+
report.missing_field(&format!("{key:?}"), context);
32+
}
33+
}
34+
}

rust/catalyst-types/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Catalyst Generic Types
22
3+
pub mod cbor_utils;
34
pub mod conversion;
45
pub mod hashes;
56
pub mod id_uri;

rust/cbork-utils/Cargo.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,4 @@ workspace = true
1414
minicbor = { version = "0.25.1", features = ["std"] }
1515

1616
[dev-dependencies]
17-
proptest = { version = "1.5.0" }
18-
# Potentially it could be replaced with using `proptest::property_test` attribute macro,
19-
# after this PR will be merged https://github.com/proptest-rs/proptest/pull/523
20-
test-strategy = "0.4.0"
17+
proptest = { version = "1.6.0", features = ["attr-macro"] }

rust/cbork-utils/src/decode_helper.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ pub fn decode_any<'d>(d: &mut Decoder<'d>, from: &str) -> Result<&'d [u8], decod
9595
#[cfg(test)]
9696
mod tests {
9797
use minicbor::Encoder;
98-
use test_strategy::proptest;
98+
use proptest::property_test;
9999

100100
use super::*;
101101

102-
#[proptest]
102+
#[property_test]
103103
fn test_decode_any_bytes(random_bytes: Vec<u8>) {
104104
let mut buf = Vec::new();
105105
let mut e = Encoder::new(&mut buf);
@@ -112,7 +112,7 @@ mod tests {
112112
assert_eq!(result, random_bytes);
113113
}
114114

115-
#[proptest]
115+
#[property_test]
116116
fn test_decode_any_string(random_string: String) {
117117
let mut buf = Vec::new();
118118
let mut e = Encoder::new(&mut buf);
@@ -126,7 +126,7 @@ mod tests {
126126
assert_eq!(result, random_string);
127127
}
128128

129-
#[proptest]
129+
#[property_test]
130130
fn test_decode_any_array(random_array: Vec<u8>) {
131131
// The array should contain a supported type
132132
let mut buf = Vec::new();
@@ -143,7 +143,7 @@ mod tests {
143143
assert_eq!(result, random_array.len() as u64);
144144
}
145145

146-
#[proptest]
146+
#[property_test]
147147
fn test_decode_any_u32(random_u32: u32) {
148148
let mut buf = Vec::new();
149149
let mut e = Encoder::new(&mut buf);
@@ -157,7 +157,7 @@ mod tests {
157157
assert_eq!(result, random_u32);
158158
}
159159

160-
#[proptest]
160+
#[property_test]
161161
fn test_decode_any_i32(random_i32: i32) {
162162
let mut buf = Vec::new();
163163
let mut e = Encoder::new(&mut buf);

0 commit comments

Comments
 (0)