Skip to content

Commit

Permalink
[#8] Implement IntoIterator for AddressParserResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
kodemartin committed Feb 16, 2022
1 parent 774a5a2 commit 0125313
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! ```
use std::collections::HashMap;
use std::ffi::{CStr, CString, NulError};
use std::vec::IntoIter;
use std::slice::Iter;

use crate::ffi;
Expand All @@ -40,6 +41,16 @@ impl AddressParserResponse {
}
}

impl IntoIterator for AddressParserResponse {
type Item = (String, String);
type IntoIter = std::iter::Zip<IntoIter<String>, IntoIter<String>>;

/// Iterates over `(label, token)` pairs by consuming the value.
fn into_iter(self) -> Self::IntoIter {
self.labels.into_iter().zip(self.tokens.into_iter())
}
}

impl<'a> IntoIterator for &'a AddressParserResponse {
type Item = (&'a String, &'a String);
type IntoIter = std::iter::Zip<Iter<'a, String>, Iter<'a, String>>;
Expand Down Expand Up @@ -290,10 +301,8 @@ impl From<AddressParserResponse> for ParsedAddress {
/// Create a new `ParsedAddress` from an `AddressParserResponse`.
fn from(response: AddressParserResponse) -> Self {
let mut parsed_address = ParsedAddress::default();
for (label, token) in &response {
parsed_address
.label_to_token
.insert(label.clone(), token.clone());
for (label, token) in response {
parsed_address.label_to_token.insert(label, token);
}
parsed_address
}
Expand Down
4 changes: 2 additions & 2 deletions tests/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use rustpostal::LibModules;

fn assert_actual_eq_expected(address: &str, expected: Vec<(&str, &str)>) {
let response = rustpostal::address::parse_address(address, None, None).unwrap();
let actual: Vec<(&str, &str)> = response
let actual: Vec<(&str, &str)> = (&response)
.into_iter()
.map(|(l, t)| (l.as_str(), t.as_str()))
.map(|(l, t)| (l.as_ref(), t.as_ref()))
.collect();
assert_eq!(actual, expected);
}
Expand Down

0 comments on commit 0125313

Please sign in to comment.