Skip to content

Commit 755dd28

Browse files
author
Fahad Zubair
committed
Simplify the match statements
1 parent fce2d6d commit 755dd28

File tree

1 file changed

+44
-34
lines changed
  • rust-runtime/aws-smithy-protocol-test/src

1 file changed

+44
-34
lines changed

rust-runtime/aws-smithy-protocol-test/src/lib.rs

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -424,45 +424,50 @@ fn cbor_values_equal(
424424
a: &ciborium::value::Value,
425425
b: &ciborium::value::Value,
426426
) -> Result<bool, ProtocolTestFailure> {
427-
let result = match (a, b) {
427+
match (a, b) {
428428
(ciborium::value::Value::Array(a_array), ciborium::value::Value::Array(b_array)) => {
429429
// Both arrays should be equal in size.
430-
a_array.len() == b_array.len() &&
430+
if a_array.len() != b_array.len() {
431+
return Ok(false);
432+
}
431433
// Compare arrays element-wise.
432-
a_array.iter().zip(b_array.iter()).try_fold(true, |acc, (a_elem, b_elem)| {
433-
cbor_values_equal(a_elem, b_elem).map(|equal| acc && equal)
434-
})?
434+
for (a_elem, b_elem) in a_array.iter().zip(b_array.iter()) {
435+
if !cbor_values_equal(a_elem, b_elem)? {
436+
return Ok(false);
437+
}
438+
}
439+
Ok(true)
435440
}
436441

437442
// Convert `ciborium::value::Value::Map` to a `HashMap`, and then compare the values of
438443
// each key in `a` with those in `b`.
439444
(ciborium::value::Value::Map(a_map), ciborium::value::Value::Map(b_map)) => {
440-
let a_hashmap = ciborium_map_to_hashmap(a_map)?;
441-
let b_hashmap = ciborium_map_to_hashmap(b_map)?;
445+
if a_map.len() != b_map.len() {
446+
return Ok(false);
447+
}
442448

443-
if a_hashmap.len() != b_hashmap.len() {
444-
false
445-
} else {
446-
// Each key in `a` should exist in `b`, and the values should match.
447-
a_hashmap.iter().try_fold(true, |acc, (a_key, a_value)| {
448-
b_hashmap
449-
.get(a_key)
450-
.map(|b_value| {
451-
cbor_values_equal(a_value, b_value).map(|equal| acc && equal)
452-
})
453-
.unwrap_or(Ok(false))
454-
})?
449+
let b_hashmap = ciborium_map_to_hashmap(b_map)?;
450+
// Each key in `a` should exist in `b`, and the values should match.
451+
for a_key_value in a_map.iter() {
452+
let (a_key, a_value) = get_text_key_value(a_key_value)?;
453+
match b_hashmap.get(a_key) {
454+
Some(b_value) => {
455+
if !cbor_values_equal(a_value, b_value)? {
456+
return Ok(false);
457+
}
458+
}
459+
None => return Ok(false),
460+
}
455461
}
462+
Ok(true)
456463
}
457464

458465
(ciborium::value::Value::Float(a_float), ciborium::value::Value::Float(b_float)) => {
459-
a_float == b_float || a_float.is_nan() && b_float.is_nan()
466+
Ok(a_float == b_float || (a_float.is_nan() && b_float.is_nan()))
460467
}
461468

462-
_ => a == b,
463-
};
464-
465-
Ok(result)
469+
_ => Ok(a == b),
470+
}
466471
}
467472

468473
/// Converts a `ciborium::value::Value::Map` into a `HashMap<&String, &ciborium::value::Value>`.
@@ -475,16 +480,21 @@ fn cbor_values_equal(
475480
fn ciborium_map_to_hashmap(
476481
cbor_map: &[(ciborium::value::Value, ciborium::value::Value)],
477482
) -> Result<std::collections::HashMap<&String, &ciborium::value::Value>, ProtocolTestFailure> {
478-
cbor_map
479-
.iter()
480-
.map(|(key, value)| match key {
481-
ciborium::value::Value::Text(key_str) => Ok((key_str, value)),
482-
_ => Err(ProtocolTestFailure::InvalidBodyFormat {
483-
expected: "a text key as map entry".to_string(),
484-
found: format!("{:?}", key),
485-
}),
486-
})
487-
.collect()
483+
cbor_map.iter().map(get_text_key_value).collect()
484+
}
485+
486+
/// Extracts a string key and its associated value from a CBOR key-value pair.
487+
/// Returns a `ProtocolTestFailure::InvalidBodyFormat` error if the key is not a text value.
488+
fn get_text_key_value(
489+
(key, value): &(ciborium::value::Value, ciborium::value::Value),
490+
) -> Result<(&String, &ciborium::value::Value), ProtocolTestFailure> {
491+
match key {
492+
ciborium::value::Value::Text(key_str) => Ok((key_str, value)),
493+
_ => Err(ProtocolTestFailure::InvalidBodyFormat {
494+
expected: "a text key as map entry".to_string(),
495+
found: format!("{:?}", key),
496+
}),
497+
}
488498
}
489499

490500
fn try_cbor_eq<T: AsRef<[u8]> + Debug>(

0 commit comments

Comments
 (0)