vcard 0.5.0

A pure Rust implementation of vCard 4.0 for generating and parsing vCards, based on RFC 6350 and RFC 9554.
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Value types used by vCard properties, defined in RFC 6350 section 4 and extended by RFC 9554.

mod date_time;
mod language_tag;
mod name;
mod uri;

use std::{
    fmt::{self, Display, Formatter, Write as _},
    str::FromStr,
};

pub use date_time::{Date, DateAndOrTime, DateTime, Time, Timestamp, UtcOffset, Zone};
pub use language_tag::LanguageTag;
pub use name::{AddressValue, NameValue};
pub use uri::Uri;
use validators::prelude::*;

use crate::{
    error::InvalidValueError,
    syntax::{is_token, split_unescaped, unescape_text, write_escaped_text},
};

/// A token made of ASCII letters, digits and hyphens.
///
/// This covers both the `iana-token` and the `x-name` rules of RFC 6350 and is used for extension names and values.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Token(String);

impl Token {
    /// Returns the token as a string slice.
    #[inline]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Checks whether this token is an `x-name`, which starts with `x-` or `X-`.
    #[inline]
    pub fn is_x_name(&self) -> bool {
        let bytes = self.0.as_bytes();

        bytes.len() > 2 && (bytes[0] == b'x' || bytes[0] == b'X') && bytes[1] == b'-'
    }
}

impl FromStr for Token {
    type Err = InvalidValueError;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if is_token(s) { Ok(Self(s.to_string())) } else { Err(InvalidValueError::new("token")) }
    }
}

impl Display for Token {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str(&self.0)
    }
}

/// The value of the KIND property, which describes what the vCard represents.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KindValue {
    /// A single person or entity.
    Individual,
    /// A group of persons or entities.
    Group,
    /// An organization.
    Org,
    /// A named geographical place.
    Location,
    /// An extension value which is an IANA token or an x-name.
    Extension(Token),
}

impl Display for KindValue {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::Individual => f.write_str("individual"),
            Self::Group => f.write_str("group"),
            Self::Org => f.write_str("org"),
            Self::Location => f.write_str("location"),
            Self::Extension(token) => Display::fmt(token, f),
        }
    }
}

impl FromStr for KindValue {
    type Err = InvalidValueError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Known values are compared case-insensitively as RFC 6350 requires.
        if s.eq_ignore_ascii_case("individual") {
            Ok(Self::Individual)
        } else if s.eq_ignore_ascii_case("group") {
            Ok(Self::Group)
        } else if s.eq_ignore_ascii_case("org") {
            Ok(Self::Org)
        } else if s.eq_ignore_ascii_case("location") {
            Ok(Self::Location)
        } else {
            Token::from_str(s).map(Self::Extension)
        }
    }
}

/// The sex component of the GENDER property.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Sex {
    /// Male, written as `M`.
    Male,
    /// Female, written as `F`.
    Female,
    /// Other, written as `O`.
    Other,
    /// None or not applicable, written as `N`.
    NoneOrNotApplicable,
    /// Unknown, written as `U`.
    Unknown,
}

impl Display for Sex {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str(match self {
            Self::Male => "M",
            Self::Female => "F",
            Self::Other => "O",
            Self::NoneOrNotApplicable => "N",
            Self::Unknown => "U",
        })
    }
}

impl FromStr for Sex {
    type Err = InvalidValueError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "M" | "m" => Ok(Self::Male),
            "F" | "f" => Ok(Self::Female),
            "O" | "o" => Ok(Self::Other),
            "N" | "n" => Ok(Self::NoneOrNotApplicable),
            "U" | "u" => Ok(Self::Unknown),
            _ => Err(InvalidValueError::new("sex")),
        }
    }
}

/// The structured value of the GENDER property.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct GenderValue {
    /// The sex component.
    pub sex:      Option<Sex>,
    /// The free-form gender identity component.
    pub identity: Option<String>,
}

impl Display for GenderValue {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        if let Some(sex) = self.sex {
            Display::fmt(&sex, f)?;
        }

        if let Some(identity) = &self.identity {
            f.write_char(';')?;

            write_escaped_text(f, identity, true)?;
        }

        Ok(())
    }
}

