Skip to content

Commit bb7bc76

Browse files
committed
Use a more idiomatic rust pattern (iterators)
1 parent 835f6c9 commit bb7bc76

File tree

1 file changed

+113
-80
lines changed

1 file changed

+113
-80
lines changed

crates/bitwarden-exporters/src/cxf/identity.rs

Lines changed: 113 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,44 @@ pub fn passport_to_identity(passport: &PassportCredential) -> (Identity, Vec<Fie
8888
};
8989

9090
// Create custom fields for unmapped data according to CXF mapping document
91-
let mut custom_fields = Vec::new();
92-
93-
if let Some(issuing_country) = &passport.issuing_country {
94-
custom_fields.push(create_field("Issuing Country", issuing_country));
95-
}
96-
if let Some(nationality) = &passport.nationality {
97-
custom_fields.push(create_field("Nationality", nationality));
98-
}
99-
if let Some(birth_date) = &passport.birth_date {
100-
custom_fields.push(create_field("Birth Date", birth_date));
101-
}
102-
if let Some(birth_place) = &passport.birth_place {
103-
custom_fields.push(create_field("Birth Place", birth_place));
104-
}
105-
if let Some(sex) = &passport.sex {
106-
custom_fields.push(create_field("Sex", sex));
107-
}
108-
if let Some(issue_date) = &passport.issue_date {
109-
custom_fields.push(create_field("Issue Date", issue_date));
110-
}
111-
if let Some(expiry_date) = &passport.expiry_date {
112-
custom_fields.push(create_field("Expiry Date", expiry_date));
113-
}
114-
if let Some(issuing_authority) = &passport.issuing_authority {
115-
custom_fields.push(create_field("Issuing Authority", issuing_authority));
116-
}
117-
if let Some(passport_type) = &passport.passport_type {
118-
custom_fields.push(create_field("Passport Type", passport_type));
119-
}
91+
let custom_fields = [
92+
passport
93+
.issuing_country
94+
.as_ref()
95+
.map(|issuing_country| create_field("Issuing Country", issuing_country)),
96+
passport
97+
.nationality
98+
.as_ref()
99+
.map(|nationality| create_field("Nationality", nationality)),
100+
passport
101+
.birth_date
102+
.as_ref()
103+
.map(|birth_date| create_field("Birth Date", birth_date)),
104+
passport
105+
.birth_place
106+
.as_ref()
107+
.map(|birth_place| create_field("Birth Place", birth_place)),
108+
passport.sex.as_ref().map(|sex| create_field("Sex", sex)),
109+
passport
110+
.issue_date
111+
.as_ref()
112+
.map(|issue_date| create_field("Issue Date", issue_date)),
113+
passport
114+
.expiry_date
115+
.as_ref()
116+
.map(|expiry_date| create_field("Expiry Date", expiry_date)),
117+
passport
118+
.issuing_authority
119+
.as_ref()
120+
.map(|issuing_authority| create_field("Issuing Authority", issuing_authority)),
121+
passport
122+
.passport_type
123+
.as_ref()
124+
.map(|passport_type| create_field("Passport Type", passport_type)),
125+
]
126+
.into_iter()
127+
.flatten()
128+
.collect();
120129

121130
(identity, custom_fields)
122131
}
@@ -175,14 +184,19 @@ pub fn person_name_to_identity(person_name: &PersonNameCredential) -> (Identity,
175184
};
176185

177186
// Create custom fields for unmapped data
178-
let mut custom_fields = Vec::new();
179-
180-
if let Some(given_informal) = &person_name.given_informal {
181-
custom_fields.push(create_field("Informal Given Name", given_informal));
182-
}
183-
if let Some(generation) = &person_name.generation {
184-
custom_fields.push(create_field("Generation", generation));
185-
}
187+
let custom_fields = [
188+
person_name
189+
.given_informal
190+
.as_ref()
191+
.map(|given_informal| create_field("Informal Given Name", given_informal)),
192+
person_name
193+
.generation
194+
.as_ref()
195+
.map(|generation| create_field("Generation", generation)),
196+
]
197+
.into_iter()
198+
.flatten()
199+
.collect();
186200

187201
(identity, custom_fields)
188202
}
@@ -243,23 +257,31 @@ pub fn drivers_license_to_identity(
243257
};
244258

