api(jsonrpc): Add parse_vcard() (#5202)

Add a function parsing a vCard file at the given path.

Co-authored-by: Hocuri <hocuri@gmx.de>
Co-authored-by: Asiel Díaz Benítez <asieldbenitez@gmail.com>
This commit is contained in:
iequidoo
2024-05-04 23:07:41 -03:00
committed by iequidoo
parent 4a738ebd19
commit 95238b6e17
7 changed files with 62 additions and 8 deletions

View File

@@ -44,7 +44,8 @@ use regex::Regex;
pub struct VcardContact {
/// The email address, vcard property `email`
pub addr: String,
/// The contact's display name, vcard property `fn`
/// The contact's display name, vcard property `fn`. Can be empty, one should use
/// `display_name()` to obtain the actual value.
pub display_name: String,
/// The contact's public PGP key in Base64, vcard property `key`
pub key: Option<String>,
@@ -54,6 +55,16 @@ pub struct VcardContact {
pub timestamp: Result<u64>,
}
impl VcardContact {
/// Returns the contact's display name.
pub fn display_name(&self) -> &str {
match self.display_name.is_empty() {
false => &self.display_name,
true => &self.addr,
}
}
}
/// Returns a vCard containing given contacts.
///
/// Calling [`parse_vcard()`] on the returned result is a reverse operation.
@@ -68,10 +79,7 @@ pub fn make_vcard(contacts: &[VcardContact]) -> String {
let mut res = "".to_string();
for c in contacts {
let addr = &c.addr;
let display_name = match c.display_name.is_empty() {
false => &c.display_name,
true => &c.addr,
};
let display_name = c.display_name();
res += &format!(
"BEGIN:VCARD\n\
VERSION:4.0\n\