| 1 |
//! Easy representation of contact information found in an Entity. |
| 2 |
//! |
| 3 |
//! This module converts contact information to and from vCard/jCard, which is hard to |
| 4 |
//! work with directly. It is also intended as a way of bridging the between vCard/jCard |
| 5 |
//! and any new contact model. |
| 6 |
//! |
| 7 |
//! This struct can be built using the builder. |
| 8 |
//! |
| 9 |
//! ```rust |
| 10 |
//! use icann_rdap_common::contact::Contact; |
| 11 |
//! |
| 12 |
//! let contact = Contact::builder() |
| 13 |
//! .kind("individual") |
| 14 |
//! .full_name("Bob Smurd") |
| 15 |
//! .build(); |
| 16 |
//! ``` |
| 17 |
//! |
| 18 |
//! Once built, a Contact struct can be converted to an array of [serde_json::Value]'s, |
| 19 |
//! which can be used with serde to serialize to JSON. |
| 20 |
//! |
| 21 |
//! ```rust |
| 22 |
//! use icann_rdap_common::contact::Contact; |
| 23 |
//! use serde::Serialize; |
| 24 |
//! use serde_json::Value; |
| 25 |
//! |
| 26 |
//! let contact = Contact::builder() |
| 27 |
//! .kind("individual") |
| 28 |
//! .full_name("Bob Smurd") |
| 29 |
//! .build(); |
| 30 |
//! |
| 31 |
//! let v = contact.to_vcard(); |
| 32 |
//! let json = serde_json::to_string(&v); |
| 33 |
//! ``` |
| 34 |
//! |
| 35 |
//! To deserialize, use the `from_vcard` function. |
| 36 |
//! |
| 37 |
//! ```rust |
| 38 |
//! use icann_rdap_common::contact::Contact; |
| 39 |
//! use serde::Deserialize; |
| 40 |
//! use serde_json::Value; |
| 41 |
//! |
| 42 |
//! let json = r#" |
| 43 |
//! [ |
| 44 |
//! "vcard", |
| 45 |
//! [ |
| 46 |
//! ["version", {}, "text", "4.0"], |
| 47 |
//! ["fn", {}, "text", "Joe User"], |
| 48 |
//! ["kind", {}, "text", "individual"], |
| 49 |
//! ["org", { |
| 50 |
//! "type":"work" |
| 51 |
//! }, "text", "Example"], |
| 52 |
//! ["title", {}, "text", "Research Scientist"], |
| 53 |
//! ["role", {}, "text", "Project Lead"], |
| 54 |
//! ["adr", |
| 55 |
//! { "type":"work" }, |
| 56 |
//! "text", |
| 57 |
//! [ |
| 58 |
//! "", |
| 59 |
//! "Suite 1234", |
| 60 |
//! "4321 Rue Somewhere", |
| 61 |
//! "Quebec", |
| 62 |
//! "QC", |
| 63 |
//! "G1V 2M2", |
| 64 |
//! "Canada" |
| 65 |
//! ] |
| 66 |
//! ], |
| 67 |
//! ["tel", |
| 68 |
//! { "type":["work", "voice"], "pref":"1" }, |
| 69 |
//! "uri", "tel:+1-555-555-1234;ext=102" |
| 70 |
//! ], |
| 71 |
//! ["email", |
| 72 |
//! { "type":"work" }, |
| 73 |
//! "text", "[email protected]" |
| 74 |
//! ] |
| 75 |
//! ] |
| 76 |
//! ]"#; |
| 77 |
//! |
| 78 |
//! let data: Vec<Value> = serde_json::from_str(json).unwrap(); |
| 79 |
//! let contact = Contact::from_vcard(&data).unwrap(); |
| 80 |
//! |
| 81 |
//! // use the getter functions to access the data. |
| 82 |
//! let kind = contact.kind(); |
| 83 |
//! let email_addr = contact.emails().first().unwrap().email(); |
| 84 |
//! ``` |
| 85 |
|
| 86 |
mod from_vcard; |
| 87 |
pub(crate) mod jscontact; |
| 88 |
mod to_vcard; |
| 89 |
|
| 90 |
use std::{collections::BTreeMap, fmt::Display}; |
| 91 |
|
| 92 |
use buildstructor::Builder; |
| 93 |
|
| 94 |
use crate::prelude::to_opt_vec; |
| 95 |
|
| 96 |
/// Represents a contact. This more closely represents an EPP Contact with some |
| 97 |
/// things taken from JSContact. |
| 98 |
/// |
| 99 |
/// Using the builder to create the Contact: |
| 100 |
/// ```rust |
| 101 |
/// use icann_rdap_common::contact::Contact; |
| 102 |
/// |
| 103 |
/// let contact = Contact::builder() |
| 104 |
/// .kind("individual") |
| 105 |
/// .full_name("Bob Smurd") |
| 106 |
/// .build(); |
| 107 |
/// ``` |
| 108 |
/// |
| 109 |
/// |
| 110 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 111 |
pub struct Contact { |
| 112 |
/// Preferred languages. |
| 113 |
pub(crate) langs: Option<Vec<Lang>>, |
| 114 |
|
| 115 |
/// The kind such as individual, company, etc... |
| 116 |
pub(crate) kind: Option<String>, |
| 117 |
|
| 118 |
/// Email addresses. |
| 119 |
pub(crate) emails: Option<Vec<Email>>, |
| 120 |
|
| 121 |
/// Phone numbers. |
| 122 |
pub(crate) phones: Option<Vec<Phone>>, |
| 123 |
|
| 124 |
/// Contact URIs. |
| 125 |
pub(crate) contact_uris: Option<Vec<String>>, |
| 126 |
|
| 127 |
/// URLs |
| 128 |
pub(crate) urls: Option<Vec<String>>, |
| 129 |
|
| 130 |
/// The unlocalized parts. |
| 131 |
pub(crate) unlocalized: Localizable, |
| 132 |
|
| 133 |
/// Localizations |
| 134 |
pub(crate) localizations: BTreeMap<String, Localizable>, |
| 135 |
} |
| 136 |
|
| 137 |
#[buildstructor::buildstructor] |
| 138 |
impl Contact { |
| 139 |
#[builder(visibility = "pub")] |
| 140 |
fn new( |
| 141 |
langs: Vec<Lang>, |
| 142 |
kind: Option<String>, |
| 143 |
full_name: Option<String>, |
| 144 |
name_parts: Option<NameParts>, |
| 145 |
nick_names: Vec<String>, |
| 146 |
titles: Vec<String>, |
| 147 |
roles: Vec<String>, |
| 148 |
organization_names: Vec<String>, |
| 149 |
postal_addresses: Vec<PostalAddress>, |
| 150 |
emails: Vec<Email>, |
| 151 |
phones: Vec<Phone>, |
| 152 |
contact_uris: Vec<String>, |
| 153 |
urls: Vec<String>, |
| 154 |
) -> Self { |
| 155 |
Self { |
| 156 |
langs: to_opt_vec(langs), |
| 157 |
kind, |
| 158 |
unlocalized: Localizable { |
| 159 |
full_name, |
| 160 |
name_parts, |
| 161 |
nick_names: to_opt_vec(nick_names), |
| 162 |
titles: to_opt_vec(titles), |
| 163 |
roles: to_opt_vec(roles), |
| 164 |
organization_names: to_opt_vec(organization_names), |
| 165 |
postal_addresses: to_opt_vec(postal_addresses), |
| 166 |
}, |
| 167 |
emails: to_opt_vec(emails), |
| 168 |
phones: to_opt_vec(phones), |
| 169 |
contact_uris: to_opt_vec(contact_uris), |
| 170 |
urls: to_opt_vec(urls), |
| 171 |
localizations: BTreeMap::new(), |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/// Returns false if there is data in the Contact. |
| 176 |
pub fn is_non_empty(&self) -> bool { |
| 177 |
self.langs.is_some() |
| 178 |
|| self.kind.is_some() |
| 179 |
|| self.unlocalized.full_name.is_some() |
| 180 |
|| self.unlocalized.name_parts.is_some() |
| 181 |
|| self.unlocalized.nick_names.is_some() |
| 182 |
|| self.unlocalized.titles.is_some() |
| 183 |
|| self.unlocalized.roles.is_some() |
| 184 |
|| self.unlocalized.organization_names.is_some() |
| 185 |
|| self.unlocalized.postal_addresses.is_some() |
| 186 |
|| self.emails.is_some() |
| 187 |
|| self.phones.is_some() |
| 188 |
|| self.contact_uris.is_some() |
| 189 |
|| self.urls.is_some() |
| 190 |
} |
| 191 |
|
| 192 |
/// Set a localization. |
| 193 |
pub fn with_localization(mut self, lang: String, localization: Localizable) -> Self { |
| 194 |
self.localizations.insert(lang, localization); |
| 195 |
self |
| 196 |
} |
| 197 |
|
| 198 |
/// Set the set of emails. |
| 199 |
pub fn with_email_addresses(mut self, emails: &[impl ToString]) -> Self { |
| 200 |
let emails: Vec<Email> = emails |
| 201 |
.iter() |
| 202 |
.map(|e| Email::builder().email(e.to_string()).build()) |
| 203 |
.collect(); |
| 204 |
self.emails = (!emails.is_empty()).then_some(emails); |
| 205 |
self |
| 206 |
} |
| 207 |
|
| 208 |
/// Set the emails. |
| 209 |
pub fn with_emails(mut self, emails: Vec<Email>) -> Self { |
| 210 |
self.emails = Some(emails); |
| 211 |
self |
| 212 |
} |
| 213 |
|
| 214 |
/// Add a voice phone to the set of phones. |
| 215 |
pub fn with_voice_phone_numbers(mut self, phones: &[impl ToString]) -> Self { |
| 216 |
let mut phones: Vec<Phone> = phones |
| 217 |
.iter() |
| 218 |
.map(|p| { |
| 219 |
Phone::builder() |
| 220 |
.contexts(vec!["voice".to_string()]) |
| 221 |
.phone(p.to_string()) |
| 222 |
.build() |
| 223 |
}) |
| 224 |
.collect(); |
| 225 |
if let Some(mut self_phones) = self.phones.clone() { |
| 226 |
phones.append(&mut self_phones); |
| 227 |
} else { |
| 228 |
self.phones = (!phones.is_empty()).then_some(phones); |
| 229 |
} |
| 230 |
self |
| 231 |
} |
| 232 |
|
| 233 |
/// Add a facsimile phone to the set of phones. |
| 234 |
pub fn with_fax_phone_numbers(mut self, phones: &[impl ToString]) -> Self { |
| 235 |
let mut phones: Vec<Phone> = phones |
| 236 |
.iter() |
| 237 |
.map(|p| { |
| 238 |
Phone::builder() |
| 239 |
.contexts(vec!["fax".to_string()]) |
| 240 |
.phone(p.to_string()) |
| 241 |
.build() |
| 242 |
}) |
| 243 |
.collect(); |
| 244 |
if let Some(mut self_phones) = self.phones.clone() { |
| 245 |
phones.append(&mut self_phones); |
| 246 |
} else { |
| 247 |
self.phones = (!phones.is_empty()).then_some(phones); |
| 248 |
} |
| 249 |
self |
| 250 |
} |
| 251 |
|
| 252 |
/// Set the phones. |
| 253 |
pub fn with_phones(mut self, phones: Vec<Phone>) -> Self { |
| 254 |
self.phones = Some(phones); |
| 255 |
self |
| 256 |
} |
| 257 |
|
| 258 |
/// Set the set of postal addresses to only be the passed in postal address. |
| 259 |
pub fn with_postal_address(mut self, postal_address: PostalAddress) -> Self { |
| 260 |
self.unlocalized.postal_addresses = Some(vec![postal_address]); |
| 261 |
self |
| 262 |
} |
| 263 |
|
| 264 |
/// Set the complete set of postal addresses. |
| 265 |
pub fn with_postal_addresses(mut self, postal_addresses: Vec<PostalAddress>) -> Self { |
| 266 |
self.unlocalized.postal_addresses = Some(postal_addresses); |
| 267 |
self |
| 268 |
} |
| 269 |
|
| 270 |
/// Set the full name. |
| 271 |
pub fn with_full_name(mut self, full_name: String) -> Self { |
| 272 |
self.unlocalized.full_name = Some(full_name); |
| 273 |
self |
| 274 |
} |
| 275 |
|
| 276 |
/// Set the name parts. |
| 277 |
pub fn with_name_parts(mut self, name_parts: Option<NameParts>) -> Self { |
| 278 |
self.unlocalized.name_parts = name_parts; |
| 279 |
self |
| 280 |
} |
| 281 |
|
| 282 |
/// Set the nick names. |
| 283 |
pub fn with_nick_names(mut self, nick_names: Vec<String>) -> Self { |
| 284 |
self.unlocalized.nick_names = (!nick_names.is_empty()).then_some(nick_names); |
| 285 |
self |
| 286 |
} |
| 287 |
|
| 288 |
/// Set the titles. |
| 289 |
pub fn with_titles(mut self, titles: Vec<String>) -> Self { |
| 290 |
self.unlocalized.titles = (!titles.is_empty()).then_some(titles); |
| 291 |
self |
| 292 |
} |
| 293 |
|
| 294 |
/// Set the organizational roles. |
| 295 |
pub fn with_roles(mut self, roles: Vec<String>) -> Self { |
| 296 |
self.unlocalized.roles = (!roles.is_empty()).then_some(roles); |
| 297 |
self |
| 298 |
} |
| 299 |
|
| 300 |
/// Set the organization names. |
| 301 |
pub fn with_organization_names(mut self, organization_names: Vec<String>) -> Self { |
| 302 |
self.unlocalized.organization_names = Some(organization_names); |
| 303 |
self |
| 304 |
} |
| 305 |
|
| 306 |
/// Set the kind. |
| 307 |
pub fn with_kind(mut self, kind: String) -> Self { |
| 308 |
self.kind = Some(kind); |
| 309 |
self |
| 310 |
} |
| 311 |
|
| 312 |
/// Set the langs. |
| 313 |
pub fn with_langs(mut self, langs: Vec<Lang>) -> Self { |
| 314 |
self.langs = (!langs.is_empty()).then_some(langs); |
| 315 |
self |
| 316 |
} |
| 317 |
|
| 318 |
/// Get the langs. |
| 319 |
pub fn langs(&self) -> &[Lang] { |
| 320 |
self.langs.as_deref().unwrap_or_default() |
| 321 |
} |
| 322 |
|
| 323 |
/// Get the first language tag. |
| 324 |
pub fn lang(&self) -> Option<&Lang> { |
| 325 |
self.langs().first() |
| 326 |
} |
| 327 |
|
| 328 |
/// Get the kind. |
| 329 |
pub fn kind(&self) -> Option<&str> { |
| 330 |
self.kind.as_deref() |
| 331 |
} |
| 332 |
|
| 333 |
/// Get the full name. |
| 334 |
pub fn full_name(&self) -> Option<&str> { |
| 335 |
self.unlocalized.full_name.as_deref() |
| 336 |
} |
| 337 |
|
| 338 |
/// Get the name parts. |
| 339 |
pub fn name_parts(&self) -> Option<&NameParts> { |
| 340 |
self.unlocalized.name_parts.as_ref() |
| 341 |
} |
| 342 |
|
| 343 |
/// Get the nick names. |
| 344 |
pub fn nick_names(&self) -> &[String] { |
| 345 |
self.unlocalized.nick_names.as_deref().unwrap_or_default() |
| 346 |
} |
| 347 |
|
| 348 |
/// Get the titles. |
| 349 |
pub fn titles(&self) -> &[String] { |
| 350 |
self.unlocalized.titles.as_deref().unwrap_or_default() |
| 351 |
} |
| 352 |
|
| 353 |
/// Get the organizational roles. |
| 354 |
pub fn roles(&self) -> &[String] { |
| 355 |
self.unlocalized.roles.as_deref().unwrap_or_default() |
| 356 |
} |
| 357 |
|
| 358 |
/// Get the organization names. |
| 359 |
pub fn organization_names(&self) -> &[String] { |
| 360 |
self.unlocalized |
| 361 |
.organization_names |
| 362 |
.as_deref() |
| 363 |
.unwrap_or_default() |
| 364 |
} |
| 365 |
|
| 366 |
/// Get the first organization name. |
| 367 |
pub fn organization_name(&self) -> Option<&str> { |
| 368 |
self.organization_names().first().map(|x| x.as_str()) |
| 369 |
} |
| 370 |
|
| 371 |
/// Get the postal addresses. |
| 372 |
pub fn postal_addresses(&self) -> &[PostalAddress] { |
| 373 |
self.unlocalized |
| 374 |
.postal_addresses |
| 375 |
.as_deref() |
| 376 |
.unwrap_or_default() |
| 377 |
} |
| 378 |
|
| 379 |
/// Get the first postal address. |
| 380 |
pub fn postal_address(&self) -> Option<&PostalAddress> { |
| 381 |
self.postal_addresses().first() |
| 382 |
} |
| 383 |
|
| 384 |
/// Get the emails. |
| 385 |
pub fn emails(&self) -> &[Email] { |
| 386 |
self.emails.as_deref().unwrap_or_default() |
| 387 |
} |
| 388 |
|
| 389 |
/// Get the first email. |
| 390 |
pub fn email(&self) -> Option<&Email> { |
| 391 |
self.emails().first() |
| 392 |
} |
| 393 |
|
| 394 |
/// Get the phones. |
| 395 |
pub fn phones(&self) -> &[Phone] { |
| 396 |
self.phones.as_deref().unwrap_or_default() |
| 397 |
} |
| 398 |
|
| 399 |
/// Get the first phone. |
| 400 |
pub fn phone(&self) -> Option<&Phone> { |
| 401 |
self.phones().first() |
| 402 |
} |
| 403 |
|
| 404 |
/// Get the first phone with the voice feature. |
| 405 |
pub fn voice_phone(&self) -> Option<&Phone> { |
| 406 |
self.phones() |
| 407 |
.iter() |
| 408 |
.find(|phone| phone.features().contains(&"voice".to_string())) |
| 409 |
} |
| 410 |
|
| 411 |
/// Get the first phone with the fax feature. |
| 412 |
pub fn fax_phone(&self) -> Option<&Phone> { |
| 413 |
self.phones() |
| 414 |
.iter() |
| 415 |
.find(|phone| phone.features().contains(&"fax".to_string())) |
| 416 |
} |
| 417 |
|
| 418 |
/// Get the voice phone else get the first phone. |
| 419 |
pub fn prefer_voice_phone(&self) -> Option<&Phone> { |
| 420 |
self.voice_phone().or_else(|| self.phone()) |
| 421 |
} |
| 422 |
|
| 423 |
/// Get the contact uris. |
| 424 |
pub fn contact_uris(&self) -> &[String] { |
| 425 |
self.contact_uris.as_deref().unwrap_or_default() |
| 426 |
} |
| 427 |
|
| 428 |
/// Get the first contact uri. |
| 429 |
pub fn contact_uri(&self) -> Option<&str> { |
| 430 |
self.contact_uris().first().map(|x| x.as_str()) |
| 431 |
} |
| 432 |
|
| 433 |
/// Get the URLs. |
| 434 |
pub fn urls(&self) -> &[String] { |
| 435 |
self.urls.as_deref().unwrap_or_default() |
| 436 |
} |
| 437 |
|
| 438 |
/// Get the first URL. |
| 439 |
pub fn url(&self) -> Option<&str> { |
| 440 |
self.urls().first().map(|x| x.as_str()) |
| 441 |
} |
| 442 |
|
| 443 |
/// Get a localization for a language tag. |
| 444 |
pub fn localization(&self, lang: &str) -> Option<&Localizable> { |
| 445 |
self.localizations.get(lang) |
| 446 |
} |
| 447 |
|
| 448 |
/// Get an iterator over the localizations. |
| 449 |
pub fn localizations_iter(&self) -> impl Iterator<Item = (&String, &Localizable)> { |
| 450 |
self.localizations.iter() |
| 451 |
} |
| 452 |
|
| 453 |
/// Get a mutable iterator over the localizations. |
| 454 |
pub fn localizations_iter_mut(&mut self) -> impl Iterator<Item = (&String, &mut Localizable)> { |
| 455 |
self.localizations.iter_mut() |
| 456 |
} |
| 457 |
|
| 458 |
/// Are there no localizations. |
| 459 |
pub fn localizations_is_empty(&self) -> bool { |
| 460 |
self.localizations.is_empty() |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
/// Represents parts of the contact that can be localized. |
| 465 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 466 |
pub struct Localizable { |
| 467 |
/// Full name of the contact. |
| 468 |
pub(crate) full_name: Option<String>, |
| 469 |
|
| 470 |
/// Structured parts of the name. |
| 471 |
pub(crate) name_parts: Option<NameParts>, |
| 472 |
|
| 473 |
/// Nick names. |
| 474 |
pub(crate) nick_names: Option<Vec<String>>, |
| 475 |
|
| 476 |
/// Titles. |
| 477 |
pub(crate) titles: Option<Vec<String>>, |
| 478 |
|
| 479 |
/// Organizational Roles |
| 480 |
pub(crate) roles: Option<Vec<String>>, |
| 481 |
|
| 482 |
/// Organization names. |
| 483 |
pub(crate) organization_names: Option<Vec<String>>, |
| 484 |
|
| 485 |
/// Postal addresses. |
| 486 |
pub(crate) postal_addresses: Option<Vec<PostalAddress>>, |
| 487 |
} |
| 488 |
|
| 489 |
#[buildstructor::buildstructor] |
| 490 |
impl Localizable { |
| 491 |
#[builder(visibility = "pub")] |
| 492 |
fn new( |
| 493 |
full_name: Option<String>, |
| 494 |
name_parts: Option<NameParts>, |
| 495 |
nick_names: Vec<String>, |
| 496 |
titles: Vec<String>, |
| 497 |
roles: Vec<String>, |
| 498 |
organization_names: Vec<String>, |
| 499 |
postal_addresses: Vec<PostalAddress>, |
| 500 |
) -> Self { |
| 501 |
Self { |
| 502 |
full_name, |
| 503 |
name_parts, |
| 504 |
nick_names: to_opt_vec(nick_names), |
| 505 |
titles: to_opt_vec(titles), |
| 506 |
roles: to_opt_vec(roles), |
| 507 |
organization_names: to_opt_vec(organization_names), |
| 508 |
postal_addresses: to_opt_vec(postal_addresses), |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
/// Get the full name. |
| 513 |
pub fn full_name(&self) -> Option<&str> { |
| 514 |
self.full_name.as_deref() |
| 515 |
} |
| 516 |
|
| 517 |
/// Get the name parts. |
| 518 |
pub fn name_parts(&self) -> Option<&NameParts> { |
| 519 |
self.name_parts.as_ref() |
| 520 |
} |
| 521 |
|
| 522 |
/// Get the nick names. |
| 523 |
pub fn nick_names(&self) -> &[String] { |
| 524 |
self.nick_names.as_deref().unwrap_or_default() |
| 525 |
} |
| 526 |
|
| 527 |
/// Get the titles. |
| 528 |
pub fn titles(&self) -> &[String] { |
| 529 |
self.titles.as_deref().unwrap_or_default() |
| 530 |
} |
| 531 |
|
| 532 |
/// Get the organizational roles. |
| 533 |
pub fn roles(&self) -> &[String] { |
| 534 |
self.roles.as_deref().unwrap_or_default() |
| 535 |
} |
| 536 |
|
| 537 |
/// Get the organization names. |
| 538 |
pub fn organization_names(&self) -> &[String] { |
| 539 |
self.organization_names.as_deref().unwrap_or_default() |
| 540 |
} |
| 541 |
|
| 542 |
/// Get the first organization name. |
| 543 |
pub fn organization_name(&self) -> Option<&str> { |
| 544 |
self.organization_names().first().map(|x| x.as_str()) |
| 545 |
} |
| 546 |
|
| 547 |
/// Get the postal addresses. |
| 548 |
pub fn postal_addresses(&self) -> &[PostalAddress] { |
| 549 |
self.postal_addresses.as_deref().unwrap_or_default() |
| 550 |
} |
| 551 |
|
| 552 |
/// Get the first postal address. |
| 553 |
pub fn postal_address(&self) -> Option<&PostalAddress> { |
| 554 |
self.postal_addresses().first() |
| 555 |
} |
| 556 |
|
| 557 |
/// Set the set of postal addresses to only be the passed in postal address. |
| 558 |
pub fn with_postal_address(mut self, postal_address: PostalAddress) -> Self { |
| 559 |
self.postal_addresses = Some(vec![postal_address]); |
| 560 |
self |
| 561 |
} |
| 562 |
|
| 563 |
/// Set the complete set of postal addresses. |
| 564 |
pub fn with_postal_addresses(mut self, postal_addresses: Vec<PostalAddress>) -> Self { |
| 565 |
self.postal_addresses = Some(postal_addresses); |
| 566 |
self |
| 567 |
} |
| 568 |
|
| 569 |
/// Set the full name. |
| 570 |
pub fn with_full_name(mut self, full_name: String) -> Self { |
| 571 |
self.full_name = Some(full_name); |
| 572 |
self |
| 573 |
} |
| 574 |
|
| 575 |
/// Set the organization names. |
| 576 |
pub fn with_organization_names(mut self, organization_names: Vec<String>) -> Self { |
| 577 |
self.organization_names = Some(organization_names); |
| 578 |
self |
| 579 |
} |
| 580 |
|
| 581 |
/// Set the name parts. |
| 582 |
pub fn with_name_parts(mut self, name_parts: Option<NameParts>) -> Self { |
| 583 |
self.name_parts = name_parts; |
| 584 |
self |
| 585 |
} |
| 586 |
|
| 587 |
/// Set the nick names. |
| 588 |
pub fn with_nick_names(mut self, nick_names: Vec<String>) -> Self { |
| 589 |
self.nick_names = (!nick_names.is_empty()).then_some(nick_names); |
| 590 |
self |
| 591 |
} |
| 592 |
|
| 593 |
/// Set the titles. |
| 594 |
pub fn with_titles(mut self, titles: Vec<String>) -> Self { |
| 595 |
self.titles = (!titles.is_empty()).then_some(titles); |
| 596 |
self |
| 597 |
} |
| 598 |
|
| 599 |
/// Set the organizational roles. |
| 600 |
pub fn with_roles(mut self, roles: Vec<String>) -> Self { |
| 601 |
self.roles = (!roles.is_empty()).then_some(roles); |
| 602 |
self |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
/// The language preference of the contact. |
| 607 |
#[derive(Debug, Builder, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 608 |
pub struct Lang { |
| 609 |
/// The ordinal of the preference for this language. |
| 610 |
pub preference: Option<u64>, |
| 611 |
|
| 612 |
/// RFC 5646 language tag. |
| 613 |
pub tag: String, |
| 614 |
} |
| 615 |
|
| 616 |
impl Lang { |
| 617 |
/// Get the preference. |
| 618 |
pub fn preference(&self) -> Option<u64> { |
| 619 |
self.preference |
| 620 |
} |
| 621 |
|
| 622 |
/// Get the RFC 5646 language tag. |
| 623 |
pub fn tag(&self) -> &str { |
| 624 |
self.tag.as_str() |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
impl Display for Lang { |
| 629 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 630 |
if let Some(pref) = self.preference { |
| 631 |
write!(f, "{} (pref: {})", self.tag, pref) |
| 632 |
} else { |
| 633 |
f.write_str(&self.tag) |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
/// Name parts of a name. |
| 639 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 640 |
pub struct NameParts { |
| 641 |
/// Name prefixes such as titles or honorifics (e.g. "Mr., Mrs., Dr."). |
| 642 |
pub prefixes: Option<Vec<String>>, |
| 643 |
|
| 644 |
/// Surnames or last names. |
| 645 |
pub surnames: Option<Vec<String>>, |
| 646 |
|
| 647 |
/// Middle names. |
| 648 |
pub middle_names: Option<Vec<String>>, |
| 649 |
|
| 650 |
/// Given or first names. |
| 651 |
pub given_names: Option<Vec<String>>, |
| 652 |
|
| 653 |
/// Name suffixes such as credentials and honorifics (e.g. "Esq."). |
| 654 |
pub suffixes: Option<Vec<String>>, |
| 655 |
|
| 656 |
/// Generation markers (e.g. "Jr.", "III"). |
| 657 |
pub generations: Option<Vec<String>>, |
| 658 |
} |
| 659 |
|
| 660 |
#[buildstructor::buildstructor] |
| 661 |
impl NameParts { |
| 662 |
#[builder(visibility = "pub")] |
| 663 |
fn new( |
| 664 |
prefixes: Vec<String>, |
| 665 |
surnames: Vec<String>, |
| 666 |
middle_names: Vec<String>, |
| 667 |
given_names: Vec<String>, |
| 668 |
suffixes: Vec<String>, |
| 669 |
generations: Vec<String>, |
| 670 |
) -> Self { |
| 671 |
Self { |
| 672 |
prefixes: to_opt_vec(prefixes), |
| 673 |
surnames: to_opt_vec(surnames), |
| 674 |
middle_names: to_opt_vec(middle_names), |
| 675 |
given_names: to_opt_vec(given_names), |
| 676 |
suffixes: to_opt_vec(suffixes), |
| 677 |
generations: to_opt_vec(generations), |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
/// Get the name prefixes. |
| 682 |
pub fn prefixes(&self) -> &[String] { |
| 683 |
self.prefixes.as_deref().unwrap_or_default() |
| 684 |
} |
| 685 |
|
| 686 |
/// Get the first prefix. |
| 687 |
pub fn prefix(&self) -> Option<&str> { |
| 688 |
self.prefixes().first().map(|x| x.as_str()) |
| 689 |
} |
| 690 |
|
| 691 |
/// Get the sur names. |
| 692 |
pub fn surnames(&self) -> &[String] { |
| 693 |
self.surnames.as_deref().unwrap_or_default() |
| 694 |
} |
| 695 |
|
| 696 |
/// Get the first surname. |
| 697 |
pub fn surname(&self) -> Option<&str> { |
| 698 |
self.surnames().first().map(|x| x.as_str()) |
| 699 |
} |
| 700 |
|
| 701 |
/// Get the middle names. |
| 702 |
pub fn middle_names(&self) -> &[String] { |
| 703 |
self.middle_names.as_deref().unwrap_or_default() |
| 704 |
} |
| 705 |
|
| 706 |
/// Get the first middle name. |
| 707 |
pub fn middle_name(&self) -> Option<&str> { |
| 708 |
self.middle_names().first().map(|x| x.as_str()) |
| 709 |
} |
| 710 |
|
| 711 |
/// Get the given names. |
| 712 |
pub fn given_names(&self) -> &[String] { |
| 713 |
self.given_names.as_deref().unwrap_or_default() |
| 714 |
} |
| 715 |
|
| 716 |
/// Get the first given name. |
| 717 |
pub fn given_name(&self) -> Option<&str> { |
| 718 |
self.given_names().first().map(|x| x.as_str()) |
| 719 |
} |
| 720 |
|
| 721 |
/// Get the suffixes. |
| 722 |
pub fn suffixes(&self) -> &[String] { |
| 723 |
self.suffixes.as_deref().unwrap_or_default() |
| 724 |
} |
| 725 |
|
| 726 |
/// Get the first suffix. |
| 727 |
pub fn suffix(&self) -> Option<&str> { |
| 728 |
self.suffixes().first().map(|x| x.as_str()) |
| 729 |
} |
| 730 |
|
| 731 |
/// Get the generations. |
| 732 |
pub fn generations(&self) -> &[String] { |
| 733 |
self.generations.as_deref().unwrap_or_default() |
| 734 |
} |
| 735 |
|
| 736 |
/// Get the first generation. |
| 737 |
pub fn generation(&self) -> Option<&str> { |
| 738 |
self.generations().first().map(|x| x.as_str()) |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
/// A postal address. |
| 743 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 744 |
pub struct PostalAddress { |
| 745 |
/// Preference of this address in relation to others. |
| 746 |
pub preference: Option<u64>, |
| 747 |
|
| 748 |
/// Work, home, etc.... Known as "type" in JCard. |
| 749 |
pub contexts: Option<Vec<String>>, |
| 750 |
|
| 751 |
/// An unstructured address. An unstructured postal address is |
| 752 |
/// usually the complete postal address. That is, this string |
| 753 |
/// would contain the street address, country, region, postal code, etc... |
| 754 |
/// |
| 755 |
/// Depending on how the postal address is given, it can either |
| 756 |
/// be structured or unstructured. If it is given as unstructured, |
| 757 |
/// then this value is populated. |
| 758 |
/// |
| 759 |
/// It is possible that a single postal address is given as both, |
| 760 |
/// in which case this value is populated along with the other |
| 761 |
/// values of the postal address. |
| 762 |
pub full_address: Option<String>, |
| 763 |
|
| 764 |
/// Individual street lines. |
| 765 |
pub street_parts: Option<Vec<String>>, |
| 766 |
|
| 767 |
/// City name, county name, etc... |
| 768 |
pub locality: Option<String>, |
| 769 |
|
| 770 |
/// Name of region (i.e. state, province, etc...). |
| 771 |
pub region_name: Option<String>, |
| 772 |
|
| 773 |
/// Code for region. |
| 774 |
pub region_code: Option<String>, |
| 775 |
|
| 776 |
/// Name of the country. |
| 777 |
pub country_name: Option<String>, |
| 778 |
|
| 779 |
/// Code of the country. |
| 780 |
pub country_code: Option<String>, |
| 781 |
|
| 782 |
/// Postal code. |
| 783 |
pub postal_code: Option<String>, |
| 784 |
} |
| 785 |
|
| 786 |
#[buildstructor::buildstructor] |
| 787 |
impl PostalAddress { |
| 788 |
#[builder(visibility = "pub")] |
| 789 |
fn new( |
| 790 |
preference: Option<u64>, |
| 791 |
contexts: Vec<String>, |
| 792 |
full_address: Option<String>, |
| 793 |
street_parts: Vec<String>, |
| 794 |
locality: Option<String>, |
| 795 |
region_name: Option<String>, |
| 796 |
region_code: Option<String>, |
| 797 |
country_name: Option<String>, |
| 798 |
country_code: Option<String>, |
| 799 |
postal_code: Option<String>, |
| 800 |
) -> Self { |
| 801 |
Self { |
| 802 |
preference, |
| 803 |
contexts: to_opt_vec(contexts), |
| 804 |
full_address, |
| 805 |
street_parts: to_opt_vec(street_parts), |
| 806 |
locality, |
| 807 |
region_name, |
| 808 |
region_code, |
| 809 |
country_name, |
| 810 |
country_code, |
| 811 |
postal_code, |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
/// Get the preference. |
| 816 |
pub fn preference(&self) -> Option<u64> { |
| 817 |
self.preference |
| 818 |
} |
| 819 |
|
| 820 |
/// Get the contexts. |
| 821 |
pub fn contexts(&self) -> &[String] { |
| 822 |
self.contexts.as_deref().unwrap_or_default() |
| 823 |
} |
| 824 |
|
| 825 |
/// Get the full address. |
| 826 |
/// |
| 827 |
/// An unstructured address. An unstructured postal address is |
| 828 |
/// usually the complete postal address. That is, this string |
| 829 |
/// would contain the street address, country, region, postal code, etc... |
| 830 |
/// |
| 831 |
/// Depending on how the postal address is given, it can either |
| 832 |
/// be structured or unstructured. If it is given as unstructured, |
| 833 |
/// then this value is populated. |
| 834 |
/// |
| 835 |
/// It is possible that a single postal address is given as both, |
| 836 |
/// in which case this value is populated along with the other |
| 837 |
/// values of the postal address. |
| 838 |
pub fn full_address(&self) -> Option<&str> { |
| 839 |
self.full_address.as_deref() |
| 840 |
} |
| 841 |
|
| 842 |
/// Get the street parts. |
| 843 |
pub fn street_parts(&self) -> &[String] { |
| 844 |
self.street_parts.as_deref().unwrap_or_default() |
| 845 |
} |
| 846 |
|
| 847 |
/// Get the locality. |
| 848 |
pub fn locality(&self) -> Option<&str> { |
| 849 |
self.locality.as_deref() |
| 850 |
} |
| 851 |
|
| 852 |
/// Get the region name. |
| 853 |
pub fn region_name(&self) -> Option<&str> { |
| 854 |
self.region_name.as_deref() |
| 855 |
} |
| 856 |
|
| 857 |
/// Get the region code. |
| 858 |
pub fn region_code(&self) -> Option<&str> { |
| 859 |
self.region_code.as_deref() |
| 860 |
} |
| 861 |
|
| 862 |
/// Get the country name. |
| 863 |
pub fn country_name(&self) -> Option<&str> { |
| 864 |
self.country_name.as_deref() |
| 865 |
} |
| 866 |
|
| 867 |
/// Get the country code. |
| 868 |
pub fn country_code(&self) -> Option<&str> { |
| 869 |
self.country_code.as_deref() |
| 870 |
} |
| 871 |
|
| 872 |
/// Get the postal code. |
| 873 |
pub fn postal_code(&self) -> Option<&str> { |
| 874 |
self.postal_code.as_deref() |
| 875 |
} |
| 876 |
|
| 877 |
/// Set the postal code. |
| 878 |
pub fn with_postal_code(mut self, postal_code: String) -> Self { |
| 879 |
self.postal_code = Some(postal_code); |
| 880 |
self |
| 881 |
} |
| 882 |
|
| 883 |
/// Set the locality. |
| 884 |
pub fn with_locality(mut self, locality: String) -> Self { |
| 885 |
self.locality = Some(locality); |
| 886 |
self |
| 887 |
} |
| 888 |
|
| 889 |
pub fn with_street_parts(mut self, street_parts: Vec<String>) -> Self { |
| 890 |
self.street_parts = Some(street_parts); |
| 891 |
self |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
/// Represents an email address. |
| 896 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 897 |
pub struct Email { |
| 898 |
/// Preference of this email in relation to others. |
| 899 |
pub preference: Option<u64>, |
| 900 |
|
| 901 |
/// Work, home, etc.... Known as "type" in JCard. |
| 902 |
pub contexts: Option<Vec<String>>, |
| 903 |
|
| 904 |
/// The email address. |
| 905 |
pub email: String, |
| 906 |
} |
| 907 |
|
| 908 |
#[buildstructor::buildstructor] |
| 909 |
impl Email { |
| 910 |
#[builder(visibility = "pub")] |
| 911 |
fn new(preference: Option<u64>, contexts: Vec<String>, email: String) -> Self { |
| 912 |
Self { |
| 913 |
preference, |
| 914 |
contexts: to_opt_vec(contexts), |
| 915 |
email, |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
/// Get the preference. |
| 920 |
pub fn preference(&self) -> Option<u64> { |
| 921 |
self.preference |
| 922 |
} |
| 923 |
|
| 924 |
/// Get the contexts. |
| 925 |
pub fn contexts(&self) -> &[String] { |
| 926 |
self.contexts.as_deref().unwrap_or_default() |
| 927 |
} |
| 928 |
|
| 929 |
/// Get the email address. |
| 930 |
pub fn email(&self) -> &str { |
| 931 |
self.email.as_str() |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
impl Display for Email { |
| 936 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 937 |
let mut qualifiers = vec![]; |
| 938 |
if let Some(pref) = self.preference { |
| 939 |
qualifiers.push(format!("(pref: {pref})")); |
| 940 |
} |
| 941 |
if let Some(contexts) = &self.contexts { |
| 942 |
qualifiers.push(format!("({})", contexts.join(","))); |
| 943 |
} |
| 944 |
let qualifiers = qualifiers.join(" "); |
| 945 |
if qualifiers.is_empty() { |
| 946 |
f.write_str(&self.email) |
| 947 |
} else { |
| 948 |
write!(f, "{} {}", self.email, qualifiers) |
| 949 |
} |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
/// Represents phone number. |
| 954 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 955 |
pub struct Phone { |
| 956 |
/// Preference of this phone in relation to others. |
| 957 |
pub preference: Option<u64>, |
| 958 |
|
| 959 |
/// Work, home, etc.... Known as "type" in JCard. |
| 960 |
pub contexts: Option<Vec<String>>, |
| 961 |
|
| 962 |
/// The phone number. |
| 963 |
pub phone: String, |
| 964 |
|
| 965 |
/// Features (voice, fax, etc...) |
| 966 |
pub features: Option<Vec<String>>, |
| 967 |
} |
| 968 |
|
| 969 |
#[buildstructor::buildstructor] |
| 970 |
impl Phone { |
| 971 |
#[builder(visibility = "pub")] |
| 972 |
fn new( |
| 973 |
preference: Option<u64>, |
| 974 |
contexts: Vec<String>, |
| 975 |
phone: String, |
| 976 |
features: Vec<String>, |
| 977 |
) -> Self { |
| 978 |
Self { |
| 979 |
preference, |
| 980 |
contexts: to_opt_vec(contexts), |
| 981 |
phone, |
| 982 |
features: to_opt_vec(features), |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
/// Get the preference. |
| 987 |
pub fn preference(&self) -> Option<u64> { |
| 988 |
self.preference |
| 989 |
} |
| 990 |
|
| 991 |
/// Get the contexts. |
| 992 |
pub fn contexts(&self) -> &[String] { |
| 993 |
self.contexts.as_deref().unwrap_or_default() |
| 994 |
} |
| 995 |
|
| 996 |
/// Get the phone number. |
| 997 |
pub fn phone(&self) -> &str { |
| 998 |
self.phone.as_str() |
| 999 |
} |
| 1000 |
|
| 1001 |
/// Get the phone features. |
| 1002 |
pub fn features(&self) -> &[String] { |
| 1003 |
self.features.as_deref().unwrap_or_default() |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
impl Display for Phone { |
| 1008 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 1009 |
let mut qualifiers = vec![]; |
| 1010 |
if let Some(pref) = self.preference { |
| 1011 |
qualifiers.push(format!("(pref: {pref})")); |
| 1012 |
} |
| 1013 |
if let Some(contexts) = &self.contexts { |
| 1014 |
qualifiers.push(format!("({})", contexts.join(","))); |
| 1015 |
} |
| 1016 |
if let Some(features) = &self.features { |
| 1017 |
qualifiers.push(format!("({})", features.join(","))); |
| 1018 |
} |
| 1019 |
let qualifiers = qualifiers.join(" "); |
| 1020 |
if qualifiers.is_empty() { |
| 1021 |
f.write_str(&self.phone) |
| 1022 |
} else { |
| 1023 |
write!(f, "{} {}", self.phone, qualifiers) |
| 1024 |
} |
| 1025 |
} |
| 1026 |
} |