| 1 |
//! JSContact for Contact |
| 2 |
use std::collections::HashMap; |
| 3 |
|
| 4 |
use serde::{Deserialize, Serialize}; |
| 5 |
|
| 6 |
use crate::contact::{Contact, Localizable, NameParts, PostalAddress}; |
| 7 |
|
| 8 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 9 |
pub struct JsContactCard { |
| 10 |
#[serde(rename = "@type")] |
| 11 |
pub card_type: String, |
| 12 |
|
| 13 |
pub version: String, |
| 14 |
|
| 15 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 16 |
pub kind: Option<String>, |
| 17 |
|
| 18 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 19 |
pub language: Option<String>, |
| 20 |
|
| 21 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 22 |
pub organizations: Option<Organizations>, |
| 23 |
|
| 24 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 25 |
pub name: Option<Name>, |
| 26 |
|
| 27 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 28 |
pub addresses: Option<Addresses>, |
| 29 |
|
| 30 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 31 |
pub phones: Option<Phones>, |
| 32 |
|
| 33 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 34 |
pub emails: Option<Emails>, |
| 35 |
|
| 36 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 37 |
pub links: Option<Links>, |
| 38 |
|
| 39 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 40 |
pub localizations: Option<HashMap<String, Localization>>, |
| 41 |
} |
| 42 |
|
| 43 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 44 |
pub struct Localization { |
| 45 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 46 |
pub organizations: Option<Organizations>, |
| 47 |
|
| 48 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 49 |
pub name: Option<Name>, |
| 50 |
|
| 51 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 52 |
pub addresses: Option<Addresses>, |
| 53 |
} |
| 54 |
|
| 55 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 56 |
pub struct Organizations { |
| 57 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 58 |
pub org: Option<Org>, |
| 59 |
} |
| 60 |
|
| 61 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 62 |
pub struct Org { |
| 63 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 64 |
pub name: Option<String>, |
| 65 |
} |
| 66 |
|
| 67 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 68 |
pub struct Name { |
| 69 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 70 |
pub full: Option<String>, |
| 71 |
|
| 72 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 73 |
pub components: Option<Vec<KindValue>>, |
| 74 |
} |
| 75 |
|
| 76 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 77 |
pub struct KindValue { |
| 78 |
pub kind: String, |
| 79 |
pub value: String, |
| 80 |
} |
| 81 |
|
| 82 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 83 |
pub struct Addresses { |
| 84 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 85 |
pub addr: Option<Address>, |
| 86 |
|
| 87 |
#[serde(rename = "addresses-1")] |
| 88 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 89 |
pub address_one: Option<Address>, |
| 90 |
} |
| 91 |
|
| 92 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 93 |
pub struct Address { |
| 94 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 95 |
#[serde(flatten)] |
| 96 |
pub name: Option<Name>, |
| 97 |
|
| 98 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 99 |
#[serde(rename = "countryCode")] |
| 100 |
pub country_code: Option<String>, |
| 101 |
} |
| 102 |
|
| 103 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 104 |
pub struct Features { |
| 105 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 106 |
pub voice: Option<bool>, |
| 107 |
|
| 108 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 109 |
pub fax: Option<bool>, |
| 110 |
} |
| 111 |
|
| 112 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 113 |
pub struct Phone { |
| 114 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 115 |
pub features: Option<Features>, |
| 116 |
|
| 117 |
pub number: String, |
| 118 |
} |
| 119 |
|
| 120 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 121 |
pub struct Phones { |
| 122 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 123 |
pub voice: Option<Phone>, |
| 124 |
|
| 125 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 126 |
pub fax: Option<Phone>, |
| 127 |
} |
| 128 |
|
| 129 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 130 |
pub struct Emails { |
| 131 |
pub email: Email, |
| 132 |
} |
| 133 |
|
| 134 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 135 |
pub struct Email { |
| 136 |
pub address: String, |
| 137 |
} |
| 138 |
|
| 139 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 140 |
pub struct Url { |
| 141 |
pub uri: String, |
| 142 |
} |
| 143 |
|
| 144 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 145 |
pub struct ContactUri { |
| 146 |
pub kind: String, |
| 147 |
|
| 148 |
pub uri: String, |
| 149 |
} |
| 150 |
|
| 151 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 152 |
pub struct Links { |
| 153 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 154 |
pub url: Option<Url>, |
| 155 |
|
| 156 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 157 |
#[serde(rename = "contact-uri")] |
| 158 |
pub contact_uri: Option<ContactUri>, |
| 159 |
} |
| 160 |
|
| 161 |
impl Contact { |
| 162 |
pub fn to_jscontact(&self) -> JsContactCard { |
| 163 |
JsContactCard { |
| 164 |
card_type: "Card".to_string(), |
| 165 |
version: "2.0".to_string(), |
| 166 |
kind: self.kind().map(|s| s.to_string()), |
| 167 |
language: self.lang().map(|l| l.tag().to_string()), |
| 168 |
organizations: org_to_jscontact(self.organization_name()), |
| 169 |
name: name_to_jscontact(self.full_name(), self.name_parts()), |
| 170 |
addresses: addresses_to_jscontact(self.postal_addresses()), |
| 171 |
phones: phones_to_jscontact(self.prefer_voice_phone(), self.fax_phone()), |
| 172 |
emails: emails_to_jscontact(self.emails()), |
| 173 |
links: links_to_jscontact(self), |
| 174 |
localizations: if self.localizations_is_empty() { |
| 175 |
None |
| 176 |
} else { |
| 177 |
let mut hm = HashMap::new(); |
| 178 |
for (tag, local) in self.localizations_iter() { |
| 179 |
hm.insert(tag.to_owned(), local.to_localization()); |
| 180 |
} |
| 181 |
Some(hm) |
| 182 |
}, |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
pub fn from_jscontact(jscontact: &JsContactCard) -> Contact { |
| 187 |
let mut builder = Contact::builder(); |
| 188 |
if let Some(lang) = &jscontact.language { |
| 189 |
builder = builder.lang(super::Lang { |
| 190 |
preference: None, |
| 191 |
tag: lang.to_owned(), |
| 192 |
}); |
| 193 |
}; |
| 194 |
if let Some(phones) = &jscontact.phones { |
| 195 |
if let Some(voice) = &phones.voice { |
| 196 |
builder = builder.phone( |
| 197 |
super::Phone::builder() |
| 198 |
.phone(voice.number.clone()) |
| 199 |
.feature("voice".to_string()) |
| 200 |
.build(), |
| 201 |
) |
| 202 |
} |
| 203 |
if let Some(fax) = &phones.fax { |
| 204 |
builder = builder.phone( |
| 205 |
super::Phone::builder() |
| 206 |
.phone(fax.number.clone()) |
| 207 |
.feature("fax".to_string()) |
| 208 |
.build(), |
| 209 |
) |
| 210 |
} |
| 211 |
}; |
| 212 |
if let Some(emails) = &jscontact.emails { |
| 213 |
builder = builder.email( |
| 214 |
super::Email::builder() |
| 215 |
.email(emails.email.address.to_owned()) |
| 216 |
.build(), |
| 217 |
) |
| 218 |
}; |
| 219 |
if let Some(links) = &jscontact.links { |
| 220 |
if let Some(url) = &links.url { |
| 221 |
builder = builder.url(url.uri.to_owned()) |
| 222 |
} |
| 223 |
if let Some(contact_uri) = &links.contact_uri { |
| 224 |
builder = builder.url(contact_uri.uri.to_owned()) |
| 225 |
} |
| 226 |
} |
| 227 |
let builder = builder |
| 228 |
.and_kind(jscontact.kind.as_ref().map(|s| s.to_owned())) |
| 229 |
.organization_names(jscontact_to_org_names(&jscontact.organizations)) |
| 230 |
.and_name_parts(jscontact_to_nameparts(&jscontact.name)) |
| 231 |
.postal_addresses(jscontact_to_postaladdresses(&jscontact.addresses)) |
| 232 |
.and_full_name(jscontact_to_fullname(&jscontact.name)); |
| 233 |
let mut contact = builder.build(); |
| 234 |
if let Some(localizations) = &jscontact.localizations { |
| 235 |
for (tag, local) in localizations { |
| 236 |
contact = |
| 237 |
contact.with_localization(tag.to_owned(), Localizable::from_jscontact(local)); |
| 238 |
} |
| 239 |
} |
| 240 |
contact |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
impl Localizable { |
| 245 |
pub fn to_localization(&self) -> Localization { |
| 246 |
Localization { |
| 247 |
organizations: org_to_jscontact(self.organization_name()), |
| 248 |
name: name_to_jscontact(self.full_name(), self.name_parts()), |
| 249 |
addresses: addresses_to_jscontact(self.postal_addresses()), |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
pub fn from_jscontact(localization: &Localization) -> Self { |
| 254 |
Localizable::builder() |
| 255 |
.organization_names(jscontact_to_org_names(&localization.organizations)) |
| 256 |
.and_name_parts(jscontact_to_nameparts(&localization.name)) |
| 257 |
.postal_addresses(jscontact_to_postaladdresses(&localization.addresses)) |
| 258 |
.and_full_name(jscontact_to_fullname(&localization.name)) |
| 259 |
.build() |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
fn jscontact_to_postaladdresses(addresses: &Option<Addresses>) -> Vec<PostalAddress> { |
| 264 |
let mut postal_addresses = vec![]; |
| 265 |
if let Some(addresses) = addresses { |
| 266 |
if let Some(addr) = &addresses.addr { |
| 267 |
postal_addresses.push(address_to_postaladdress(addr)); |
| 268 |
} |
| 269 |
if let Some(addr) = &addresses.address_one { |
| 270 |
postal_addresses.push(address_to_postaladdress(addr)); |
| 271 |
} |
| 272 |
} |
| 273 |
postal_addresses |
| 274 |
} |
| 275 |
|
| 276 |
fn address_to_postaladdress(addr: &Address) -> PostalAddress { |
| 277 |
let mut street_parts = vec![]; |
| 278 |
let mut locality = None; |
| 279 |
let mut region = None; |
| 280 |
let mut country = None; |
| 281 |
let mut postal_code = None; |
| 282 |
if let Some(Name { |
| 283 |
components: Some(components), |
| 284 |
.. |
| 285 |
}) = &addr.name |
| 286 |
{ |
| 287 |
for component in components { |
| 288 |
if component.kind.eq_ignore_ascii_case("name") { |
| 289 |
street_parts.push(component.value.to_owned()); |
| 290 |
} |
| 291 |
if component.kind.eq_ignore_ascii_case("locality") { |
| 292 |
locality = Some(component.value.to_owned()); |
| 293 |
} |
| 294 |
if component.kind.eq_ignore_ascii_case("region") { |
| 295 |
region = Some(component.value.to_owned()); |
| 296 |
} |
| 297 |
if component.kind.eq_ignore_ascii_case("country") { |
| 298 |
country = Some(component.value.to_owned()); |
| 299 |
} |
| 300 |
if component.kind.eq_ignore_ascii_case("postalcode") { |
| 301 |
postal_code = Some(component.value.to_owned()); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
let builder = PostalAddress::builder() |
| 306 |
.and_full_address(addr.name.as_ref().and_then(|n| n.full.to_owned())) |
| 307 |
.street_parts(street_parts) |
| 308 |
.and_locality(locality) |
| 309 |
.and_region_name(region) |
| 310 |
.and_country_name(country) |
| 311 |
.and_postal_code(postal_code) |
| 312 |
.and_country_code(addr.country_code.to_owned()); |
| 313 |
builder.build() |
| 314 |
} |
| 315 |
|
| 316 |
fn jscontact_to_nameparts(name: &Option<Name>) -> Option<NameParts> { |
| 317 |
if let Some(Name { |
| 318 |
components: Some(components), |
| 319 |
.. |
| 320 |
}) = name |
| 321 |
{ |
| 322 |
let mut np = NameParts::builder(); |
| 323 |
for component in components { |
| 324 |
if component.kind.eq_ignore_ascii_case("title") { |
| 325 |
np = np.prefix(component.value.to_owned()) |
| 326 |
} |
| 327 |
if component.kind.eq_ignore_ascii_case("given") |
| 328 |
|| component.kind.eq_ignore_ascii_case("given2") |
| 329 |
{ |
| 330 |
np = np.given_name(component.value.to_owned()) |
| 331 |
} |
| 332 |
if component.kind.eq_ignore_ascii_case("surname") |
| 333 |
|| component.kind.eq_ignore_ascii_case("surname2") |
| 334 |
{ |
| 335 |
np = np.surname(component.value.to_owned()) |
| 336 |
} |
| 337 |
if component.kind.eq_ignore_ascii_case("credentials") { |
| 338 |
np = np.suffix(component.value.to_owned()) |
| 339 |
} |
| 340 |
if component.kind.eq_ignore_ascii_case("generation") { |
| 341 |
np = np.generation(component.value.to_owned()) |
| 342 |
} |
| 343 |
} |
| 344 |
Some(np.build()) |
| 345 |
} else { |
| 346 |
None |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
fn jscontact_to_fullname(name: &Option<Name>) -> Option<String> { |
| 351 |
name.as_ref().and_then(|n| n.full.to_owned()) |
| 352 |
} |
| 353 |
|
| 354 |
fn jscontact_to_org_names(organizations: &Option<Organizations>) -> Vec<String> { |
| 355 |
organizations |
| 356 |
.as_ref() |
| 357 |
.and_then(|o| o.org.as_ref()) |
| 358 |
.and_then(|o| o.name.as_ref()) |
| 359 |
.map(|n| vec![n.to_owned()]) |
| 360 |
.unwrap_or_default() |
| 361 |
} |
| 362 |
|
| 363 |
fn org_to_jscontact(organization_name: Option<&str>) -> Option<Organizations> { |
| 364 |
organization_name.map(|org_name| Organizations { |
| 365 |
org: Some(Org { |
| 366 |
name: Some(org_name.to_owned()), |
| 367 |
}), |
| 368 |
}) |
| 369 |
} |
| 370 |
|
| 371 |
fn name_to_jscontact(full_name: Option<&str>, name_parts: Option<&NameParts>) -> Option<Name> { |
| 372 |
let has_full_name = full_name.map(|s| !s.is_empty()).unwrap_or(false); |
| 373 |
if !has_full_name && name_parts.is_none() { |
| 374 |
return None; |
| 375 |
} |
| 376 |
// else |
| 377 |
Some(Name { |
| 378 |
full: full_name.filter(|s| !s.is_empty()).map(|s| s.to_owned()), |
| 379 |
components: name_parts.map(|np| { |
| 380 |
let mut components = vec![]; |
| 381 |
if let Some(prefix) = np.prefix() { |
| 382 |
components.push(KindValue { |
| 383 |
kind: "title".to_string(), |
| 384 |
value: prefix.to_owned(), |
| 385 |
}) |
| 386 |
}; |
| 387 |
if let Some(given_name) = np.given_name() { |
| 388 |
components.push(KindValue { |
| 389 |
kind: "given".to_string(), |
| 390 |
value: given_name.to_owned(), |
| 391 |
}) |
| 392 |
}; |
| 393 |
if let Some(middle_name) = np.middle_name() { |
| 394 |
components.push(KindValue { |
| 395 |
kind: "given2".to_string(), |
| 396 |
value: middle_name.to_owned(), |
| 397 |
}) |
| 398 |
}; |
| 399 |
if let Some(surname) = np.surname() { |
| 400 |
components.push(KindValue { |
| 401 |
kind: "surname".to_string(), |
| 402 |
value: surname.to_owned(), |
| 403 |
}) |
| 404 |
}; |
| 405 |
if let Some(surname) = np.surnames().get(1) { |
| 406 |
components.push(KindValue { |
| 407 |
kind: "surname2".to_string(), |
| 408 |
value: surname.to_owned(), |
| 409 |
}) |
| 410 |
}; |
| 411 |
if let Some(suffix) = np.suffix() { |
| 412 |
components.push(KindValue { |
| 413 |
kind: "credential".to_string(), |
| 414 |
value: suffix.to_owned(), |
| 415 |
}) |
| 416 |
}; |
| 417 |
if let Some(generation) = np.generation() { |
| 418 |
components.push(KindValue { |
| 419 |
kind: "generation".to_string(), |
| 420 |
value: generation.to_owned(), |
| 421 |
}) |
| 422 |
}; |
| 423 |
components |
| 424 |
}), |
| 425 |
}) |
| 426 |
} |
| 427 |
|
| 428 |
fn emails_to_jscontact(emails: &[super::Email]) -> Option<Emails> { |
| 429 |
emails.first().map(|email| Emails { |
| 430 |
email: Email { |
| 431 |
address: email.email().to_owned(), |
| 432 |
}, |
| 433 |
}) |
| 434 |
} |
| 435 |
|
| 436 |
fn addresses_to_jscontact(addresses: &[PostalAddress]) -> Option<Addresses> { |
| 437 |
if addresses.is_empty() { |
| 438 |
None |
| 439 |
} else { |
| 440 |
let addresses = Addresses { |
| 441 |
addr: addresses.first().map(postal_address_to_jscontact), |
| 442 |
address_one: addresses.get(1).map(postal_address_to_jscontact), |
| 443 |
}; |
| 444 |
Some(addresses) |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
fn postal_address_to_jscontact(addr: &PostalAddress) -> Address { |
| 449 |
let mut components = vec![]; |
| 450 |
for part in addr.street_parts() { |
| 451 |
components.push(KindValue { |
| 452 |
kind: "name".to_string(), |
| 453 |
value: part.to_owned(), |
| 454 |
}) |
| 455 |
} |
| 456 |
if let Some(locality) = addr.locality() { |
| 457 |
components.push(KindValue { |
| 458 |
kind: "locality".to_string(), |
| 459 |
value: locality.to_owned(), |
| 460 |
}) |
| 461 |
} |
| 462 |
if let Some(region) = addr.region_name() { |
| 463 |
components.push(KindValue { |
| 464 |
kind: "region".to_string(), |
| 465 |
value: region.to_owned(), |
| 466 |
}) |
| 467 |
} |
| 468 |
if let Some(country) = addr.country_name() { |
| 469 |
components.push(KindValue { |
| 470 |
kind: "country".to_string(), |
| 471 |
value: country.to_owned(), |
| 472 |
}) |
| 473 |
} |
| 474 |
if let Some(postal) = addr.postal_code() { |
| 475 |
components.push(KindValue { |
| 476 |
kind: "postalcode".to_string(), |
| 477 |
value: postal.to_owned(), |
| 478 |
}) |
| 479 |
} |
| 480 |
let name = Name { |
| 481 |
full: addr.full_address().map(|s| s.to_owned()), |
| 482 |
components: Some(components), |
| 483 |
}; |
| 484 |
Address { |
| 485 |
country_code: addr.country_code().map(|s| s.to_owned()), |
| 486 |
name: Some(name), |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
fn phones_to_jscontact(voice: Option<&super::Phone>, fax: Option<&super::Phone>) -> Option<Phones> { |
| 491 |
if voice.is_some() || fax.is_some() { |
| 492 |
Some(Phones { |
| 493 |
voice: voice.map(phone_to_jscontact), |
| 494 |
fax: fax.map(phone_to_jscontact), |
| 495 |
}) |
| 496 |
} else { |
| 497 |
None |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
fn phone_to_jscontact(phone: &super::Phone) -> Phone { |
| 502 |
let voice = phone.features().contains(&"voice".to_string()); |
| 503 |
let fax = phone.features().contains(&"fax".to_string()); |
| 504 |
let features = if voice || fax { |
| 505 |
Some(Features { |
| 506 |
voice: voice.then_some(true), |
| 507 |
fax: fax.then_some(true), |
| 508 |
}) |
| 509 |
} else { |
| 510 |
None |
| 511 |
}; |
| 512 |
Phone { |
| 513 |
features, |
| 514 |
number: phone.phone().to_owned(), |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
fn links_to_jscontact(contact: &Contact) -> Option<Links> { |
| 519 |
if contact.urls().is_empty() && contact.contact_uris().is_empty() { |
| 520 |
return None; |
| 521 |
} |
| 522 |
//else |
| 523 |
Some(Links { |
| 524 |
url: contact.url().map(|u| Url { uri: u.to_owned() }), |
| 525 |
contact_uri: contact.contact_uri().map(|u| ContactUri { |
| 526 |
kind: "contact".to_string(), |
| 527 |
uri: u.to_owned(), |
| 528 |
}), |
| 529 |
}) |
| 530 |
} |
| 531 |
|
| 532 |
#[cfg(test)] |
| 533 |
mod test { |
| 534 |
|
| 535 |
use std::collections::HashMap; |
| 536 |
|
| 537 |
use indoc::indoc; |
| 538 |
|
| 539 |
use crate::contact::jscontact::{ |
| 540 |
Address, Addresses, ContactUri, Email, Emails, Features, JsContactCard, KindValue, Links, |
| 541 |
Localization, Name, Org, Organizations, Phone, Phones, Url, |
| 542 |
}; |
| 543 |
use crate::contact::{ |
| 544 |
Contact, Email as ContactEmail, NameParts, Phone as ContactPhone, PostalAddress, |
| 545 |
}; |
| 546 |
|
| 547 |
use super::{ |
| 548 |
addresses_to_jscontact, emails_to_jscontact, links_to_jscontact, name_to_jscontact, |
| 549 |
org_to_jscontact, phones_to_jscontact, |
| 550 |
}; |
| 551 |
|
| 552 |
const DRAFT_EXAMPLE1: &str = indoc! {r#" |
| 553 |
{ |
| 554 |
"@type": "Card", |
| 555 |
"version": "2.0", |
| 556 |
"name": { |
| 557 |
"full": "Joe User", |
| 558 |
"components": [ |
| 559 |
{ "kind": "surname", "value": "User" }, |
| 560 |
{ "kind": "given", "value": "Joe" } |
| 561 |
] |
| 562 |
}, |
| 563 |
"organizations": { |
| 564 |
"org": { |
| 565 |
"name": "Org Example" |
| 566 |
} |
| 567 |
}, |
| 568 |
"addresses": { |
| 569 |
"addr": { |
| 570 |
"components": [ |
| 571 |
{ "kind": "name", "value": "Main Street 1" }, |
| 572 |
{ "kind": "locality", "value": "Ludwigshafen am Rhein" }, |
| 573 |
{ "kind": "region", "value": "Rhineland-Palatinate" }, |
| 574 |
{ "kind": "postcode", "value": "67067" }, |
| 575 |
{ "kind": "country", "value": "Germany" } |
| 576 |
], |
| 577 |
"countryCode": "DE" |
| 578 |
}, |
| 579 |
"addresses-1": { |
| 580 |
"full": "Somewhere Street 1 Mutterstadt 67112 Germany" |
| 581 |
} |
| 582 |
}, |
| 583 |
"phones": { |
| 584 |
"voice": { |
| 585 |
"features": { "voice": true }, |
| 586 |
"number": "tel:+49-1522-3433333" |
| 587 |
}, |
| 588 |
"fax": { |
| 589 |
"features": { "fax": true }, |
| 590 |
"number": "tel:+49-30-901820" |
| 591 |
} |
| 592 |
}, |
| 593 |
"emails": { |
| 594 |
"email": { |
| 595 |
"address": "[email protected]" |
| 596 |
} |
| 597 |
}, |
| 598 |
"links": { |
| 599 |
"url": { |
| 600 |
"uri": "https://www.example.com" |
| 601 |
}, |
| 602 |
"contact-uri": { |
| 603 |
"kind": "contact", |
| 604 |
"uri": "mailto:[email protected]" |
| 605 |
} |
| 606 |
} |
| 607 |
}"#}; |
| 608 |
|
| 609 |
const DRAFT_EXAMPLE2: &str = indoc! {r#" |
| 610 |
{ |
| 611 |
"@type": "Card", |
| 612 |
"version": "2.0", |
| 613 |
"language": "en", |
| 614 |
"name": { |
| 615 |
"full": "Vasya Pupkin" |
| 616 |
}, |
| 617 |
"organizations": { |
| 618 |
"org": { |
| 619 |
"name": "My Company" |
| 620 |
} |
| 621 |
}, |
| 622 |
"addresses": { |
| 623 |
"addr": { |
| 624 |
"components": [ |
| 625 |
{ "kind": "name", "value": "1 Street" }, |
| 626 |
{ "kind": "postOfficeBox", "value": "01001" }, |
| 627 |
{ "kind": "locality", "value": "Kyiv" } |
| 628 |
], |
| 629 |
"countryCode": "UA" |
| 630 |
} |
| 631 |
}, |
| 632 |
"localizations": { |
| 633 |
"ua": { |
| 634 |
"addresses": { |
| 635 |
"addr": { |
| 636 |
"components": [ |
| 637 |
{ "kind": "name", "value": "1, Улица" }, |
| 638 |
{ "kind": "postOfficeBox", "value": "01001" }, |
| 639 |
{ "kind": "locality", "value": "Киев" } |
| 640 |
], |
| 641 |
"countryCode": "UA" |
| 642 |
} |
| 643 |
}, |
| 644 |
"name": { |
| 645 |
"full": "Вася Пупкин" |
| 646 |
}, |
| 647 |
"organizations": { |
| 648 |
"org": { |
| 649 |
"name": "Моя Компания" |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
"#}; |
| 656 |
|
| 657 |
fn test_jscontact_card() -> JsContactCard { |
| 658 |
let mut localizations = HashMap::new(); |
| 659 |
localizations.insert( |
| 660 |
"UA".to_string(), |
| 661 |
Localization { |
| 662 |
organizations: Some(Organizations { |
| 663 |
org: Some(Org { |
| 664 |
name: Some("Acme Ltd in UA".to_string()), |
| 665 |
}), |
| 666 |
}), |
| 667 |
name: Some(Name { |
| 668 |
full: Some("Bob Smurd in UA".to_string()), |
| 669 |
components: Some(vec![ |
| 670 |
KindValue { |
| 671 |
kind: "surname".to_string(), |
| 672 |
value: "UA Smurd".to_string(), |
| 673 |
}, |
| 674 |
KindValue { |
| 675 |
kind: "given".to_string(), |
| 676 |
value: "UA Bob".to_string(), |
| 677 |
}, |
| 678 |
]), |
| 679 |
}), |
| 680 |
addresses: Some(Addresses { |
| 681 |
addr: Some(Address { |
| 682 |
name: Some(Name { |
| 683 |
full: Some("123 Glendale Booolvard".to_string()), |
| 684 |
components: Some(vec![ |
| 685 |
KindValue { |
| 686 |
kind: "locality".to_string(), |
| 687 |
value: "Kyiv".to_string(), |
| 688 |
}, |
| 689 |
KindValue { |
| 690 |
kind: "region".to_string(), |
| 691 |
value: "Kyivland".to_string(), |
| 692 |
}, |
| 693 |
]), |
| 694 |
}), |
| 695 |
country_code: Some("UA".to_string()), |
| 696 |
}), |
| 697 |
address_one: Some(Address { |
| 698 |
name: Some(Name { |
| 699 |
full: Some("333 Bing Strasse".to_string()), |
| 700 |
components: Some(vec![ |
| 701 |
KindValue { |
| 702 |
kind: "locality".to_string(), |
| 703 |
value: "Some Town".to_string(), |
| 704 |
}, |
| 705 |
KindValue { |
| 706 |
kind: "region".to_string(), |
| 707 |
value: "Some Region".to_string(), |
| 708 |
}, |
| 709 |
]), |
| 710 |
}), |
| 711 |
country_code: Some("UA".to_string()), |
| 712 |
}), |
| 713 |
}), |
| 714 |
}, |
| 715 |
); |
| 716 |
JsContactCard { |
| 717 |
card_type: "Card".to_string(), |
| 718 |
version: "2.0".to_string(), |
| 719 |
kind: Some("individual".to_string()), |
| 720 |
language: Some("en".to_string()), |
| 721 |
organizations: Some(Organizations { |
| 722 |
org: Some(Org { |
| 723 |
name: Some("Acme Ltd".to_string()), |
| 724 |
}), |
| 725 |
}), |
| 726 |
name: Some(Name { |
| 727 |
full: Some("Bob Smurd".to_string()), |
| 728 |
components: Some(vec![ |
| 729 |
KindValue { |
| 730 |
kind: "surname".to_string(), |
| 731 |
value: "Smurd".to_string(), |
| 732 |
}, |
| 733 |
KindValue { |
| 734 |
kind: "given".to_string(), |
| 735 |
value: "Bob".to_string(), |
| 736 |
}, |
| 737 |
]), |
| 738 |
}), |
| 739 |
addresses: Some(Addresses { |
| 740 |
addr: Some(Address { |
| 741 |
name: Some(Name { |
| 742 |
full: Some("123 Glendale Blvd".to_string()), |
| 743 |
components: Some(vec![ |
| 744 |
KindValue { |
| 745 |
kind: "locality".to_string(), |
| 746 |
value: "Glenburnie".to_string(), |
| 747 |
}, |
| 748 |
KindValue { |
| 749 |
kind: "region".to_string(), |
| 750 |
value: "Maryland".to_string(), |
| 751 |
}, |
| 752 |
]), |
| 753 |
}), |
| 754 |
country_code: Some("US".to_string()), |
| 755 |
}), |
| 756 |
address_one: Some(Address { |
| 757 |
name: Some(Name { |
| 758 |
full: Some("123 Glendale Blvd".to_string()), |
| 759 |
components: Some(vec![ |
| 760 |
KindValue { |
| 761 |
kind: "locality".to_string(), |
| 762 |
value: "Glenburnie".to_string(), |
| 763 |
}, |
| 764 |
KindValue { |
| 765 |
kind: "region".to_string(), |
| 766 |
value: "Maryland".to_string(), |
| 767 |
}, |
| 768 |
]), |
| 769 |
}), |
| 770 |
country_code: Some("US".to_string()), |
| 771 |
}), |
| 772 |
}), |
| 773 |
phones: Some(Phones { |
| 774 |
voice: Some(Phone { |
| 775 |
features: Some(Features { |
| 776 |
voice: Some(true), |
| 777 |
fax: None, |
| 778 |
}), |
| 779 |
number: "555-1212".to_string(), |
| 780 |
}), |
| 781 |
fax: Some(Phone { |
| 782 |
features: Some(Features { |
| 783 |
voice: None, |
| 784 |
fax: Some(true), |
| 785 |
}), |
| 786 |
number: "555-2121".to_string(), |
| 787 |
}), |
| 788 |
}), |
| 789 |
emails: Some(Emails { |
| 790 |
email: Email { |
| 791 |
address: "mailto:[email protected]".to_string(), |
| 792 |
}, |
| 793 |
}), |
| 794 |
links: Some(Links { |
| 795 |
url: Some(Url { |
| 796 |
uri: "https://example.com".to_string(), |
| 797 |
}), |
| 798 |
contact_uri: Some(ContactUri { |
| 799 |
kind: "contact-uri".to_owned(), |
| 800 |
uri: "https://example.org".to_owned(), |
| 801 |
}), |
| 802 |
}), |
| 803 |
localizations: Some(localizations), |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
#[test] |
| 808 |
fn test_deserialize_draft1() { |
| 809 |
// GIVEN |
| 810 |
let expected = DRAFT_EXAMPLE1; |
| 811 |
|
| 812 |
// WHEN |
| 813 |
let actual = serde_json::from_str::<JsContactCard>(expected); |
| 814 |
|
| 815 |
// THEN |
| 816 |
actual.unwrap(); |
| 817 |
} |
| 818 |
|
| 819 |
#[test] |
| 820 |
fn test_deserialize_draft2() { |
| 821 |
// GIVEN |
| 822 |
let expected = DRAFT_EXAMPLE2; |
| 823 |
|
| 824 |
// WHEN |
| 825 |
let actual = serde_json::from_str::<JsContactCard>(expected); |
| 826 |
|
| 827 |
// THEN |
| 828 |
actual.unwrap(); |
| 829 |
} |
| 830 |
|
| 831 |
#[test] |
| 832 |
fn test_serialize() { |
| 833 |
// GIVEN |
| 834 |
let expected = test_jscontact_card(); |
| 835 |
|
| 836 |
// WHEN |
| 837 |
let actual = serde_json::to_string_pretty(&expected); |
| 838 |
|
| 839 |
// THEN |
| 840 |
eprintln!("\n{}\n", actual.unwrap()); |
| 841 |
} |
| 842 |
|
| 843 |
#[test] |
| 844 |
fn test_roundtrip_example1() { |
| 845 |
// GIVEN |
| 846 |
let expected = serde_json::from_str::<JsContactCard>(DRAFT_EXAMPLE1).expect("valid json"); |
| 847 |
dbg!(&expected); |
| 848 |
|
| 849 |
// WHEN |
| 850 |
let de_ser = serde_json::from_str::<JsContactCard>(DRAFT_EXAMPLE1).expect("valid json"); |
| 851 |
let ser = serde_json::to_string_pretty(&de_ser).expect("serialize json"); |
| 852 |
let actual = serde_json::from_str::<JsContactCard>(&ser).expect("valid json"); |
| 853 |
dbg!(&actual); |
| 854 |
|
| 855 |
// THEN |
| 856 |
assert_eq!(expected, actual); |
| 857 |
} |
| 858 |
|
| 859 |
#[test] |
| 860 |
fn test_roundtrip_example2() { |
| 861 |
// GIVEN |
| 862 |
let expected = serde_json::from_str::<JsContactCard>(DRAFT_EXAMPLE2).expect("valid json"); |
| 863 |
dbg!(&expected); |
| 864 |
|
| 865 |
// WHEN |
| 866 |
let de_ser = serde_json::from_str::<JsContactCard>(DRAFT_EXAMPLE2).expect("valid json"); |
| 867 |
let ser = serde_json::to_string_pretty(&de_ser).expect("serialize json"); |
| 868 |
let actual = serde_json::from_str::<JsContactCard>(&ser).expect("valid json"); |
| 869 |
dbg!(&actual); |
| 870 |
|
| 871 |
// THEN |
| 872 |
assert_eq!(expected, actual); |
| 873 |
} |
| 874 |
|
| 875 |
#[test] |
| 876 |
fn test_roundtrip_builder_example() { |
| 877 |
// GIVEN |
| 878 |
let expected = test_jscontact_card(); |
| 879 |
dbg!(&expected); |
| 880 |
|
| 881 |
// WHEN |
| 882 |
let ser = serde_json::to_string_pretty(&expected).expect("serialize json"); |
| 883 |
let actual = serde_json::from_str::<JsContactCard>(&ser).expect("valid json"); |
| 884 |
dbg!(&actual); |
| 885 |
|
| 886 |
// THEN |
| 887 |
assert_eq!(expected, actual); |
| 888 |
} |
| 889 |
|
| 890 |
#[test] |
| 891 |
fn test_org_to_jscontact_wth_some_org_name() { |
| 892 |
// GIVEN |
| 893 |
let org_name = Some("Acme Corp"); |
| 894 |
|
| 895 |
// WHEN |
| 896 |
let result = org_to_jscontact(org_name); |
| 897 |
|
| 898 |
// THEN |
| 899 |
assert!(result.is_some()); |
| 900 |
assert_eq!(result.unwrap().org.unwrap().name.unwrap(), "Acme Corp"); |
| 901 |
} |
| 902 |
|
| 903 |
#[test] |
| 904 |
fn test_org_to_jscontact_with_no_org_name() { |
| 905 |
// GIVEN |
| 906 |
let org_name: Option<&str> = None; |
| 907 |
|
| 908 |
// WHEN |
| 909 |
let result = org_to_jscontact(org_name); |
| 910 |
|
| 911 |
// THEN |
| 912 |
assert!(result.is_none()); |
| 913 |
} |
| 914 |
|
| 915 |
#[test] |
| 916 |
fn test_name_to_jscontact_with_full_name() { |
| 917 |
// GIVEN |
| 918 |
let full_name = Some("John Doe"); |
| 919 |
let name_parts: Option<&NameParts> = None; |
| 920 |
|
| 921 |
// WHEN |
| 922 |
let result = name_to_jscontact(full_name, name_parts); |
| 923 |
|
| 924 |
// THEN |
| 925 |
assert!(result.is_some()); |
| 926 |
assert_eq!(result.unwrap().full, Some("John Doe".to_string())); |
| 927 |
} |
| 928 |
|
| 929 |
#[test] |
| 930 |
fn test_name_to_jscontact_with_name_parts() { |
| 931 |
// GIVEN |
| 932 |
let full_name: Option<&str> = None; |
| 933 |
let name_parts = Some( |
| 934 |
NameParts::builder() |
| 935 |
.given_name("John") |
| 936 |
.surname("Doe") |
| 937 |
.build(), |
| 938 |
); |
| 939 |
|
| 940 |
// WHEN |
| 941 |
let result = name_to_jscontact(full_name, name_parts.as_ref()); |
| 942 |
|
| 943 |
// THEN |
| 944 |
assert!(result.is_some()); |
| 945 |
let name = result.unwrap(); |
| 946 |
assert!(name.full.is_none()); |
| 947 |
assert!(name.components.is_some()); |
| 948 |
} |
| 949 |
|
| 950 |
#[test] |
| 951 |
fn test_name_to_jscontact_with_nothing() { |
| 952 |
// GIVEN |
| 953 |
let full_name: Option<&str> = None; |
| 954 |
let name_parts: Option<&NameParts> = None; |
| 955 |
|
| 956 |
// WHEN |
| 957 |
let result = name_to_jscontact(full_name, name_parts); |
| 958 |
|
| 959 |
// THEN |
| 960 |
assert!(result.is_none()); |
| 961 |
} |
| 962 |
|
| 963 |
#[test] |
| 964 |
fn test_name_to_jscontact_with_empty_full_name() { |
| 965 |
// GIVEN |
| 966 |
let full_name = Some(""); |
| 967 |
let name_parts: Option<&NameParts> = None; |
| 968 |
|
| 969 |
// WHEN |
| 970 |
let result = name_to_jscontact(full_name, name_parts); |
| 971 |
|
| 972 |
// THEN |
| 973 |
assert!(result.is_none()); |
| 974 |
} |
| 975 |
|
| 976 |
#[test] |
| 977 |
fn test_name_to_jscontact_with_empty_full_name_and_name_parts() { |
| 978 |
// GIVEN |
| 979 |
let full_name = Some(""); |
| 980 |
let name_parts = Some( |
| 981 |
NameParts::builder() |
| 982 |
.given_name("John") |
| 983 |
.surname("Doe") |
| 984 |
.build(), |
| 985 |
); |
| 986 |
|
| 987 |
// WHEN |
| 988 |
let result = name_to_jscontact(full_name, name_parts.as_ref()); |
| 989 |
|
| 990 |
// THEN |
| 991 |
assert!(result.is_some()); |
| 992 |
let name = result.unwrap(); |
| 993 |
assert!(name.full.is_none()); |
| 994 |
assert!(name.components.is_some()); |
| 995 |
} |
| 996 |
|
| 997 |
#[test] |
| 998 |
fn test_emails_to_jscontact() { |
| 999 |
// GIVEN |
| 1000 |
let emails = vec![ContactEmail::builder().email("[email protected]").build()]; |
| 1001 |
|
| 1002 |
// WHEN |
| 1003 |
let result = emails_to_jscontact(&emails); |
| 1004 |
|
| 1005 |
// THEN |
| 1006 |
assert!(result.is_some()); |
| 1007 |
assert_eq!(result.unwrap().email.address, "[email protected]"); |
| 1008 |
} |
| 1009 |
|
| 1010 |
#[test] |
| 1011 |
fn test_emails_to_jscontact_with_no_emails() { |
| 1012 |
// GIVEN |
| 1013 |
let emails: Vec<ContactEmail> = vec![]; |
| 1014 |
|
| 1015 |
// WHEN |
| 1016 |
let result = emails_to_jscontact(&emails); |
| 1017 |
|
| 1018 |
// THEN |
| 1019 |
assert!(result.is_none()); |
| 1020 |
} |
| 1021 |
|
| 1022 |
#[test] |
| 1023 |
fn test_addresses_to_jscontact() { |
| 1024 |
// GIVEN |
| 1025 |
let addresses = vec![PostalAddress::builder().full_address("123 Main St").build()]; |
| 1026 |
|
| 1027 |
// WHEN |
| 1028 |
let result = addresses_to_jscontact(&addresses); |
| 1029 |
|
| 1030 |
// THEN |
| 1031 |
assert!(result.is_some()); |
| 1032 |
} |
| 1033 |
|
| 1034 |
#[test] |
| 1035 |
fn test_addresses_to_jscontact_with_empty_addresses() { |
| 1036 |
// GIVEN |
| 1037 |
let addresses: Vec<PostalAddress> = vec![]; |
| 1038 |
|
| 1039 |
// WHEN |
| 1040 |
let result = addresses_to_jscontact(&addresses); |
| 1041 |
|
| 1042 |
// THEN |
| 1043 |
assert!(result.is_none()); |
| 1044 |
} |
| 1045 |
|
| 1046 |
#[test] |
| 1047 |
fn test_phones_to_jscontact_with_voice() { |
| 1048 |
// GIVEN |
| 1049 |
let voice = Some( |
| 1050 |
ContactPhone::builder() |
| 1051 |
.phone("+1-555-1234") |
| 1052 |
.feature("voice".to_string()) |
| 1053 |
.build(), |
| 1054 |
); |
| 1055 |
let fax: Option<&ContactPhone> = None; |
| 1056 |
|
| 1057 |
// WHEN |
| 1058 |
let result = phones_to_jscontact(voice.as_ref(), fax); |
| 1059 |
|
| 1060 |
// THEN |
| 1061 |
assert!(result.is_some()); |
| 1062 |
} |
| 1063 |
|
| 1064 |
#[test] |
| 1065 |
fn test_phones_to_jscontact_with_fax() { |
| 1066 |
// GIVEN |
| 1067 |
let voice: Option<&ContactPhone> = None; |
| 1068 |
let fax = Some( |
| 1069 |
ContactPhone::builder() |
| 1070 |
.phone("+1-555-5678") |
| 1071 |
.feature("fax".to_string()) |
| 1072 |
.build(), |
| 1073 |
); |
| 1074 |
|
| 1075 |
// WHEN |
| 1076 |
let result = phones_to_jscontact(voice, fax.as_ref()); |
| 1077 |
|
| 1078 |
// THEN |
| 1079 |
assert!(result.is_some()); |
| 1080 |
} |
| 1081 |
|
| 1082 |
#[test] |
| 1083 |
fn test_phones_to_jscontact_with_no_phones() { |
| 1084 |
// GIVEN |
| 1085 |
let voice: Option<&ContactPhone> = None; |
| 1086 |
let fax: Option<&ContactPhone> = None; |
| 1087 |
|
| 1088 |
// WHEN |
| 1089 |
let result = phones_to_jscontact(voice, fax); |
| 1090 |
|
| 1091 |
// THEN |
| 1092 |
assert!(result.is_none()); |
| 1093 |
} |
| 1094 |
|
| 1095 |
#[test] |
| 1096 |
fn test_links_to_jscontact_with_url_and_contact() { |
| 1097 |
// GIVEN |
| 1098 |
let contact = Contact::builder() |
| 1099 |
.url("https://example.com") |
| 1100 |
.contact_uri("mailto:[email protected]") |
| 1101 |
.build(); |
| 1102 |
|
| 1103 |
// WHEN |
| 1104 |
let result = links_to_jscontact(&contact); |
| 1105 |
|
| 1106 |
// THEN |
| 1107 |
assert!(result.is_some()); |
| 1108 |
} |
| 1109 |
|
| 1110 |
#[test] |
| 1111 |
fn test_links_to_jscontact_with_only_url() { |
| 1112 |
// GIVEN |
| 1113 |
let contact = Contact::builder().url("https://example.com").build(); |
| 1114 |
|
| 1115 |
// WHEN |
| 1116 |
let result = links_to_jscontact(&contact); |
| 1117 |
|
| 1118 |
// THEN |
| 1119 |
assert!(result.is_some()); |
| 1120 |
} |
| 1121 |
|
| 1122 |
#[test] |
| 1123 |
fn test_links_to_jscontact_with_only_contact_uri() { |
| 1124 |
// GIVEN |
| 1125 |
let contact = Contact::builder() |
| 1126 |
.contact_uri("mailto:[email protected]") |
| 1127 |
.build(); |
| 1128 |
|
| 1129 |
// WHEN |
| 1130 |
let result = links_to_jscontact(&contact); |
| 1131 |
|
| 1132 |
// THEN |
| 1133 |
assert!(result.is_some()); |
| 1134 |
} |
| 1135 |
|
| 1136 |
#[test] |
| 1137 |
fn test_links_to_jscontact_with_nothing() { |
| 1138 |
// GIVEN |
| 1139 |
let contact = Contact::builder().build(); |
| 1140 |
|
| 1141 |
// WHEN |
| 1142 |
let result = links_to_jscontact(&contact); |
| 1143 |
|
| 1144 |
// THEN |
| 1145 |
assert!(result.is_none()); |
| 1146 |
} |
| 1147 |
} |