impl FromStr for GenderValue {
    type Err = InvalidValueError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts = split_unescaped(s, b';');

        let sex = match parts[0] {
            "" => None,
            sex => Some(Sex::from_str(sex)?),
        };

        let identity = match parts.get(1) {
            Some(identity) if !identity.is_empty() => Some(unescape_text(identity)),
            _ => None,
        };

        Ok(Self {
            sex,
            identity,
        })
    }
}

/// The value of the GRAMGENDER property defined by RFC 9554.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GramGenderValue {
    /// The animate grammatical gender.
    Animate,
    /// The common grammatical gender.
    Common,
    /// The feminine grammatical gender.
    Feminine,
    /// The inanimate grammatical gender.
    Inanimate,
    /// The masculine grammatical gender.
    Masculine,
    /// The neuter grammatical gender.
    Neuter,
    /// An extension value which is an IANA token or an x-name.
    Extension(Token),
}

impl Display for GramGenderValue {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::Animate => f.write_str("animate"),
            Self::Common => f.write_str("common"),
            Self::Feminine => f.write_str("feminine"),
            Self::Inanimate => f.write_str("inanimate"),
            Self::Masculine => f.write_str("masculine"),
            Self::Neuter => f.write_str("neuter"),
            Self::Extension(token) => Display::fmt(token, f),
        }
    }
}

impl FromStr for GramGenderValue {
    type Err = InvalidValueError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.eq_ignore_ascii_case("animate") {
            Ok(Self::Animate)
        } else if s.eq_ignore_ascii_case("common") {
            Ok(Self::Common)
        } else if s.eq_ignore_ascii_case("feminine") {
            Ok(Self::Feminine)
        } else if s.eq_ignore_ascii_case("inanimate") {
            Ok(Self::Inanimate)
        } else if s.eq_ignore_ascii_case("masculine") {
            Ok(Self::Masculine)
        } else if s.eq_ignore_ascii_case("neuter") {
            Ok(Self::Neuter)
        } else {
            Token::from_str(s).map(Self::Extension)
        }
    }
}

/// The structured value of the ORG property, which is an organization name followed by organizational unit names.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OrgValue {
    /// The organization name.
    pub name:  String,
    /// The names of the organizational units, from the largest to the smallest.
    pub units: Vec<String>,
}

impl Display for OrgValue {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write_escaped_text(f, &self.name, true)?;

        for unit in &self.units {
            f.write_char(';')?;

            write_escaped_text(f, unit, true)?;
        }

        Ok(())
    }
}

impl FromStr for OrgValue {
    type Err = InvalidValueError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts = split_unescaped(s, b';');

        Ok(Self {
            name:  unescape_text(parts[0]),
            units: parts[1..].iter().map(|unit| unescape_text(unit)).collect(),
        })
    }
}

/// The structured value of the CLIENTPIDMAP property, which maps a PID source number to a globally unique URI.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientPidMapValue {
    /// The small integer that PID parameters refer to after their dot.
    pub source_id: u32,
    /// The URI that identifies the product which created the vCard.
    pub uri:       Uri,
}

impl Display for ClientPidMapValue {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{};{}", self.source_id, self.uri)
    }
}

impl FromStr for ClientPidMapValue {
    type Err = InvalidValueError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        const ERROR: InvalidValueError = InvalidValueError::new("client pid map");

        // The URI part may also contain semicolons, so only the first one splits.
        let (source_id, uri) = s.split_once(';').ok_or(ERROR)?;

        Ok(Self {
            source_id: source_id.parse().map_err(|_| ERROR)?,
            uri:       Uri::from_str(uri).map_err(|_| ERROR)?,
        })
    }
}

/// A validator for email addresses, following the common rules of RFC 5321.
#[derive(Validator)]
#[validator(email(
    comment(Disallow),
    ip(Allow),
    local(Allow),
    at_least_two_labels(Allow),
    non_ascii(Allow)
))]
#[allow(dead_code)]
struct EmailValidator {
    local_part:  String,
    need_quoted: bool,
    domain_part: validators::models::Host,
}

/// The value of the EMAIL property.
///
/// RFC 6350 defines it as free-form text, but this type validates it as an email address for correctness.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EmailValue(String);

