1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use crate::*;
/// A structure to store contact information.
///
/// All fields correspond (at least somewhat) to
/// the [VCard 4 specification](https://datatracker.ietf.org/doc/html/rfc6350)
///
#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
pub struct Contact {
/*
* Explanatory Properties
*/
/// The Contact's Uid
pub uid: String,
/*
* Identification Property
*/
/// The Contacts Name
pub name: Name,
/// The Contact's Nickname
pub nickname: Option<String>,
/// The Contact's Anniversary
pub anniversary: Option<DateTime>,
/// The Contact's Birthday
pub bday: Option<DateTime>,
/// The Contact's Photo
pub photo: Option<Uri>,
/*
* Organisational Properties
*/
/// The Contact's Job Title
pub title: Option<String>,
/// The Contact's Job Role
pub role: Option<String>,
/// The Contact's Organization
pub org: Option<Org>,
/// The Contact's Organization's Logo
pub logo: Option<Uri>,
/*
* Communication Properties
*/
/// The Contact's contact information, emails,
/// phones etc
pub contact_information: Vec<ContactInformation>,
/// The contact's sex and gender
pub gender: Option<Gender>,
/*
* Deliver Addressing Properties
*/
/// The Contact's Work Address
pub work_address: Option<address::Address>,
/// The Contact's Home Address
pub home_address: Option<address::Address>,
}
impl Contact {
/// Generates a uuid for the contact's uid.
///
/// This should be called after cloning a contact,
/// as not to have a clashing uid which breaks
/// the specification.
///
/// The `uuid` crate is used for this generation.
///
/// # Example
///
/// Clone a contact and give it a new uid.
///
/// ```rust
/// use contack::{Name, Contact};
/// # let mut contact_1 = Contact::new(Name::default());
/// let mut contact = contact_1.clone();
/// contact.gen_uid();
/// assert_ne!(contact.uid, "");
/// ```
pub fn gen_uid(&mut self) {
// Generates a UID from the UUID Value
self.uid = uuid::Uuid::new_v4().to_string();
}
/// Creates a contact from a [`Name`]
///
/// It is required to use a `name` from the VCard
/// specification, alongside a `uid`, which is
/// generated automatically from the `uuid` crate.
///
/// # Example
///
/// Create a new contact with the name "John Doe"
///
/// ```rust
/// use contack::{Name, Contact};
/// let contact = Contact::new(Name {
/// given: vec!["John".to_string()],
/// family: vec!["Doe".to_string()],
/// ..Default::default()
/// });
/// ```
#[must_use]
pub fn new(name: name::Name) -> Self {
Self {
name,
uid: uuid::Uuid::new_v4().to_string(),
..Self::default()
}
}
}
#[cfg(feature = "read_write")]
#[cfg_attr(feature = "dox", doc(cfg(feature = "read_write")))]
impl From<Contact> for read_write::vcard::VCard {
fn from(c: Contact) -> Self {
// Vec to store propertiess
let mut vec = vec![
// Insert the UID
Component {
name: "UID".to_string(),
values: vec![vec![c.uid]],
..Component::default()
},
// And the name (FN followed by N)
Component {
name: "FN".to_string(),
values: vec![vec![c.name.to_string()]],
..Component::default()
},
c.name.into(),
];
// Nickname next
if let Some(nickname) = c.nickname {
vec.push(Component {
name: "NICKNAME".to_string(),
values: vec![vec![nickname]],
..Component::default()
});
}
// Anniversary
if let Some(anniversary) = c.anniversary {
let mut anniversary: Component = anniversary.into();
anniversary.name = "ANNIVERSARY".to_string();
vec.push(anniversary);
}
// Bithday
if let Some(bday) = c.bday {
let mut bday: Component = bday.into();
bday.name = "BDAY".to_string();
vec.push(bday);
}
// Photo
if let Some(photo) = c.photo {
let mut photo: Component = photo.into();
photo.name = "PHOTO".to_string();
vec.push(photo);
}
// Title
if let Some(title) = c.title {
vec.push(Component {
name: "TITLE".to_string(),
values: vec![vec![title]],
..Component::default()
});
}
// Role
if let Some(role) = c.role {
vec.push(Component {
name: "ROLE".to_string(),
values: vec![vec![role]],
..Component::default()
});
}
// Org
if let Some(org) = c.org {
vec.push(org.into());
}
// Logo
if let Some(logo) = c.logo {
let mut logo: Component = logo.into();
logo.name = "LOGO".to_string();
vec.push(logo);
}
// Contact information
for ci in c.contact_information {
vec.push(ci.into());
}
// Gender
if let Some(gender) = c.gender {
vec.push(gender.into());
}
// Work Address
if let Some(adr) = c.work_address {
let mut adr: Component = adr.into();
adr.parameters
.insert("TYPE".to_string(), "work".to_string());
vec.push(adr);
}
// Home Address
if let Some(adr) = c.home_address {
let mut adr: Component = adr.into();
adr.parameters
.insert("TYPE".to_string(), "home".to_string());
vec.push(adr);
}
Self::new(vec)
}
}
#[cfg(feature = "read_write")]
#[cfg_attr(feature = "dox", doc(cfg(feature = "read_write")))]
impl TryFrom<read_write::vcard::VCard> for Contact {
type Error = FromComponentError;
fn try_from(vcard: read_write::vcard::VCard) -> Result<Self, Self::Error> {
// Create a contact - with no name (for now)
let mut contact = Self::new(Name::default());
// Loop through all the components
for mut comp in vcard.0 {
match comp.name.as_str() {
"UID" => {
if let Some(uid) =
comp.values.pop().and_then(|mut x| x.pop())
{
contact.uid = uid;
}
}
"FN" => {
if contact.name == Name::default() {
if let Some(name) =
comp.values.pop().and_then(|mut x| x.pop())
{
contact.name.given = vec![name];
}
}
}
"N" => contact.name = comp.into(),
"NICKNAME" => {
contact.nickname = Some(
comp.values
.pop()
.and_then(|mut x| x.pop())
.ok_or(FromComponentError::NotEnoughValues)?,
);
}
"ANNIVERSARY" => contact.anniversary = Some(comp.try_into()?),
"BDAY" => contact.bday = Some(comp.try_into()?),
"PHOTO" => contact.photo = Some(comp.try_into()?),
"TITLE" => {
contact.title = Some(
comp.values
.pop()
.and_then(|mut x| x.pop())
.ok_or(FromComponentError::NotEnoughValues)?,
);
}
"ROLE" => {
contact.role = Some(
comp.values
.pop()
.and_then(|mut x| x.pop())
.ok_or(FromComponentError::NotEnoughValues)?,
);
}
"ORG" => contact.org = Some(Org::from(comp)),
"LOGO" => contact.logo = Some(comp.try_into()?),
"EMAIL" | "TEL" | "X-DISCORD" | "X-MATRIX" | "X-SKYPE"
| "X-AIM" | "X-JABBER" | "X-ICQ" | "X-GROUPWISE"
| "X-GADUGADU" | "IMPP" => {
contact.contact_information.push(comp.try_into()?);
}
"GENDER" => contact.gender = Some(comp.try_into()?),
// Addresses.
//
// Note that if ambigouous it is assumed that an address
// is home.
"ADR" => match comp
.parameters
.get("TYPE")
.map_or("home", String::as_str)
{
"work" => contact.work_address = Some(comp.try_into()?),
_ => contact.home_address = Some(comp.try_into()?),
},
// Values which we don't know shall be scrapped.
_ => (),
}
}
Ok(contact)
}
}