| 1 |
//! Convert jCard/vCard to Contact. |
| 2 |
use serde_json::Value; |
| 3 |
|
| 4 |
use super::{Contact, Email, Lang, NameParts, Phone, PostalAddress}; |
| 5 |
|
| 6 |
impl Contact { |
| 7 |
/// Creates a Contact from an array of [`Value`]s. |
| 8 |
/// |
| 9 |
/// ```rust |
| 10 |
/// use icann_rdap_common::contact::Contact; |
| 11 |
/// use serde::Deserialize; |
| 12 |
/// use serde_json::Value; |
| 13 |
/// |
| 14 |
/// let json = r#" |
| 15 |
/// [ |
| 16 |
/// "vcard", |
| 17 |
/// [ |
| 18 |
/// ["version", {}, "text", "4.0"], |
| 19 |
/// ["fn", {}, "text", "Joe User"], |
| 20 |
/// ["kind", {}, "text", "individual"], |
| 21 |
/// ["org", { |
| 22 |
/// "type":"work" |
| 23 |
/// }, "text", "Example"], |
| 24 |
/// ["title", {}, "text", "Research Scientist"], |
| 25 |
/// ["role", {}, "text", "Project Lead"], |
| 26 |
/// ["adr", |
| 27 |
/// { "type":"work" }, |
| 28 |
/// "text", |
| 29 |
/// [ |
| 30 |
/// "", |
| 31 |
/// "Suite 1234", |
| 32 |
/// "4321 Rue Somewhere", |
| 33 |
/// "Quebec", |
| 34 |
/// "QC", |
| 35 |
/// "G1V 2M2", |
| 36 |
/// "Canada" |
| 37 |
/// ] |
| 38 |
/// ], |
| 39 |
/// ["tel", |
| 40 |
/// { "type":["work", "voice"], "pref":"1" }, |
| 41 |
/// "uri", "tel:+1-555-555-1234;ext=102" |
| 42 |
/// ], |
| 43 |
/// ["email", |
| 44 |
/// { "type":"work" }, |
| 45 |
/// "text", "[email protected]" |
| 46 |
/// ] |
| 47 |
/// ] |
| 48 |
/// ]"#; |
| 49 |
/// |
| 50 |
/// let data: Vec<Value> = serde_json::from_str(json).unwrap(); |
| 51 |
/// let contact = Contact::from_vcard(&data); |
| 52 |
/// ``` |
| 53 |
pub fn from_vcard(vcard_array: &[Value]) -> Option<Contact> { |
| 54 |
// value should be "vcard" followed by array |
| 55 |
let value = vcard_array.first()?; |
| 56 |
let vcard_literal = value.as_str()?; |
| 57 |
if !vcard_literal.eq_ignore_ascii_case("vcard") { |
| 58 |
return None; |
| 59 |
}; |
| 60 |
let vcard = vcard_array.get(1)?; |
| 61 |
let vcard = vcard.as_array()?; |
| 62 |
|
| 63 |
let contact = Contact::builder() |
| 64 |
.and_full_name(vcard.find_property("fn").get_text()) |
| 65 |
.and_kind(vcard.find_property("kind").get_text()) |
| 66 |
.titles(vcard.find_properties("title").get_texts().unwrap_or(vec![])) |
| 67 |
.roles(vcard.find_properties("role").get_texts().unwrap_or(vec![])) |
| 68 |
.nick_names( |
| 69 |
vcard |
| 70 |
.find_properties("nickname") |
| 71 |
.get_texts() |
| 72 |
.unwrap_or(vec![]), |
| 73 |
) |
| 74 |
.organization_names(vcard.find_properties("org").get_texts().unwrap_or(vec![])) |
| 75 |
.langs(vcard.find_properties("lang").get_langs().unwrap_or(vec![])) |
| 76 |
.emails( |
| 77 |
vcard |
| 78 |
.find_properties("email") |
| 79 |
.get_emails() |
| 80 |
.unwrap_or(vec![]), |
| 81 |
) |
| 82 |
.phones(vcard.find_properties("tel").get_phones().unwrap_or(vec![])) |
| 83 |
.postal_addresses( |
| 84 |
vcard |
| 85 |
.find_properties("adr") |
| 86 |
.get_postal_addresses() |
| 87 |
.unwrap_or(vec![]), |
| 88 |
) |
| 89 |
.and_name_parts(vcard.find_property("n").get_name_parts()) |
| 90 |
.contact_uris( |
| 91 |
vcard |
| 92 |
.find_properties("contact-uri") |
| 93 |
.get_texts() |
| 94 |
.unwrap_or(vec![]), |
| 95 |
) |
| 96 |
.urls(vcard.find_properties("url").get_texts().unwrap_or(vec![])) |
| 97 |
.build(); |
| 98 |
|
| 99 |
contact.is_non_empty().then_some(contact) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
trait FindProperty<'a> { |
| 104 |
fn find_property(self, name: &'a str) -> Option<&'a Vec<Value>>; |
| 105 |
} |
| 106 |
|
| 107 |
impl<'a> FindProperty<'a> for &'a [Value] { |
| 108 |
fn find_property(self, name: &'a str) -> Option<&'a Vec<Value>> { |
| 109 |
self.iter() |
| 110 |
.filter_map(|prop_array| prop_array.as_array()) |
| 111 |
.find(|prop_array| { |
| 112 |
if let Some(prop_name) = prop_array.first() { |
| 113 |
if let Some(prop_name) = prop_name.as_str() { |
| 114 |
prop_name.eq_ignore_ascii_case(name) |
| 115 |
} else { |
| 116 |
false |
| 117 |
} |
| 118 |
} else { |
| 119 |
false |
| 120 |
} |
| 121 |
}) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
trait FindProperties<'a> { |
| 126 |
fn find_properties(self, name: &'a str) -> Vec<&'a Vec<Value>>; |
| 127 |
} |
| 128 |
|
| 129 |
impl<'a> FindProperties<'a> for &'a [Value] { |
| 130 |
fn find_properties(self, name: &'a str) -> Vec<&'a Vec<Value>> { |
| 131 |
self.iter() |
| 132 |
.filter_map(|prop_array| prop_array.as_array()) |
| 133 |
.filter(|prop_array| { |
| 134 |
if let Some(prop_name) = prop_array.first() { |
| 135 |
if let Some(prop_name) = prop_name.as_str() { |
| 136 |
prop_name.eq_ignore_ascii_case(name) |
| 137 |
} else { |
| 138 |
false |
| 139 |
} |
| 140 |
} else { |
| 141 |
false |
| 142 |
} |
| 143 |
}) |
| 144 |
.collect() |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
trait GetText<'a> { |
| 149 |
fn get_text(self) -> Option<String>; |
| 150 |
} |
| 151 |
|
| 152 |
impl<'a> GetText<'a> for Option<&'a Vec<Value>> { |
| 153 |
fn get_text(self) -> Option<String> { |
| 154 |
let values = self?; |
| 155 |
let fourth = values.get(3)?; |
| 156 |
fourth.as_str().map(|s| s.to_owned()) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl<'a> GetText<'a> for &'a Vec<Value> { |
| 161 |
fn get_text(self) -> Option<String> { |
| 162 |
let fourth = self.get(3)?; |
| 163 |
fourth.as_str().map(|s| s.to_owned()) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
trait GetTexts<'a> { |
| 168 |
fn get_texts(self) -> Option<Vec<String>>; |
| 169 |
} |
| 170 |
|
| 171 |
impl<'a> GetTexts<'a> for &'a [&'a Vec<Value>] { |
| 172 |
fn get_texts(self) -> Option<Vec<String>> { |
| 173 |
let texts = self |
| 174 |
.iter() |
| 175 |
.filter_map(|prop| (*prop).get_text()) |
| 176 |
.collect::<Vec<String>>(); |
| 177 |
(!texts.is_empty()).then_some(texts) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/// Get a "pref" parameter. |
| 182 |
trait GetPreference<'a> { |
| 183 |
fn get_preference(self) -> Option<u64>; |
| 184 |
} |
| 185 |
|
| 186 |
impl<'a> GetPreference<'a> for &'a Vec<Value> { |
| 187 |
fn get_preference(self) -> Option<u64> { |
| 188 |
let second = self.get(1)?; |
| 189 |
let second = second.as_object()?; |
| 190 |
let preference = second.get("pref")?; |
| 191 |
preference.as_str().and_then(|s| s.parse().ok()) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/// Get a "label" parameter. |
| 196 |
trait GetLabel<'a> { |
| 197 |
fn get_label(self) -> Option<String>; |
| 198 |
} |
| 199 |
|
| 200 |
impl<'a> GetLabel<'a> for &'a Vec<Value> { |
| 201 |
fn get_label(self) -> Option<String> { |
| 202 |
let second = self.get(1)?; |
| 203 |
let second = second.as_object()?; |
| 204 |
let label = second.get("label")?; |
| 205 |
label.as_str().map(|s| s.to_owned()) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/// Get a "cc" parameter. |
| 210 |
trait GetCountryCode<'a> { |
| 211 |
fn get_country_code(self) -> Option<String>; |
| 212 |
} |
| 213 |
|
| 214 |
impl<'a> GetCountryCode<'a> for &'a Vec<Value> { |
| 215 |
fn get_country_code(self) -> Option<String> { |
| 216 |
let second = self.get(1)?; |
| 217 |
let second = second.as_object()?; |
| 218 |
let cc = second.get("cc")?; |
| 219 |
cc.as_str().map(|s| s.to_owned()) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
const CONTEXTS: [&str; 6] = ["home", "work", "office", "private", "mobile", "cell"]; |
| 224 |
|
| 225 |
/// Get the vCard "type" parameter as a JSContact context. |
| 226 |
trait GetContexts<'a> { |
| 227 |
fn get_contexts(self) -> Option<Vec<String>>; |
| 228 |
} |
| 229 |
|
| 230 |
impl<'a> GetContexts<'a> for &'a Vec<Value> { |
| 231 |
fn get_contexts(self) -> Option<Vec<String>> { |
| 232 |
let second = self.get(1)?; |
| 233 |
let second = second.as_object()?; |
| 234 |
let contexts = second.get("type")?; |
| 235 |
if let Some(context) = contexts.as_str() { |
| 236 |
let context = context.to_lowercase(); |
| 237 |
if CONTEXTS.contains(&context.as_str()) { |
| 238 |
return Some(vec![context]); |
| 239 |
} else { |
| 240 |
return None; |
| 241 |
} |
| 242 |
}; |
| 243 |
let contexts = contexts.as_array()?; |
| 244 |
let contexts = contexts |
| 245 |
.iter() |
| 246 |
.filter_map(|v| v.as_str()) |
| 247 |
.map(|s| s.to_lowercase()) |
| 248 |
.filter(|s| CONTEXTS.contains(&s.as_str())) |
| 249 |
.collect::<Vec<String>>(); |
| 250 |
(!contexts.is_empty()).then_some(contexts) |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/// Get a vCard "type" parameter as a JSContact context. |
| 255 |
trait GetFeatures<'a> { |
| 256 |
fn get_features(self) -> Option<Vec<String>>; |
| 257 |
} |
| 258 |
|
| 259 |
impl<'a> GetFeatures<'a> for &'a Vec<Value> { |
| 260 |
fn get_features(self) -> Option<Vec<String>> { |
| 261 |
let second = self.get(1)?; |
| 262 |
let second = second.as_object()?; |
| 263 |
let features = second.get("type")?; |
| 264 |
if let Some(feature) = features.as_str() { |
| 265 |
let feature = feature.to_lowercase(); |
| 266 |
if !CONTEXTS.contains(&feature.as_str()) { |
| 267 |
return Some(vec![feature]); |
| 268 |
} else { |
| 269 |
return None; |
| 270 |
} |
| 271 |
}; |
| 272 |
let features = features.as_array()?; |
| 273 |
let features = features |
| 274 |
.iter() |
| 275 |
.filter_map(|v| v.as_str()) |
| 276 |
.map(|s| s.to_lowercase()) |
| 277 |
.filter(|s| !CONTEXTS.contains(&s.as_str())) |
| 278 |
.collect::<Vec<String>>(); |
| 279 |
(!features.is_empty()).then_some(features) |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
trait GetLangs<'a> { |
| 284 |
fn get_langs(self) -> Option<Vec<Lang>>; |
| 285 |
} |
| 286 |
|
| 287 |
impl<'a> GetLangs<'a> for &'a [&'a Vec<Value>] { |
| 288 |
fn get_langs(self) -> Option<Vec<Lang>> { |
| 289 |
let langs = self |
| 290 |
.iter() |
| 291 |
.filter_map(|prop| { |
| 292 |
let tag = (*prop).get_text()?; |
| 293 |
let lang = Lang::builder() |
| 294 |
.tag(tag) |
| 295 |
.and_preference((*prop).get_preference()) |
| 296 |
.build(); |
| 297 |
Some(lang) |
| 298 |
}) |
| 299 |
.collect::<Vec<Lang>>(); |
| 300 |
(!langs.is_empty()).then_some(langs) |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
trait GetEmails<'a> { |
| 305 |
fn get_emails(self) -> Option<Vec<Email>>; |
| 306 |
} |
| 307 |
|
| 308 |
impl<'a> GetEmails<'a> for &'a [&'a Vec<Value>] { |
| 309 |
fn get_emails(self) -> Option<Vec<Email>> { |
| 310 |
let emails = self |
| 311 |
.iter() |
| 312 |
.filter_map(|prop| { |
| 313 |
let addr = (*prop).get_text()?; |
| 314 |
let email = Email::builder() |
| 315 |
.email(addr) |
| 316 |
.contexts((*prop).get_contexts().unwrap_or_default()) |
| 317 |
.and_preference((*prop).get_preference()) |
| 318 |
.build(); |
| 319 |
Some(email) |
| 320 |
}) |
| 321 |
.collect::<Vec<Email>>(); |
| 322 |
(!emails.is_empty()).then_some(emails) |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
trait GetPhones<'a> { |
| 327 |
fn get_phones(self) -> Option<Vec<Phone>>; |
| 328 |
} |
| 329 |
|
| 330 |
impl<'a> GetPhones<'a> for &'a [&'a Vec<Value>] { |
| 331 |
fn get_phones(self) -> Option<Vec<Phone>> { |
| 332 |
let phones = self |
| 333 |
.iter() |
| 334 |
.filter_map(|prop| { |
| 335 |
let number = (*prop).get_text()?; |
| 336 |
let phone = Phone::builder() |
| 337 |
.phone(number) |
| 338 |
.features((*prop).get_features().unwrap_or_default()) |
| 339 |
.contexts((*prop).get_contexts().unwrap_or_default()) |
| 340 |
.and_preference((*prop).get_preference()) |
| 341 |
.build(); |
| 342 |
Some(phone) |
| 343 |
}) |
| 344 |
.collect::<Vec<Phone>>(); |
| 345 |
(!phones.is_empty()).then_some(phones) |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
trait GetPostalAddresses<'a> { |
| 350 |
fn get_postal_addresses(self) -> Option<Vec<PostalAddress>>; |
| 351 |
} |
| 352 |
|
| 353 |
impl<'a> GetPostalAddresses<'a> for &'a [&'a Vec<Value>] { |
| 354 |
fn get_postal_addresses(self) -> Option<Vec<PostalAddress>> { |
| 355 |
let addrs = self |
| 356 |
.iter() |
| 357 |
.map(|prop| { |
| 358 |
let mut postal_code: Option<String> = None; |
| 359 |
let mut country_code: Option<String> = None; |
| 360 |
let mut country_name: Option<String> = None; |
| 361 |
let mut region_code: Option<String> = None; |
| 362 |
let mut region_name: Option<String> = None; |
| 363 |
let mut locality: Option<String> = None; |
| 364 |
let mut street_parts: Vec<String> = vec![]; |
| 365 |
if let Some(fourth) = prop.get(3) |
| 366 |
&& let Some(addr) = fourth.as_array() |
| 367 |
{ |
| 368 |
// the jcard address fields are in a different index of the array. |
| 369 |
// |
| 370 |
// [ |
| 371 |
// "adr", |
| 372 |
// {}, |
| 373 |
// "text", |
| 374 |
// [ |
| 375 |
// "Mail Stop 3", // post office box (not recommended for use) |
| 376 |
// "Suite 3000", // apartment or suite (not recommended for use) |
| 377 |
// "123 Maple Ave", // street address |
| 378 |
// "Quebec", // locality or city name |
| 379 |
// "QC", // region (can be either a code or full name) |
| 380 |
// "G1V 2M2", // postal code |
| 381 |
// "Canada" // full country name |
| 382 |
// ] |
| 383 |
// ], |
| 384 |
if let Some(pobox) = addr.first() |
| 385 |
&& let Some(s) = pobox.as_str() |
| 386 |
&& !s.is_empty() |
| 387 |
{ |
| 388 |
street_parts.push(s.to_string()) |
| 389 |
} |
| 390 |
if let Some(appt) = addr.get(1) |
| 391 |
&& let Some(s) = appt.as_str() |
| 392 |
&& !s.is_empty() |
| 393 |
{ |
| 394 |
street_parts.push(s.to_string()) |
| 395 |
} |
| 396 |
if let Some(street) = addr.get(2) { |
| 397 |
if let Some(s) = street.as_str() { |
| 398 |
if !s.is_empty() { |
| 399 |
street_parts.push(s.to_string()) |
| 400 |
} |
| 401 |
} else if let Some(arry_s) = street.as_array() { |
| 402 |
arry_s |
| 403 |
.iter() |
| 404 |
.filter_map(|v| v.as_str()) |
| 405 |
.filter(|s| !s.is_empty()) |
| 406 |
.for_each(|s| street_parts.push(s.to_string())) |
| 407 |
} |
| 408 |
} |
| 409 |
if let Some(city) = addr.get(3) |
| 410 |
&& let Some(s) = city.as_str() |
| 411 |
&& !s.is_empty() |
| 412 |
{ |
| 413 |
locality = Some(s.to_string()); |
| 414 |
} |
| 415 |
if let Some(region) = addr.get(4) |
| 416 |
&& let Some(s) = region.as_str() |
| 417 |
&& !s.is_empty() |
| 418 |
{ |
| 419 |
if s.len() == 2 && s.to_uppercase() == s { |
| 420 |
region_code = Some(s.to_string()) |
| 421 |
} else { |
| 422 |
region_name = Some(s.to_string()) |
| 423 |
} |
| 424 |
} |
| 425 |
if let Some(pc) = addr.get(5) |
| 426 |
&& let Some(s) = pc.as_str() |
| 427 |
&& !s.is_empty() |
| 428 |
{ |
| 429 |
postal_code = Some(s.to_string()); |
| 430 |
} |
| 431 |
if let Some(country) = addr.get(6) |
| 432 |
&& let Some(s) = country.as_str() |
| 433 |
&& !s.is_empty() |
| 434 |
{ |
| 435 |
if s.len() == 2 && s.to_uppercase() == s { |
| 436 |
country_code = Some(s.to_string()) |
| 437 |
} else { |
| 438 |
country_name = Some(s.to_string()) |
| 439 |
} |
| 440 |
} |
| 441 |
}; |
| 442 |
let street_parts = (!street_parts.is_empty()).then_some(street_parts); |
| 443 |
PostalAddress::builder() |
| 444 |
.and_full_address((*prop).get_label()) |
| 445 |
.contexts((*prop).get_contexts().unwrap_or_default()) |
| 446 |
.and_preference((*prop).get_preference()) |
| 447 |
// prefer "cc" parameter but use country code in the country name field if no "cc" parameter |
| 448 |
.and_country_code((*prop).get_country_code().or(country_code)) |
| 449 |
.and_country_name(country_name) |
| 450 |
.and_postal_code(postal_code) |
| 451 |
.and_region_name(region_name) |
| 452 |
.and_region_code(region_code) |
| 453 |
.and_locality(locality) |
| 454 |
.street_parts(street_parts.unwrap_or_default()) |
| 455 |
.build() |
| 456 |
}) |
| 457 |
.collect::<Vec<PostalAddress>>(); |
| 458 |
(!addrs.is_empty()).then_some(addrs) |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
trait GetNameParts<'a> { |
| 463 |
fn get_name_parts(self) -> Option<NameParts>; |
| 464 |
} |
| 465 |
|
| 466 |
impl<'a> GetNameParts<'a> for Option<&'a Vec<Value>> { |
| 467 |
fn get_name_parts(self) -> Option<NameParts> { |
| 468 |
let values = self?; |
| 469 |
let fourth = values.get(3)?; |
| 470 |
let parts = fourth.as_array()?; |
| 471 |
let mut iter = parts.iter().filter(|p| p.is_string() || p.is_array()); |
| 472 |
let mut prefixes: Option<Vec<String>> = None; |
| 473 |
let mut surnames: Option<Vec<String>> = None; |
| 474 |
let mut given_names: Option<Vec<String>> = None; |
| 475 |
let mut middle_names: Option<Vec<String>> = None; |
| 476 |
let mut suffixes: Option<Vec<String>> = None; |
| 477 |
if let Some(e) = iter.next() { |
| 478 |
surnames = get_string_or_vec(e); |
| 479 |
}; |
| 480 |
if let Some(e) = iter.next() { |
| 481 |
given_names = get_string_or_vec(e); |
| 482 |
}; |
| 483 |
if let Some(e) = iter.next() { |
| 484 |
middle_names = get_string_or_vec(e); |
| 485 |
}; |
| 486 |
if let Some(e) = iter.next() { |
| 487 |
prefixes = get_string_or_vec(e); |
| 488 |
}; |
| 489 |
if let Some(e) = iter.next() { |
| 490 |
suffixes = get_string_or_vec(e); |
| 491 |
}; |
| 492 |
let name_parts = NameParts::builder() |
| 493 |
.surnames(surnames.unwrap_or_default()) |
| 494 |
.prefixes(prefixes.unwrap_or_default()) |
| 495 |
.given_names(given_names.unwrap_or_default()) |
| 496 |
.middle_names(middle_names.unwrap_or_default()) |
| 497 |
.suffixes(suffixes.unwrap_or_default()) |
| 498 |
.build(); |
| 499 |
if name_parts.surnames.is_none() |
| 500 |
&& name_parts.given_names.is_none() |
| 501 |
&& name_parts.middle_names.is_none() |
| 502 |
&& name_parts.suffixes.is_none() |
| 503 |
&& name_parts.prefixes.is_none() |
| 504 |
{ |
| 505 |
None |
| 506 |
} else { |
| 507 |
Some(name_parts) |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
fn get_string_or_vec(value: &Value) -> Option<Vec<String>> { |
| 513 |
if let Some(e) = value.as_str() { |
| 514 |
if e.is_empty() { |
| 515 |
return None; |
| 516 |
} else { |
| 517 |
return Some(vec![e.to_string()]); |
| 518 |
}; |
| 519 |
}; |
| 520 |
if let Some(e) = value.as_array() { |
| 521 |
let strings = e |
| 522 |
.iter() |
| 523 |
.filter_map(|e| e.as_str()) |
| 524 |
.filter(|s| !s.is_empty()) |
| 525 |
.map(|s| s.to_string()) |
| 526 |
.collect::<Vec<String>>(); |
| 527 |
return (!strings.is_empty()).then_some(strings); |
| 528 |
}; |
| 529 |
None |
| 530 |
} |
| 531 |
|
| 532 |
#[cfg(test)] |
| 533 |
mod tests { |
| 534 |
use serde_json::Value; |
| 535 |
|
| 536 |
use crate::contact::{Contact, NameParts}; |
| 537 |
|
| 538 |
#[test] |
| 539 |
fn test_vcard_properties() { |
| 540 |
// GIVEN |
| 541 |
let vcard = r#" |
| 542 |
[ |
| 543 |
"vcard", |
| 544 |
[ |
| 545 |
["version", {}, "text", "4.0"], |
| 546 |
["fn", {}, "text", "Joe User"], |
| 547 |
["n", {}, "text", |
| 548 |
["User", "Joe", "", "", ["ing. jr", "M.Sc."]] |
| 549 |
], |
| 550 |
["kind", {}, "text", "individual"], |
| 551 |
["lang", { |
| 552 |
"pref":"1" |
| 553 |
}, "language-tag", "fr"], |
| 554 |
["lang", { |
| 555 |
"pref":"2" |
| 556 |
}, "language-tag", "en"], |
| 557 |
["org", { |
| 558 |
"type":"work" |
| 559 |
}, "text", "Example"], |
| 560 |
["title", {}, "text", "Research Scientist"], |
| 561 |
["role", {}, "text", "Project Lead"], |
| 562 |
["adr", |
| 563 |
{ "type":"work" }, |
| 564 |
"text", |
| 565 |
[ |
| 566 |
"", |
| 567 |
"Suite 1234", |
| 568 |
"4321 Rue Somewhere", |
| 569 |
"Quebec", |
| 570 |
"QC", |
| 571 |
"G1V 2M2", |
| 572 |
"Canada" |
| 573 |
] |
| 574 |
], |
| 575 |
["adr", |
| 576 |
{ |
| 577 |
"type":"home", |
| 578 |
"label":"123 Maple Ave\nSuite 90001\nVancouver\nBC\n1239\n" |
| 579 |
}, |
| 580 |
"text", |
| 581 |
[ |
| 582 |
"", "", "", "", "", "", "" |
| 583 |
] |
| 584 |
], |
| 585 |
["tel", |
| 586 |
{ |
| 587 |
"type":["work", "voice"], |
| 588 |
"pref":"1" |
| 589 |
}, |
| 590 |
"uri", |
| 591 |
"tel:+1-555-555-1234;ext=102" |
| 592 |
], |
| 593 |
["tel", |
| 594 |
{ "type":["work", "cell", "voice", "video", "text"] }, |
| 595 |
"uri", |
| 596 |
"tel:+1-555-555-4321" |
| 597 |
], |
| 598 |
["email", |
| 599 |
{ "type":"work" }, |
| 600 |
"text", |
| 601 |
"[email protected]" |
| 602 |
], |
| 603 |
["geo", { |
| 604 |
"type":"work" |
| 605 |
}, "uri", "geo:46.772673,-71.282945"], |
| 606 |
["key", |
| 607 |
{ "type":"work" }, |
| 608 |
"uri", |
| 609 |
"https://www.example.com/joe.user/joe.asc" |
| 610 |
], |
| 611 |
["tz", {}, |
| 612 |
"utc-offset", "-05:00"], |
| 613 |
["contact-uri", {}, |
| 614 |
"uri", |
| 615 |
"https://example.com/contact-form" |
| 616 |
], |
| 617 |
["url", {}, |
| 618 |
"uri", |
| 619 |
"https://example.com/some-url" |
| 620 |
] |
| 621 |
] |
| 622 |
] |
| 623 |
"#; |
| 624 |
|
| 625 |
// WHEN |
| 626 |
let actual = serde_json::from_str::<Vec<Value>>(vcard); |
| 627 |
|
| 628 |
// THEN |
| 629 |
let actual = actual.expect("parsing vcard"); |
| 630 |
let actual = Contact::from_vcard(&actual).expect("vcard not found"); |
| 631 |
|
| 632 |
// full name |
| 633 |
assert_eq!(actual.full_name().expect("full_name not found"), "Joe User"); |
| 634 |
|
| 635 |
// kind |
| 636 |
assert_eq!(actual.kind().expect("kind not found"), "individual"); |
| 637 |
|
| 638 |
// titles |
| 639 |
assert_eq!( |
| 640 |
actual.titles().first().expect("titles empty"), |
| 641 |
"Research Scientist" |
| 642 |
); |
| 643 |
|
| 644 |
// roles |
| 645 |
assert_eq!(actual.roles().first().expect("roles empty"), "Project Lead"); |
| 646 |
|
| 647 |
// organization names |
| 648 |
assert_eq!( |
| 649 |
actual |
| 650 |
.organization_names() |
| 651 |
.first() |
| 652 |
.expect("organization_names empty"), |
| 653 |
"Example" |
| 654 |
); |
| 655 |
|
| 656 |
// nick names |
| 657 |
assert!(actual.nick_names().is_empty()); |
| 658 |
|
| 659 |
// langs |
| 660 |
assert_eq!(actual.langs().len(), 2); |
| 661 |
assert_eq!(actual.langs().first().expect("first lang").tag, "fr"); |
| 662 |
assert_eq!( |
| 663 |
actual.langs().first().expect("first lang").preference, |
| 664 |
Some(1) |
| 665 |
); |
| 666 |
assert_eq!(actual.langs().get(1).expect("second lang").tag, "en"); |
| 667 |
assert_eq!( |
| 668 |
actual.langs().get(1).expect("second lang").preference, |
| 669 |
Some(2) |
| 670 |
); |
| 671 |
|
| 672 |
// emails |
| 673 |
let Some(email) = actual.email() else { |
| 674 |
panic!("no email found") |
| 675 |
}; |
| 676 |
assert_eq!(email.email, "[email protected]"); |
| 677 |
assert!( |
| 678 |
email |
| 679 |
.contexts |
| 680 |
.as_ref() |
| 681 |
.expect("contexts not found") |
| 682 |
.contains(&"work".to_string()) |
| 683 |
); |
| 684 |
|
| 685 |
// phones |
| 686 |
let Some(phone) = actual.phone() else { |
| 687 |
panic!("no first phone") |
| 688 |
}; |
| 689 |
assert_eq!(phone.phone, "tel:+1-555-555-1234;ext=102"); |
| 690 |
assert!( |
| 691 |
phone |
| 692 |
.contexts |
| 693 |
.as_ref() |
| 694 |
.expect("no contexts") |
| 695 |
.contains(&"work".to_string()) |
| 696 |
); |
| 697 |
assert!( |
| 698 |
phone |
| 699 |
.features |
| 700 |
.as_ref() |
| 701 |
.expect("no features") |
| 702 |
.contains(&"voice".to_string()) |
| 703 |
); |
| 704 |
let Some(phone) = actual.phones().last() else { |
| 705 |
panic!("no last phone") |
| 706 |
}; |
| 707 |
assert_eq!(phone.phone, "tel:+1-555-555-4321"); |
| 708 |
assert!( |
| 709 |
phone |
| 710 |
.contexts |
| 711 |
.as_ref() |
| 712 |
.expect("no contexts") |
| 713 |
.contains(&"cell".to_string()) |
| 714 |
); |
| 715 |
assert!( |
| 716 |
phone |
| 717 |
.features |
| 718 |
.as_ref() |
| 719 |
.expect("no features") |
| 720 |
.contains(&"video".to_string()) |
| 721 |
); |
| 722 |
|
| 723 |
// postal addresses |
| 724 |
let Some(addr) = actual.postal_address() else { |
| 725 |
panic!("first address not found") |
| 726 |
}; |
| 727 |
assert!( |
| 728 |
addr.contexts |
| 729 |
.as_ref() |
| 730 |
.expect("no contexts") |
| 731 |
.contains(&"work".to_string()) |
| 732 |
); |
| 733 |
let Some(street_parts) = &addr.street_parts else { |
| 734 |
panic!("no street parts") |
| 735 |
}; |
| 736 |
assert_eq!(street_parts.first().expect("street part 0"), "Suite 1234"); |
| 737 |
assert_eq!( |
| 738 |
street_parts.get(1).expect("street part 1"), |
| 739 |
"4321 Rue Somewhere" |
| 740 |
); |
| 741 |
assert_eq!(addr.country_name.as_ref().expect("country name"), "Canada"); |
| 742 |
assert!(addr.country_code.is_none()); |
| 743 |
assert_eq!(addr.region_code.as_ref().expect("region code"), "QC"); |
| 744 |
assert!(addr.region_name.is_none()); |
| 745 |
assert_eq!(addr.postal_code.as_ref().expect("postal code"), "G1V 2M2"); |
| 746 |
let Some(addr) = actual.postal_addresses().last() else { |
| 747 |
panic!("last address not found") |
| 748 |
}; |
| 749 |
assert!( |
| 750 |
addr.contexts |
| 751 |
.as_ref() |
| 752 |
.expect("no contexts") |
| 753 |
.contains(&"home".to_string()) |
| 754 |
); |
| 755 |
assert_eq!( |
| 756 |
addr.full_address.as_ref().expect("full address not found"), |
| 757 |
"123 Maple Ave\nSuite 90001\nVancouver\nBC\n1239\n" |
| 758 |
); |
| 759 |
|
| 760 |
// name parts |
| 761 |
let Some(name_parts) = actual.name_parts() else { |
| 762 |
panic!("no name parts") |
| 763 |
}; |
| 764 |
let expected = NameParts::builder() |
| 765 |
.surnames(vec!["User".to_string()]) |
| 766 |
.given_names(vec!["Joe".to_string()]) |
| 767 |
.suffixes(vec!["ing. jr".to_string(), "M.Sc.".to_string()]) |
| 768 |
.build(); |
| 769 |
assert_eq!(name_parts, &expected); |
| 770 |
|
| 771 |
// contact-uris |
| 772 |
assert_eq!( |
| 773 |
actual |
| 774 |
.contact_uris |
| 775 |
.expect("no contact-uris") |
| 776 |
.first() |
| 777 |
.expect("contact-uris empty"), |
| 778 |
"https://example.com/contact-form" |
| 779 |
); |
| 780 |
|
| 781 |
// urls |
| 782 |
assert_eq!( |
| 783 |
actual |
| 784 |
.urls |
| 785 |
.expect("no urls") |
| 786 |
.first() |
| 787 |
.expect("urls are empty"), |
| 788 |
"https://example.com/some-url" |
| 789 |
); |
| 790 |
} |
| 791 |
|
| 792 |
#[test] |
| 793 |
fn test_addr_street_array() { |
| 794 |
// GIVEN |
| 795 |
let vcard = r#" |
| 796 |
[ |
| 797 |
"vcard", |
| 798 |
[ |
| 799 |
["version", {}, "text", "4.0"], |
| 800 |
["fn", {}, "text", "Joe User"], |
| 801 |
["adr", |
| 802 |
{ "type":"work" }, |
| 803 |
"text", |
| 804 |
[ |
| 805 |
"", |
| 806 |
"Suite 1234", |
| 807 |
["4321 Rue Blue", "1, Gawwn"], |
| 808 |
"Quebec", |
| 809 |
"QC", |
| 810 |
"G1V 2M2", |
| 811 |
"Canada" |
| 812 |
] |
| 813 |
] |
| 814 |
] |
| 815 |
] |
| 816 |
"#; |
| 817 |
|
| 818 |
// WHEN |
| 819 |
let actual = serde_json::from_str::<Vec<Value>>(vcard); |
| 820 |
|
| 821 |
// THEN |
| 822 |
let actual = actual.expect("parsing vcard"); |
| 823 |
let actual = Contact::from_vcard(&actual).expect("vcard not found"); |
| 824 |
|
| 825 |
// full name |
| 826 |
assert_eq!(actual.full_name().expect("full_name not found"), "Joe User"); |
| 827 |
|
| 828 |
// postal addresses |
| 829 |
let Some(addr) = actual.postal_address() else { |
| 830 |
panic!("first address not found") |
| 831 |
}; |
| 832 |
assert!( |
| 833 |
addr.contexts |
| 834 |
.as_ref() |
| 835 |
.expect("no contexts") |
| 836 |
.contains(&"work".to_string()) |
| 837 |
); |
| 838 |
let Some(street_parts) = &addr.street_parts else { |
| 839 |
panic!("no street parts") |
| 840 |
}; |
| 841 |
assert_eq!(street_parts.first().expect("street part 0"), "Suite 1234"); |
| 842 |
assert_eq!(street_parts.get(1).expect("street part 1"), "4321 Rue Blue"); |
| 843 |
assert_eq!(street_parts.get(2).expect("street part 2"), "1, Gawwn"); |
| 844 |
assert_eq!(addr.country_name.as_ref().expect("country name"), "Canada"); |
| 845 |
assert!(addr.country_code.is_none()); |
| 846 |
assert_eq!(addr.region_code.as_ref().expect("region code"), "QC"); |
| 847 |
assert!(addr.region_name.is_none()); |
| 848 |
assert_eq!(addr.postal_code.as_ref().expect("postal code"), "G1V 2M2"); |
| 849 |
} |
| 850 |
|
| 851 |
#[test] |
| 852 |
fn test_addr_cc_in_county_name() { |
| 853 |
// GIVEN |
| 854 |
let vcard = r#" |
| 855 |
[ |
| 856 |
"vcard", |
| 857 |
[ |
| 858 |
["version", {}, "text", "4.0"], |
| 859 |
["fn", {}, "text", "Joe User"], |
| 860 |
["adr", |
| 861 |
{ "type":"work" }, |
| 862 |
"text", |
| 863 |
[ |
| 864 |
"", |
| 865 |
"Suite 1234", |
| 866 |
["4321 Rue Blue", "1, Gawwn"], |
| 867 |
"Quebec", |
| 868 |
"QC", |
| 869 |
"G1V 2M2", |
| 870 |
"CA" |
| 871 |
] |
| 872 |
] |
| 873 |
] |
| 874 |
] |
| 875 |
"#; |
| 876 |
|
| 877 |
// WHEN |
| 878 |
let actual = serde_json::from_str::<Vec<Value>>(vcard); |
| 879 |
|
| 880 |
// THEN vcard parses |
| 881 |
let actual = actual.expect("parsing vcard"); |
| 882 |
let actual = Contact::from_vcard(&actual).expect("vcard not found"); |
| 883 |
|
| 884 |
// THEN there is a postal addresses |
| 885 |
let Some(addr) = actual.postal_address() else { |
| 886 |
panic!("first address not found") |
| 887 |
}; |
| 888 |
|
| 889 |
// THEN there is no country name |
| 890 |
assert!(addr.country_name.is_none()); |
| 891 |
|
| 892 |
// THEN there is a country code |
| 893 |
assert_eq!( |
| 894 |
addr.country_code.as_ref().expect("no country code found"), |
| 895 |
"CA" |
| 896 |
); |
| 897 |
} |
| 898 |
|
| 899 |
#[test] |
| 900 |
fn test_addr_cc_param() { |
| 901 |
// GIVEN |
| 902 |
let vcard = r#" |
| 903 |
[ |
| 904 |
"vcard", |
| 905 |
[ |
| 906 |
["version", {}, "text", "4.0"], |
| 907 |
["fn", {}, "text", "Joe User"], |
| 908 |
["adr", |
| 909 |
{ "cc":"CA" }, |
| 910 |
"text", |
| 911 |
[ |
| 912 |
"", |
| 913 |
"Suite 1234", |
| 914 |
["4321 Rue Blue", "1, Gawwn"], |
| 915 |
"Quebec", |
| 916 |
"QC", |
| 917 |
"G1V 2M2", |
| 918 |
"Canada" |
| 919 |
] |
| 920 |
] |
| 921 |
] |
| 922 |
] |
| 923 |
"#; |
| 924 |
|
| 925 |
// WHEN |
| 926 |
let actual = serde_json::from_str::<Vec<Value>>(vcard); |
| 927 |
|
| 928 |
// THEN vcard parses |
| 929 |
let actual = actual.expect("parsing vcard"); |
| 930 |
let actual = Contact::from_vcard(&actual).expect("vcard not found"); |
| 931 |
|
| 932 |
// THEN there is a postal addresses |
| 933 |
let Some(addr) = actual.postal_address() else { |
| 934 |
panic!("first address not found") |
| 935 |
}; |
| 936 |
|
| 937 |
// THEN there is a country name |
| 938 |
assert_eq!( |
| 939 |
addr.country_name.as_ref().expect("no country name"), |
| 940 |
"Canada" |
| 941 |
); |
| 942 |
|
| 943 |
// THEN there is a country code |
| 944 |
assert_eq!( |
| 945 |
addr.country_code.as_ref().expect("no country code found"), |
| 946 |
"CA" |
| 947 |
); |
| 948 |
} |
| 949 |
|
| 950 |
#[test] |
| 951 |
fn test_addr_cc_param_with_type_param() { |
| 952 |
// GIVEN |
| 953 |
let vcard = r#" |
| 954 |
[ |
| 955 |
"vcard", |
| 956 |
[ |
| 957 |
["version", {}, "text", "4.0"], |
| 958 |
["fn", {}, "text", "Joe User"], |
| 959 |
["adr", |
| 960 |
{ "type":"work", "cc": "CA" }, |
| 961 |
"text", |
| 962 |
[ |
| 963 |
"", |
| 964 |
"Suite 1234", |
| 965 |
["4321 Rue Blue", "1, Gawwn"], |
| 966 |
"Quebec", |
| 967 |
"QC", |
| 968 |
"G1V 2M2", |
| 969 |
"Canada" |
| 970 |
] |
| 971 |
] |
| 972 |
] |
| 973 |
] |
| 974 |
"#; |
| 975 |
|
| 976 |
// WHEN |
| 977 |
let actual = serde_json::from_str::<Vec<Value>>(vcard); |
| 978 |
|
| 979 |
// THEN vcard parses |
| 980 |
let actual = actual.expect("parsing vcard"); |
| 981 |
let actual = Contact::from_vcard(&actual).expect("vcard not found"); |
| 982 |
|
| 983 |
// THEN there is a postal addresses |
| 984 |
let Some(addr) = actual.postal_address() else { |
| 985 |
panic!("first address not found") |
| 986 |
}; |
| 987 |
|
| 988 |
// THEN context is work |
| 989 |
assert!( |
| 990 |
addr.contexts |
| 991 |
.as_ref() |
| 992 |
.expect("no contexts") |
| 993 |
.contains(&"work".to_string()) |
| 994 |
); |
| 995 |
|
| 996 |
// THEN there is a country name |
| 997 |
assert_eq!( |
| 998 |
addr.country_name.as_ref().expect("no country name"), |
| 999 |
"Canada" |
| 1000 |
); |
| 1001 |
|
| 1002 |
// THEN there is a country code |
| 1003 |
assert_eq!( |
| 1004 |
addr.country_code.as_ref().expect("no country code found"), |
| 1005 |
"CA" |
| 1006 |
); |
| 1007 |
} |
| 1008 |
} |