From d8b8855a9711d03e9c25d5b565df8e41ca989cfe Mon Sep 17 00:00:00 2001 From: UwUssimo Robinson Date: Tue, 7 Jun 2022 15:46:37 +0500 Subject: [PATCH] Telephone types and Email adding implemented --- src/lib.rs | 34 ++++++++++++++++++++++++++-------- src/models.rs | 6 ++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c910630..856798c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use crate::models::{Location, VCard, VElement}; +use crate::models::{Location, Telephone, VCard, VElement}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -87,19 +87,16 @@ impl VCardArray { ) } - pub fn add_tel(&mut self, types: &str, number: String) { + pub fn add_tel(&mut self, types: Telephone, number: String) { let mut properties: HashMap = HashMap::new(); match types { - "v" => { + Telephone::Voice => { properties.insert("type".to_string(), "voice".to_string()); } - "f" => { + Telephone::Fax => { properties.insert("type".to_string(), "fax".to_string()); } - _ => { - properties.insert("type".to_string(), "undefined".to_string()); - } } self.add_vcard( @@ -110,6 +107,15 @@ impl VCardArray { ) } + pub fn add_email(&mut self, email: String) { + self.add_vcard( + "email".to_string(), + HashMap::new(), + "text".to_string(), + VElement::Element(email), + ); + } + pub fn to_json(&self, pretty: bool) -> String { let array: Vec = vec![ VCard::Element("vcard".to_string()), @@ -128,7 +134,7 @@ mod tests { use crate::{Location, VCardArray}; #[test] - fn sample_array() { + fn test_fn() { let mut vcard = VCardArray::new(); vcard.add_fn("John".to_string(), "Doe".to_string()); @@ -158,4 +164,16 @@ mod tests { .to_string(); assert_eq!(vcard.to_json(false), result); } + + #[test] + fn test_email() { + let mut vcard = VCardArray::new(); + + vcard.add_email("sample@mail.com".to_string()); + + let result = + "[\"vcard\",[[\"version\",{},\"text\",\"4.0\"],[\"email\",{},\"text\",\"sample@mail.com\"]]]" + .to_string(); + assert_eq!(vcard.to_json(false), result); + } } diff --git a/src/models.rs b/src/models.rs index 814033a..215f77f 100644 --- a/src/models.rs +++ b/src/models.rs @@ -31,3 +31,9 @@ pub struct Location { pub postal_code: Option, pub country: Option, } + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum Telephone { + Fax, + Voice, +}