Crate vcard_parser

source ·
Expand description

vCard Parser

Parses and validates vCard data according to RFC 6350 specification.

Creating vCards

use vcard_parser::vcard::property::Property;
use vcard_parser::vcard::Vcard;

let mut vcard = Vcard::new("John Doe");

// Add a nickname property.
let mut property = Property::try_from("NICKNAME:Johnny\n").expect("Unable to parse property.");
vcard.set_property(&property).expect("Unable to add property.");

// Print vCard with pids and clientpidmap info.
print!("{}", vcard);

// Export vCard without any ids.
print!("{}", vcard.export());

Parsing vCards

vCards can be parsed from a string containing multiple vCards using the main parse_vcards() or parse_vcards_with_client() functions, or from a string containing one vCard using Vcard::try_from.

use vcard_parser::parse_vcards;
use vcard_parser::vcard::Vcard;

let mut vcards = parse_vcards("BEGIN:VCARD\nVERSION:4.0\nFN:John Doe\nEND:VCARD\n").expect("Unable to parse string.");
assert_eq!(vcards.len(), 1);

let mut vcard = Vcard::try_from("BEGIN:VCARD\nVERSION:4.0\nFN:John Doe\nEND:VCARD\n").expect("Unable to parse string.");
assert_eq!(vcard.get_properties().len(), 1)

Parsing from file

Read a vcf file and ignore invalid properties, update the vCard object, and write back to file.

use std::fs::{read_to_string, write};
use vcard_parser::parse_vcards;
use vcard_parser::traits::HasValue;
use vcard_parser::vcard::value::Value;
use vcard_parser::vcard::value::value_text::ValueTextData;

let input = read_to_string("contacts.vcf").unwrap_or(String::from("BEGIN:VCARD\nVERSION:4.0\nFN:\nEND:VCARD\n"));
let mut vcards = parse_vcards(input.as_str()).expect("Unable to parse string.");

let vcard = vcards.first_mut().unwrap();
let mut property = vcard.get_property_by_name("FN").unwrap();

property.set_value(Value::from(ValueTextData::from("John Doe"))).unwrap();
vcard.set_property(&property).expect("Unable to update property.");

let mut data = String::new();
for vcard in vcards {
    data.push_str(vcard.export().as_str())
}

// write("contacts.vcf", data).expect("Unable to write file.");

Modules

  • Constants for string matching.
  • Error types and handling.
  • Parsing module that relies on nom for heavy lifting.
  • Utility traits.
  • The vcard module represents data that has been parsed as per the RFC 6350 vCard specification.

Functions