From cb4cffe9aae7b1e345a2cf01811c60c7ef7c2d25 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 8 Nov 2018 01:10:48 +0100 Subject: paddle::php_serialize(): Take an `IntoIterator` argument Replace the `ExactSizeIterator` with an `IntoIterator`, as I wasn't able to pass in a `HashMap` with `ExactSizeIterator`. While we lose the convenience of the `len()` method, it's easy enough to just count the length of the iterator while we're serializing the entries within it. --- license-generator/paddle/src/lib.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'license-generator/paddle/src/lib.rs') diff --git a/license-generator/paddle/src/lib.rs b/license-generator/paddle/src/lib.rs index ebb0f01..d153895 100644 --- a/license-generator/paddle/src/lib.rs +++ b/license-generator/paddle/src/lib.rs @@ -1,17 +1,14 @@ // https://paddle.com/docs/reference-verifying-webhooks/ fn verify_signature<'a, I>(params: I) -> bool -where I: ExactSizeIterator { +where I: IntoIterator { false } fn php_serialize<'a, I>(pairs: I) -> String -where I: ExactSizeIterator { +where I: IntoIterator { let mut serialized = String::with_capacity(500); - serialized.push_str( - &format!("a:{pairs_count}:{{", pairs_count = pairs.len()) - ); - + let mut len = 0; for (key, value) in pairs { serialized.push_str( &format!( @@ -22,11 +19,17 @@ where I: ExactSizeIterator { value = value ) ); + + len += 1; } serialized.push_str("}"); - serialized + format!( + "a:{pairs_count}:{{{rest}", + pairs_count = len, + rest = serialized + ) } -- cgit v1.2.3