245259
// Create custom fields for unmapped data according to CXF mapping document
246-
let mut custom_fields = Vec::new();
247-
248-
if let Some(birth_date) = &drivers_license.birth_date {
249-
custom_fields.push(create_field("Birth Date", birth_date));
250-
}
251-
if let Some(issue_date) = &drivers_license.issue_date {
252-
custom_fields.push(create_field("Issue Date", issue_date));
253-
}
254-
if let Some(expiry_date) = &drivers_license.expiry_date {
255-
custom_fields.push(create_field("Expiry Date", expiry_date));
256-
}
257-
if let Some(issuing_authority) = &drivers_license.issuing_authority {
258-
custom_fields.push(create_field("Issuing Authority", issuing_authority));
259-
}
260-
if let Some(license_class) = &drivers_license.license_class {
261-
custom_fields.push(create_field("License Class", license_class));
262-
}
260+
let custom_fields = [
261+
drivers_license
262+
.birth_date
263+
.as_ref()
264+
.map(|birth_date| create_field("Birth Date", birth_date)),
265+
drivers_license
266+
.issue_date
267+
.as_ref()
268+
.map(|issue_date| create_field("Issue Date", issue_date)),
269+
drivers_license
270+
.expiry_date
271+
.as_ref()
272+
.map(|expiry_date| create_field("Expiry Date", expiry_date)),
273+
drivers_license
274+
.issuing_authority
275+
.as_ref()
276+
.map(|issuing_authority| create_field("Issuing Authority", issuing_authority)),
277+
drivers_license
278+
.license_class
279+
.as_ref()
280+
.map(|license_class| create_field("License Class", license_class)),
281+
]
282+
.into_iter()
283+
.flatten()
284+
.collect();
263285

264286
(identity, custom_fields)
265287
}
@@ -321,32 +343,43 @@ pub fn identity_document_to_identity(
321343
};
322344

323345
// Create custom fields for unmapped data according to CXF mapping document
324-
let mut custom_fields = Vec::new();
325-
326-
if let Some(issuing_country) = &identity_document.issuing_country {
327-
custom_fields.push(create_field("Issuing Country", issuing_country));
328-
}
329-
if let Some(nationality) = &identity_document.nationality {
330-
custom_fields.push(create_field("Nationality", nationality));
331-
}
332-
if let Some(birth_date) = &identity_document.birth_date {
333-
custom_fields.push(create_field("Birth Date", birth_date));
334-
}
335-
if let Some(birth_place) = &identity_document.birth_place {
336-
custom_fields.push(create_field("Birth Place", birth_place));
337-
}
338-
if let Some(sex) = &identity_document.sex {
339-
custom_fields.push(create_field("Sex", sex));
340-
}
341-
if let Some(issue_date) = &identity_document.issue_date {
342-
custom_fields.push(create_field("Issue Date", issue_date));
343-
}
344-
if let Some(expiry_date) = &identity_document.expiry_date {
345-
custom_fields.push(create_field("Expiry Date", expiry_date));
346-
}
347-
if let Some(issuing_authority) = &identity_document.issuing_authority {
348-
custom_fields.push(create_field("Issuing Authority", issuing_authority));
349-
}
346+
let custom_fields = [
347+
identity_document
348+
.issuing_country
349+
.as_ref()
350+
.map(|issuing_country| create_field("Issuing Country", issuing_country)),
351+
identity_document
352+
.nationality
353+
.as_ref()
354+
.map(|nationality| create_field("Nationality", nationality)),
355+
identity_document
356+
.birth_date
357+
.as_ref()
358+
.map(|birth_date| create_field("Birth Date", birth_date)),
359+
identity_document
360+
.birth_place
361+
.as_ref()
362+
.map(|birth_place| create_field("Birth Place", birth_place)),
363+
identity_document
364+
.sex
365+
.as_ref()
366+
.map(|sex| create_field("Sex", sex)),
367+
identity_document
368+
.issue_date
369+
.as_ref()
370+
.map(|issue_date| create_field("Issue Date", issue_date)),
371+
identity_document
372+
.expiry_date
373+
.as_ref()
374+
.map(|expiry_date| create_field("Expiry Date", expiry_date)),
375+
identity_document
376+
.issuing_authority
377+
.as_ref()
378+
.map(|issuing_authority| create_field("Issuing Authority", issuing_authority)),
379+
]
380+
.into_iter()
381+
.flatten()
382+
.collect();
350383
// Note: identity-document doesn't have a document_type field in the CXF example
351384

352385
(identity, custom_fields)

0 commit comments

Comments
 (0)