Skip to content

Commit

Permalink
Telephone types and Email adding implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Jun 7, 2022
1 parent 7c7b547 commit d8b8855
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<String, String> = 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(
Expand All @@ -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<VCard> = vec![
VCard::Element("vcard".to_string()),
Expand All @@ -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());
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ pub struct Location {
pub postal_code: Option<String>,
pub country: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Telephone {
Fax,
Voice,
}

0 comments on commit d8b8855

Please sign in to comment.