|
1 | 1 | //! CBOR decoding helper functions. |
2 | 2 |
|
| 3 | +use std::fmt::Debug; |
| 4 | + |
| 5 | +use catalyst_types::problem_report::ProblemReport; |
3 | 6 | use minicbor::{data::Tag, decode, Decoder}; |
4 | 7 |
|
5 | 8 | /// 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 |
92 | 95 | Ok(bytes) |
93 | 96 | } |
94 | 97 |
|
| 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 | + |
95 | 126 | #[cfg(test)] |
96 | 127 | mod tests { |
97 | 128 | use minicbor::Encoder; |
|
0 commit comments