|
1 |
| -use bitwarden_vault::FieldType; |
2 | 1 | use credential_exchange_format::{
|
3 |
| - AddressCredential, DriversLicenseCredential, EditableField, EditableFieldCountryCode, |
4 |
| - EditableFieldDate, EditableFieldString, IdentityDocumentCredential, PassportCredential, |
| 2 | + AddressCredential, DriversLicenseCredential, IdentityDocumentCredential, PassportCredential, |
5 | 3 | PersonNameCredential,
|
6 | 4 | };
|
7 | 5 |
|
8 |
| -use crate::{Field, Identity}; |
9 |
| - |
10 |
| -/// Helper trait to extract value from various EditableField types |
11 |
| -trait ExtractValue { |
12 |
| - fn extract_value(&self) -> String; |
13 |
| -} |
14 |
| - |
15 |
| -impl ExtractValue for EditableField<EditableFieldString> { |
16 |
| - fn extract_value(&self) -> String { |
17 |
| - self.value.0.clone() |
18 |
| - } |
19 |
| -} |
20 |
| - |
21 |
| -impl ExtractValue for EditableField<EditableFieldCountryCode> { |
22 |
| - fn extract_value(&self) -> String { |
23 |
| - self.value.0.clone() |
24 |
| - } |
25 |
| -} |
26 |
| - |
27 |
| -impl ExtractValue for EditableField<EditableFieldDate> { |
28 |
| - fn extract_value(&self) -> String { |
29 |
| - self.value.0.to_string() |
30 |
| - } |
31 |
| -} |
32 |
| - |
33 |
| -/// Generic helper function to create a custom field from any EditableField type |
34 |
| -fn create_custom_field<T: ExtractValue>( |
35 |
| - editable_field: Option<&T>, |
36 |
| - field_name: &str, |
37 |
| -) -> Option<Field> { |
38 |
| - editable_field.map(|field| Field { |
39 |
| - name: Some(field_name.to_string()), |
40 |
| - value: Some(field.extract_value()), |
41 |
| - r#type: FieldType::Text as u8, |
42 |
| - linked_id: None, |
43 |
| - }) |
44 |
| -} |
| 6 | +use crate::{cxf::editable_field::create_field, Field, Identity}; |
45 | 7 |
|
46 | 8 | /// Convert address credentials to Identity (no custom fields needed for address)
|
47 | 9 | /// According to the mapping specification:
|
@@ -128,34 +90,32 @@ pub fn passport_to_identity(passport: &PassportCredential) -> (Identity, Vec<Fie
|
128 | 90 | // Create custom fields for unmapped data according to CXF mapping document
|
129 | 91 | let mut custom_fields = Vec::new();
|
130 | 92 |
|
131 |
| - if let Some(field) = create_custom_field(passport.issuing_country.as_ref(), "Issuing Country") { |
132 |
| - custom_fields.push(field); |
| 93 | + if let Some(issuing_country) = &passport.issuing_country { |
| 94 | + custom_fields.push(create_field("Issuing Country", issuing_country)); |
133 | 95 | }
|
134 |
| - if let Some(field) = create_custom_field(passport.nationality.as_ref(), "Nationality") { |
135 |
| - custom_fields.push(field); |
| 96 | + if let Some(nationality) = &passport.nationality { |
| 97 | + custom_fields.push(create_field("Nationality", nationality)); |
136 | 98 | }
|
137 |
| - if let Some(field) = create_custom_field(passport.birth_date.as_ref(), "Birth Date") { |
138 |
| - custom_fields.push(field); |
| 99 | + if let Some(birth_date) = &passport.birth_date { |
| 100 | + custom_fields.push(create_field("Birth Date", birth_date)); |
139 | 101 | }
|
140 |
| - if let Some(field) = create_custom_field(passport.birth_place.as_ref(), "Birth Place") { |
141 |
| - custom_fields.push(field); |
| 102 | + if let Some(birth_place) = &passport.birth_place { |
| 103 | + custom_fields.push(create_field("Birth Place", birth_place)); |
142 | 104 | }
|
143 |
| - if let Some(field) = create_custom_field(passport.sex.as_ref(), "Sex") { |
144 |
| - custom_fields.push(field); |
| 105 | + if let Some(sex) = &passport.sex { |
| 106 | + custom_fields.push(create_field("Sex", sex)); |
145 | 107 | }
|
146 |
| - if let Some(field) = create_custom_field(passport.issue_date.as_ref(), "Issue Date") { |
147 |
| - custom_fields.push(field); |
| 108 | + if let Some(issue_date) = &passport.issue_date { |
| 109 | + custom_fields.push(create_field("Issue Date", issue_date)); |
148 | 110 | }
|
149 |
| - if let Some(field) = create_custom_field(passport.expiry_date.as_ref(), "Expiry Date") { |
150 |
| - custom_fields.push(field); |
| 111 | + if let Some(expiry_date) = &passport.expiry_date { |
| 112 | + custom_fields.push(create_field("Expiry Date", expiry_date)); |
151 | 113 | }
|
152 |
| - if let Some(field) = |
153 |
| - create_custom_field(passport.issuing_authority.as_ref(), "Issuing Authority") |
154 |
| - { |
155 |
| - custom_fields.push(field); |
| 114 | + if let Some(issuing_authority) = &passport.issuing_authority { |
| 115 | + custom_fields.push(create_field("Issuing Authority", issuing_authority)); |
156 | 116 | }
|
157 |
| - if let Some(field) = create_custom_field(passport.passport_type.as_ref(), "Passport Type") { |
158 |
| - custom_fields.push(field); |
| 117 | + if let Some(passport_type) = &passport.passport_type { |
| 118 | + custom_fields.push(create_field("Passport Type", passport_type)); |
159 | 119 | }
|
160 | 120 |
|
161 | 121 | (identity, custom_fields)
|
@@ -217,13 +177,11 @@ pub fn person_name_to_identity(person_name: &PersonNameCredential) -> (Identity,
|
217 | 177 | // Create custom fields for unmapped data
|
218 | 178 | let mut custom_fields = Vec::new();
|
219 | 179 |
|
220 |
| - if let Some(field) = |
221 |
| - create_custom_field(person_name.given_informal.as_ref(), "Informal Given Name") |
222 |
| - { |
223 |
| - custom_fields.push(field); |
| 180 | + if let Some(given_informal) = &person_name.given_informal { |
| 181 | + custom_fields.push(create_field("Informal Given Name", given_informal)); |
224 | 182 | }
|
225 |
| - if let Some(field) = create_custom_field(person_name.generation.as_ref(), "Generation") { |
226 |
| - custom_fields.push(field); |
| 183 | + if let Some(generation) = &person_name.generation { |
| 184 | + custom_fields.push(create_field("Generation", generation)); |
227 | 185 | }
|
228 | 186 |
|
229 | 187 | (identity, custom_fields)
|
@@ -287,25 +245,20 @@ pub fn drivers_license_to_identity(
|
287 | 245 | // Create custom fields for unmapped data according to CXF mapping document
|
288 | 246 | let mut custom_fields = Vec::new();
|
289 | 247 |
|
290 |
| - if let Some(field) = create_custom_field(drivers_license.birth_date.as_ref(), "Birth Date") { |
291 |
| - custom_fields.push(field); |
| 248 | + if let Some(birth_date) = &drivers_license.birth_date { |
| 249 | + custom_fields.push(create_field("Birth Date", birth_date)); |
292 | 250 | }
|
293 |
| - if let Some(field) = create_custom_field(drivers_license.issue_date.as_ref(), "Issue Date") { |
294 |
| - custom_fields.push(field); |
| 251 | + if let Some(issue_date) = &drivers_license.issue_date { |
| 252 | + custom_fields.push(create_field("Issue Date", issue_date)); |
295 | 253 | }
|
296 |
| - if let Some(field) = create_custom_field(drivers_license.expiry_date.as_ref(), "Expiry Date") { |
297 |
| - custom_fields.push(field); |
| 254 | + if let Some(expiry_date) = &drivers_license.expiry_date { |
| 255 | + custom_fields.push(create_field("Expiry Date", expiry_date)); |
298 | 256 | }
|
299 |
| - if let Some(field) = create_custom_field( |
300 |
| - drivers_license.issuing_authority.as_ref(), |
301 |
| - "Issuing Authority", |
302 |
| - ) { |
303 |
| - custom_fields.push(field); |
| 257 | + if let Some(issuing_authority) = &drivers_license.issuing_authority { |
| 258 | + custom_fields.push(create_field("Issuing Authority", issuing_authority)); |
304 | 259 | }
|
305 |
| - if let Some(field) = |
306 |
| - create_custom_field(drivers_license.license_class.as_ref(), "License Class") |
307 |
| - { |
308 |
| - custom_fields.push(field); |
| 260 | + if let Some(license_class) = &drivers_license.license_class { |
| 261 | + custom_fields.push(create_field("License Class", license_class)); |
309 | 262 | }
|
310 | 263 |
|
311 | 264 | (identity, custom_fields)
|
@@ -370,38 +323,29 @@ pub fn identity_document_to_identity(
|
370 | 323 | // Create custom fields for unmapped data according to CXF mapping document
|
371 | 324 | let mut custom_fields = Vec::new();
|
372 | 325 |
|
373 |
| - if let Some(field) = create_custom_field( |
374 |
| - identity_document.issuing_country.as_ref(), |
375 |
| - "Issuing Country", |
376 |
| - ) { |
377 |
| - custom_fields.push(field); |
| 326 | + if let Some(issuing_country) = &identity_document.issuing_country { |
| 327 | + custom_fields.push(create_field("Issuing Country", issuing_country)); |
378 | 328 | }
|
379 |
| - if let Some(field) = create_custom_field(identity_document.nationality.as_ref(), "Nationality") |
380 |
| - { |
381 |
| - custom_fields.push(field); |
| 329 | + if let Some(nationality) = &identity_document.nationality { |
| 330 | + custom_fields.push(create_field("Nationality", nationality)); |
382 | 331 | }
|
383 |
| - if let Some(field) = create_custom_field(identity_document.birth_date.as_ref(), "Birth Date") { |
384 |
| - custom_fields.push(field); |
| 332 | + if let Some(birth_date) = &identity_document.birth_date { |
| 333 | + custom_fields.push(create_field("Birth Date", birth_date)); |
385 | 334 | }
|
386 |
| - if let Some(field) = create_custom_field(identity_document.birth_place.as_ref(), "Birth Place") |
387 |
| - { |
388 |
| - custom_fields.push(field); |
| 335 | + if let Some(birth_place) = &identity_document.birth_place { |
| 336 | + custom_fields.push(create_field("Birth Place", birth_place)); |
389 | 337 | }
|
390 |
| - if let Some(field) = create_custom_field(identity_document.sex.as_ref(), "Sex") { |
391 |
| - custom_fields.push(field); |
| 338 | + if let Some(sex) = &identity_document.sex { |
| 339 | + custom_fields.push(create_field("Sex", sex)); |
392 | 340 | }
|
393 |
| - if let Some(field) = create_custom_field(identity_document.issue_date.as_ref(), "Issue Date") { |
394 |
| - custom_fields.push(field); |
| 341 | + if let Some(issue_date) = &identity_document.issue_date { |
| 342 | + custom_fields.push(create_field("Issue Date", issue_date)); |
395 | 343 | }
|
396 |
| - if let Some(field) = create_custom_field(identity_document.expiry_date.as_ref(), "Expiry Date") |
397 |
| - { |
398 |
| - custom_fields.push(field); |
| 344 | + if let Some(expiry_date) = &identity_document.expiry_date { |
| 345 | + custom_fields.push(create_field("Expiry Date", expiry_date)); |
399 | 346 | }
|
400 |
| - if let Some(field) = create_custom_field( |
401 |
| - identity_document.issuing_authority.as_ref(), |
402 |
| - "Issuing Authority", |
403 |
| - ) { |
404 |
| - custom_fields.push(field); |
| 347 | + if let Some(issuing_authority) = &identity_document.issuing_authority { |
| 348 | + custom_fields.push(create_field("Issuing Authority", issuing_authority)); |
405 | 349 | }
|
406 | 350 | // Note: identity-document doesn't have a document_type field in the CXF example
|
407 | 351 |
|
|
0 commit comments