| 1 |
//! Convert a Contact to jCard/vCard. |
| 2 |
use std::str::FromStr; |
| 3 |
|
| 4 |
use serde_json::{Map, Value, json}; |
| 5 |
|
| 6 |
use super::Contact; |
| 7 |
|
| 8 |
impl Contact { |
| 9 |
/// Output the Contact data as vCard in JSON values ([`Vec<Value>`]). |
| 10 |
/// |
| 11 |
/// ```rust |
| 12 |
/// use icann_rdap_common::contact::Contact; |
| 13 |
/// use serde::Serialize; |
| 14 |
/// use serde_json::Value; |
| 15 |
/// |
| 16 |
/// let contact = Contact::builder() |
| 17 |
/// .kind("individual") |
| 18 |
/// .full_name("Bob Smurd") |
| 19 |
/// .build(); |
| 20 |
/// |
| 21 |
/// let v = contact.to_vcard(); |
| 22 |
/// let json = serde_json::to_string(&v); |
| 23 |
/// ``` |
| 24 |
pub fn to_vcard(&self) -> Vec<Value> { |
| 25 |
// start the vcard with the version. |
| 26 |
let mut vcard: Vec<Value> = vec![json!(["version", {}, "text", "4.0"])]; |
| 27 |
|
| 28 |
if let Some(full_name) = &self.full_name() { |
| 29 |
vcard.push(json!(["fn", {}, "text", full_name])); |
| 30 |
} |
| 31 |
|
| 32 |
if let Some(name_parts) = &self.name_parts() { |
| 33 |
let surnames = vec_string_to_value(&name_parts.surnames); |
| 34 |
let given_names = vec_string_to_value(&name_parts.given_names); |
| 35 |
let middle_names = vec_string_to_value(&name_parts.middle_names); |
| 36 |
let prefixes = vec_string_to_value(&name_parts.prefixes); |
| 37 |
let suffixes = vec_string_to_value(&name_parts.suffixes); |
| 38 |
vcard.push(json!([ |
| 39 |
"n", |
| 40 |
{}, |
| 41 |
"text", |
| 42 |
[surnames, given_names, middle_names, prefixes, suffixes] |
| 43 |
])); |
| 44 |
} |
| 45 |
|
| 46 |
if let Some(kind) = &self.kind { |
| 47 |
vcard.push(json!(["kind", {}, "text", kind])); |
| 48 |
} |
| 49 |
|
| 50 |
if let Some(langs) = &self.langs { |
| 51 |
for lang in langs { |
| 52 |
let mut params: Map<String, Value> = Map::new(); |
| 53 |
if let Some(pref) = lang.preference { |
| 54 |
params.insert("pref".to_string(), Value::String(pref.to_string())); |
| 55 |
} |
| 56 |
vcard.push(json!([ |
| 57 |
"lang", |
| 58 |
Value::from(params), |
| 59 |
"language-tag", |
| 60 |
lang.tag |
| 61 |
])) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
for org_name in self.organization_names() { |
| 66 |
vcard.push(json!(["org", {}, "text", org_name])); |
| 67 |
} |
| 68 |
|
| 69 |
for title in self.titles() { |
| 70 |
vcard.push(json!(["title", {}, "text", title])); |
| 71 |
} |
| 72 |
|
| 73 |
for role in self.roles() { |
| 74 |
vcard.push(json!(["role", {}, "text", role])); |
| 75 |
} |
| 76 |
|
| 77 |
for nick_name in self.nick_names() { |
| 78 |
vcard.push(json!(["nickname", {}, "text", nick_name])); |
| 79 |
} |
| 80 |
|
| 81 |
if let Some(emails) = &self.emails { |
| 82 |
for email in emails { |
| 83 |
let mut params: Map<String, Value> = Map::new(); |
| 84 |
if let Some(pref) = email.preference { |
| 85 |
params.insert("pref".to_string(), Value::String(pref.to_string())); |
| 86 |
} |
| 87 |
if let Some(contexts) = email.contexts.as_ref() { |
| 88 |
params.insert("type".to_string(), vec_string_to_param(contexts)); |
| 89 |
} |
| 90 |
vcard.push(json!(["email", Value::from(params), "text", email.email])) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if let Some(phones) = &self.phones { |
| 95 |
for phone in phones { |
| 96 |
let mut params: Map<String, Value> = Map::new(); |
| 97 |
if let Some(pref) = phone.preference { |
| 98 |
params.insert("pref".to_string(), Value::String(pref.to_string())); |
| 99 |
} |
| 100 |
let mut types: Vec<String> = vec![]; |
| 101 |
if let Some(contexts) = &phone.contexts { |
| 102 |
types.append(&mut contexts.clone()); |
| 103 |
} |
| 104 |
if let Some(features) = &phone.features { |
| 105 |
types.append(&mut features.clone()); |
| 106 |
} |
| 107 |
params.insert("type".to_string(), vec_string_to_param(&types)); |
| 108 |
vcard.push(json!(["tel", Value::from(params), "text", phone.phone])) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
for addr in self.postal_addresses() { |
| 113 |
let mut params: Map<String, Value> = Map::new(); |
| 114 |
if let Some(pref) = addr.preference { |
| 115 |
params.insert("pref".to_string(), Value::String(pref.to_string())); |
| 116 |
} |
| 117 |
if let Some(contexts) = addr.contexts.as_ref() { |
| 118 |
params.insert("type".to_string(), vec_string_to_param(contexts)); |
| 119 |
} |
| 120 |
if let Some(full_address) = &addr.full_address { |
| 121 |
params.insert( |
| 122 |
"label".to_string(), |
| 123 |
Value::from_str(full_address).expect("serializing full address"), |
| 124 |
); |
| 125 |
} |
| 126 |
let mut lines: Vec<String> = vec![]; |
| 127 |
if let Some(street_parts) = &addr.street_parts { |
| 128 |
lines.push(street_parts.first().cloned().unwrap_or("".to_string())); |
| 129 |
lines.push(street_parts.get(1).cloned().unwrap_or("".to_string())); |
| 130 |
lines.push(street_parts.get(2).cloned().unwrap_or("".to_string())); |
| 131 |
} else { |
| 132 |
lines.push("".to_string()); |
| 133 |
lines.push("".to_string()); |
| 134 |
lines.push("".to_string()); |
| 135 |
} |
| 136 |
if let Some(locality) = &addr.locality { |
| 137 |
lines.push(locality.to_owned()); |
| 138 |
} else { |
| 139 |
lines.push("".to_string()); |
| 140 |
} |
| 141 |
if let Some(region_name) = &addr.region_name { |
| 142 |
lines.push(region_name.to_owned()); |
| 143 |
} else if let Some(region_code) = &addr.region_code { |
| 144 |
lines.push(region_code.to_owned()); |
| 145 |
} else { |
| 146 |
lines.push("".to_string()); |
| 147 |
} |
| 148 |
if let Some(postal_code) = &addr.postal_code { |
| 149 |
lines.push(postal_code.to_owned()); |
| 150 |
} else { |
| 151 |
lines.push("".to_string()); |
| 152 |
} |
| 153 |
if let Some(country_name) = &addr.country_name { |
| 154 |
lines.push(country_name.to_owned()); |
| 155 |
} else { |
| 156 |
lines.push("".to_string()); |
| 157 |
} |
| 158 |
if let Some(country_code) = &addr.country_code { |
| 159 |
params.insert("cc".to_string(), Value::String(country_code.to_string())); |
| 160 |
} |
| 161 |
vcard.push(json!(["adr", Value::from(params), "text", lines])) |
| 162 |
} |
| 163 |
|
| 164 |
if let Some(contact_uris) = &self.contact_uris { |
| 165 |
for uri in contact_uris { |
| 166 |
vcard.push(json!(["contact-uri", {}, "uri", uri])); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if let Some(urls) = &self.urls { |
| 171 |
for url in urls { |
| 172 |
vcard.push(json!(["url", {}, "uri", url])); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// return the vcard array |
| 177 |
vec![Value::String("vcard".to_string()), Value::from(vcard)] |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
fn vec_string_to_value(strings: &Option<Vec<String>>) -> Value { |
| 182 |
let Some(strings) = strings else { |
| 183 |
return Value::String("".to_string()); |
| 184 |
}; |
| 185 |
|
| 186 |
if strings.is_empty() { |
| 187 |
return Value::String("".to_string()); |
| 188 |
}; |
| 189 |
|
| 190 |
if strings.len() == 1 { |
| 191 |
let Some(one) = strings.first() else { |
| 192 |
panic!("couldn't get first element on length of 1") |
| 193 |
}; |
| 194 |
return Value::String(one.to_owned()); |
| 195 |
}; |
| 196 |
|
| 197 |
// else |
| 198 |
Value::from(strings.clone()) |
| 199 |
} |
| 200 |
|
| 201 |
fn vec_string_to_param(strings: &[String]) -> Value { |
| 202 |
if strings.is_empty() { |
| 203 |
return Value::String("".to_string()); |
| 204 |
}; |
| 205 |
|
| 206 |
if strings.len() == 1 { |
| 207 |
let Some(one) = strings.first() else { |
| 208 |
panic!("couldn't get first element on length of 1") |
| 209 |
}; |
| 210 |
return Value::String(one.to_owned()); |
| 211 |
}; |
| 212 |
|
| 213 |
// else |
| 214 |
Value::from(strings) |
| 215 |
} |
| 216 |
|
| 217 |
#[cfg(test)] |
| 218 |
mod tests { |
| 219 |
|
| 220 |
use crate::contact::{Contact, Email, Lang, NameParts, Phone, PostalAddress}; |
| 221 |
|
| 222 |
#[test] |
| 223 |
fn test_contact_vcard_round_trip() { |
| 224 |
// GIVEN |
| 225 |
let contact = Contact::builder() |
| 226 |
.full_name("Joe User") |
| 227 |
.name_parts( |
| 228 |
NameParts::builder() |
| 229 |
.surnames(vec!["User".to_string()]) |
| 230 |
.given_names(vec!["Joe".to_string()]) |
| 231 |
.suffixes(vec!["ing. jr".to_string(), "M.Sc.".to_string()]) |
| 232 |
.build(), |
| 233 |
) |
| 234 |
.kind("individual") |
| 235 |
.langs(vec![ |
| 236 |
Lang::builder().preference(1).tag("fr").build(), |
| 237 |
Lang::builder().preference(2).tag("en").build(), |
| 238 |
]) |
| 239 |
.organization_names(vec!["Example".to_string()]) |
| 240 |
.titles(vec!["Research Scientist".to_string()]) |
| 241 |
.roles(vec!["Project Lead".to_string()]) |
| 242 |
.contact_uris(vec!["https://example.com/contact-form".to_string()]) |
| 243 |
.postal_addresses(vec![ |
| 244 |
PostalAddress::builder() |
| 245 |
.context("work") |
| 246 |
.country_code("CA") |
| 247 |
.country_name("Canada") |
| 248 |
.postal_code("G1V 2M2") |
| 249 |
.region_code("QC") |
| 250 |
.locality("Quebec") |
| 251 |
.street_parts(vec![ |
| 252 |
"Suite 1234".to_string(), |
| 253 |
"4321 Rue Somewhere".to_string(), |
| 254 |
]) |
| 255 |
.build(), |
| 256 |
]) |
| 257 |
.phones(vec![ |
| 258 |
Phone::builder() |
| 259 |
.preference(1) |
| 260 |
.contexts(vec!["work".to_string()]) |
| 261 |
.features(vec!["voice".to_string()]) |
| 262 |
.phone("tel:+1-555-555-1234;ext=102") |
| 263 |
.build(), |
| 264 |
Phone::builder() |
| 265 |
.contexts(vec!["work".to_string(), "cell".to_string()]) |
| 266 |
.features(vec![ |
| 267 |
"voice".to_string(), |
| 268 |
"video".to_string(), |
| 269 |
"text".to_string(), |
| 270 |
]) |
| 271 |
.phone("tel:+1-555-555-4321") |
| 272 |
.build(), |
| 273 |
]) |
| 274 |
.emails(vec![ |
| 275 |
Email::builder() |
| 276 |
.contexts(vec!["work".to_string()]) |
| 277 |
.email("[email protected]") |
| 278 |
.build(), |
| 279 |
]) |
| 280 |
.urls(vec!["https://example.com/some-url".to_string()]) |
| 281 |
.build(); |
| 282 |
|
| 283 |
// WHEN |
| 284 |
let actual = Contact::from_vcard(&contact.to_vcard()).expect("from vcard"); |
| 285 |
|
| 286 |
// THEN |
| 287 |
// yes, contact implements Eq, but by breaking everything done |
| 288 |
// we can find where problems lie faster. |
| 289 |
assert_eq!(contact.full_name(), actual.full_name()); |
| 290 |
assert_eq!(contact.name_parts(), actual.name_parts()); |
| 291 |
assert_eq!(contact.kind, actual.kind); |
| 292 |
assert_eq!(contact.langs, actual.langs); |
| 293 |
assert_eq!(contact.organization_names(), actual.organization_names()); |
| 294 |
assert_eq!(contact.titles(), actual.titles()); |
| 295 |
assert_eq!(contact.roles(), actual.roles()); |
| 296 |
assert_eq!(contact.postal_addresses(), actual.postal_addresses()); |
| 297 |
assert_eq!(contact.phones, actual.phones); |
| 298 |
assert_eq!(contact.emails, actual.emails); |
| 299 |
assert_eq!(contact.contact_uris, actual.contact_uris); |
| 300 |
assert_eq!(contact.urls, actual.urls); |
| 301 |
// but just in case we forgot something |
| 302 |
assert_eq!(contact, actual); |
| 303 |
} |
| 304 |
} |