impl EmailValue {
    /// Returns the email address as a string slice.
    #[inline]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl FromStr for EmailValue {
    type Err = InvalidValueError;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        EmailValidator::validate_str(s).map_err(|_| InvalidValueError::new("email address"))?;

        Ok(Self(s.to_string()))
    }
}

impl Display for EmailValue {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str(&self.0)
    }
}

/// A validator for phone numbers in the international format, e.g. `+886912345678`.
#[derive(Validator)]
#[validator(phone)]
#[allow(dead_code)]
struct PhoneValidator(validators::phonenumber::PhoneNumber);

/// The value of the TEL property, which is a `tel:` URI or free-form text.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TelValue {
    /// A URI value, usually using the `tel:` scheme, written with `VALUE=uri`.
    Uri(Uri),
    /// A free-form text value, which is the default form.
    Text(String),
}

impl TelValue {
    /// Creates a text telephone value after validating the input as a phone number in the international format.
    #[inline]
    pub fn from_phone_number_str(s: &str) -> Result<Self, InvalidValueError> {
        PhoneValidator::validate_str(s).map_err(|_| InvalidValueError::new("phone number"))?;

        Ok(Self::Text(s.to_string()))
    }
}

/// A validator for UUID strings like `550e8400-e29b-41d4-a716-446655440000`.
#[derive(Validator)]
#[validator(uuid(case(Any), separator(Allow(b'-'))))]
#[allow(dead_code)]
struct UuidValidator(u128);

/// A value that is a URI or free-form text, used by the RELATED, UID, KEY and SOCIALPROFILE properties.
///
/// The URI form is the default, and the text form is written with `VALUE=text`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TextOrUri {
    /// A URI value, which is the default form.
    Uri(Uri),
    /// A free-form text value, written with `VALUE=text`.
    Text(String),
}

impl TextOrUri {
    /// Creates a `urn:uuid:` URI value from a UUID string, which is useful for the UID property.
    pub fn from_uuid_str(s: &str) -> Result<Self, InvalidValueError> {
        let uuid = UuidValidator::parse_str(s).map_err(|_| InvalidValueError::new("uuid"))?.0;

        let uri = format!(
            "urn:uuid:{:08x}-{:04x}-{:04x}-{:04x}-{:012x}",
            (uuid >> 96) as u32,
            (uuid >> 80) as u16,
            (uuid >> 64) as u16,
            (uuid >> 48) as u16,
            (uuid & 0xFFFF_FFFF_FFFF) as u64
        );

        // A URN built this way is always a valid URI, so parsing never fails.
        Ok(Self::Uri(Uri::from_str(&uri).unwrap()))
    }
}

/// The value of the TZ property, which is a time zone name, a URI, or a UTC offset.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TzValue {
    /// A text value, which is the default form and should be a time zone name from the IANA time zone database.
    Text(String),
    /// A URI value, written with `VALUE=uri`.
    Uri(Uri),
    /// A UTC offset value, written with `VALUE=utc-offset`.
    UtcOffset(UtcOffset),
}

impl TzValue {
    /// Creates a text time zone value from an IANA time zone provided by the `chrono-tz` crate.
    ///
    /// RFC 6350 suggests that the text form should be a name from the IANA time zone database, and this constructor guarantees that.
    #[inline]
    pub fn from_time_zone(tz: chrono_tz::Tz) -> Self {
        Self::Text(tz.name().to_string())
    }
}

/// The value of the BDAY and ANNIVERSARY properties, which is a date-and-or-time or free-form text.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DateAndOrTimeOrText {
    /// A date, a time, or a date with a time, which is the default form.
    DateAndOrTime(DateAndOrTime),
    /// A free-form text value like `circa 1800`, written with `VALUE=text`.
    Text(String),
}

impl From<DateAndOrTime> for DateAndOrTimeOrText {
    #[inline]
    fn from(value: DateAndOrTime) -> Self {
        Self::DateAndOrTime(value)
    }
}

impl From<Date> for DateAndOrTimeOrText {
    #[inline]
    fn from(value: Date) -> Self {
        Self::DateAndOrTime(DateAndOrTime::Date(value))
    }
}