From 72b6c961018028b2d76e10fd86e742313803cc3e Mon Sep 17 00:00:00 2001 From: Fahad Zubair Date: Wed, 2 Oct 2024 08:21:25 +0100 Subject: [PATCH] Simplify the match statements --- .../aws-smithy-protocol-test/src/lib.rs | 76 +++++++++++-------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/rust-runtime/aws-smithy-protocol-test/src/lib.rs b/rust-runtime/aws-smithy-protocol-test/src/lib.rs index 9a69e4109b..be5fcd2579 100644 --- a/rust-runtime/aws-smithy-protocol-test/src/lib.rs +++ b/rust-runtime/aws-smithy-protocol-test/src/lib.rs @@ -424,45 +424,50 @@ fn cbor_values_equal( a: &ciborium::value::Value, b: &ciborium::value::Value, ) -> Result { - let result = match (a, b) { + match (a, b) { (ciborium::value::Value::Array(a_array), ciborium::value::Value::Array(b_array)) => { // Both arrays should be equal in size. - a_array.len() == b_array.len() && + if a_array.len() != b_array.len() { + return Ok(false); + } // Compare arrays element-wise. - a_array.iter().zip(b_array.iter()).try_fold(true, |acc, (a_elem, b_elem)| { - cbor_values_equal(a_elem, b_elem).map(|equal| acc && equal) - })? + for (a_elem, b_elem) in a_array.iter().zip(b_array.iter()) { + if !cbor_values_equal(a_elem, b_elem)? { + return Ok(false); + } + } + return Ok(true); } // Convert `ciborium::value::Value::Map` to a `HashMap`, and then compare the values of // each key in `a` with those in `b`. (ciborium::value::Value::Map(a_map), ciborium::value::Value::Map(b_map)) => { - let a_hashmap = ciborium_map_to_hashmap(a_map)?; - let b_hashmap = ciborium_map_to_hashmap(b_map)?; + if a_map.len() != b_map.len() { + return Ok(false); + } - if a_hashmap.len() != b_hashmap.len() { - false - } else { - // Each key in `a` should exist in `b`, and the values should match. - a_hashmap.iter().try_fold(true, |acc, (a_key, a_value)| { - b_hashmap - .get(a_key) - .map(|b_value| { - cbor_values_equal(a_value, b_value).map(|equal| acc && equal) - }) - .unwrap_or(Ok(false)) - })? + let b_hashmap = ciborium_map_to_hashmap(b_map)?; + // Each key in `a` should exist in `b`, and the values should match. + for a_key_value in a_map.iter() { + let (a_key, a_value) = get_text_key_value(a_key_value)?; + match b_hashmap.get(a_key) { + Some(b_value) => { + if !cbor_values_equal(a_value, b_value)? { + return Ok(false); + } + } + None => return Ok(false), + } } + return Ok(true); } (ciborium::value::Value::Float(a_float), ciborium::value::Value::Float(b_float)) => { - a_float == b_float || a_float.is_nan() && b_float.is_nan() + return Ok(a_float == b_float || (a_float.is_nan() && b_float.is_nan())); } - _ => a == b, + _ => return Ok(a == b), }; - - Ok(result) } /// Converts a `ciborium::value::Value::Map` into a `HashMap<&String, &ciborium::value::Value>`. @@ -475,16 +480,21 @@ fn cbor_values_equal( fn ciborium_map_to_hashmap( cbor_map: &[(ciborium::value::Value, ciborium::value::Value)], ) -> Result, ProtocolTestFailure> { - cbor_map - .iter() - .map(|(key, value)| match key { - ciborium::value::Value::Text(key_str) => Ok((key_str, value)), - _ => Err(ProtocolTestFailure::InvalidBodyFormat { - expected: "a text key as map entry".to_string(), - found: format!("{:?}", key), - }), - }) - .collect() + cbor_map.iter().map(get_text_key_value).collect() +} + +/// Extracts a string key and its associated value from a CBOR key-value pair. +/// Returns a `ProtocolTestFailure::InvalidBodyFormat` error if the key is not a text value. +fn get_text_key_value( + (key, value): &(ciborium::value::Value, ciborium::value::Value), +) -> Result<(&String, &ciborium::value::Value), ProtocolTestFailure> { + match key { + ciborium::value::Value::Text(key_str) => Ok((key_str, value)), + _ => Err(ProtocolTestFailure::InvalidBodyFormat { + expected: "a text key as map entry".to_string(), + found: format!("{:?}", key), + }), + } } fn try_cbor_eq + Debug>(