From 94a0e361e180223adf2bc9760c8810dfa3c2681f Mon Sep 17 00:00:00 2001 From: jelemux Date: Thu, 28 Jan 2021 22:24:32 +0100 Subject: use different vcard library --- src/viewmodel/address.rs | 57 +----------------------- src/viewmodel/mod.rs | 5 +-- src/viewmodel/name.rs | 105 ++++++--------------------------------------- src/viewmodel/telephone.rs | 56 +----------------------- 4 files changed, 17 insertions(+), 206 deletions(-) (limited to 'src/viewmodel') diff --git a/src/viewmodel/address.rs b/src/viewmodel/address.rs index b2257bf..61bd464 100644 --- a/src/viewmodel/address.rs +++ b/src/viewmodel/address.rs @@ -1,7 +1,3 @@ -use vcard::properties; -use vcard::parameters; -use vcard::values::{self, text}; -use std::collections::HashSet; use super::*; use crate::view::address::*; @@ -18,7 +14,7 @@ pub struct Address { pub home: bool, } -impl VCardPropertyInputObject for Address { +impl VCardPropertyInputObject for Address { fn new() -> Self { Self { post_office_box: String::new(), @@ -106,55 +102,4 @@ impl VCardPropertyInputObject for Address { self.code.is_empty() && self.country.is_empty() } - fn to_vcard_property(&self) -> Result { // TODO error handling - let address_value = values::address_value::AddressValue::from_components( - match self.post_office_box.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.post_office_box).unwrap()), - }, - match self.extension.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.extension).unwrap()), - }, - match self.street.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.street).unwrap()), - }, - match self.locality.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.locality).unwrap()), - }, - match self.region.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.region).unwrap()), - }, - match self.code.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.code).unwrap()), - }, - match self.country.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.country).unwrap()), - }, - ); - - let mut address = properties::Address::from_address_value(address_value); - - let type_values = { - let mut type_values = HashSet::new(); - - if self.work { - type_values.insert(values::type_value::TypeValue::Work); - } - if self.home { - type_values.insert(values::type_value::TypeValue::Home); - } - - vcard::Set::from_hash_set(type_values).unwrap() - }; - - address.typ = Some(parameters::typ::Type::from_type_values(type_values)); - - Ok(address) - } } \ No newline at end of file diff --git a/src/viewmodel/mod.rs b/src/viewmodel/mod.rs index 9dce01c..cc25d68 100644 --- a/src/viewmodel/mod.rs +++ b/src/viewmodel/mod.rs @@ -1,5 +1,5 @@ -use vcard::properties; use yew::prelude::*; +use vobject::Property; use crate::view::VCardPropertyInputComponent; pub mod address; @@ -7,7 +7,7 @@ pub mod name; pub mod telephone; pub mod utility; -pub trait VCardPropertyInputObject> +pub trait VCardPropertyInputObject> where Self: Sized { fn new() -> Self; @@ -24,7 +24,6 @@ pub trait VCardPropertyInputObject bool; - fn to_vcard_property(&self) -> Result; } #[derive(Debug,Clone,PartialEq)] diff --git a/src/viewmodel/name.rs b/src/viewmodel/name.rs index e0c3a5b..26beaa1 100644 --- a/src/viewmodel/name.rs +++ b/src/viewmodel/name.rs @@ -1,5 +1,3 @@ -use vcard::properties; -use vcard::values::{self, text}; use super::*; use crate::view::name::*; @@ -12,7 +10,7 @@ pub struct Name { pub suffix: String, } -impl VCardPropertyInputObject for Name { +impl VCardPropertyInputObject for Name { fn new() -> Self { Self { prefix: String::new(), @@ -68,107 +66,30 @@ impl VCardPropertyInputObject for Name { self.last_name.is_empty() && self.suffix.is_empty() } - fn to_vcard_property(&self) -> std::result::Result { - let name_value = values::name_value::NameValue::from_components( - match self.last_name.is_empty() { - true => None, - false => Some( - match text::Component::from_str(&self.last_name) { - Ok(last_name) => last_name, - Err(_) => return Err(Error{ - msg: String::from("Illegal character in last name."), - }), - } - ), - }, - match self.first_name.is_empty() { - true => None, - false => Some( - match text::Component::from_str(&self.first_name) { - Ok(first_name) => first_name, - Err(_) => return Err(Error{ - msg: String::from("Illegal character in first name."), - }), - } - ), - }, - match self.middle_name.is_empty() { - true => None, - false => Some( - match text::Component::from_str(&self.middle_name) { - Ok(middle_name) => middle_name, - Err(_) => return Err(Error{ - msg: String::from("Illegal character in middle name."), - }), - } - ), - }, - match self.prefix.is_empty() { - true => None, - false => Some( - match text::Component::from_str(&self.prefix) { - Ok(prefix) => prefix, - Err(_) => return Err(Error{ - msg: String::from("Illegal character in prefix."), - }), - } - ), - }, - match self.suffix.is_empty() { - true => None, - false => Some( - match text::Component::from_str(&self.suffix) { - Ok(suffix) => suffix, - Err(_) => return Err(Error{ - msg: String::from("Illegal character in suffix."), - }), - } - ), - }, - ); - - Ok(properties::Name::from_name_value(name_value)) - } } impl Name { - pub fn formatted_name(&self) -> Result { - let mut formatted_name = String::new(); + pub fn generate_fn(&self) -> String { + let full_name = String::new(); - if !self.prefix.is_empty() { - formatted_name.push_str(&self.prefix); - } + full_name.push_str(&self.prefix); if !self.first_name.is_empty() { - formatted_name.push_str(" "); - formatted_name.push_str(&self.first_name); + full_name.push_str(" "); + full_name.push_str(&self.first_name); } if !self.middle_name.is_empty() { - formatted_name.push_str(" "); - formatted_name.push_str(&self.middle_name); + full_name.push_str(" "); + full_name.push_str(&self.middle_name); } if !self.last_name.is_empty() { - formatted_name.push_str(" "); - formatted_name.push_str(&self.last_name); + full_name.push_str(" "); + full_name.push_str(&self.last_name); } if !self.suffix.is_empty() { - formatted_name.push_str(", "); - formatted_name.push_str(&self.suffix); - } - - if formatted_name.is_empty() { - return Err(Error{ - msg: String::from("Primary name field must not be empty for the formatted name field to be generated."), - }); + full_name.push_str(" "); + full_name.push_str(&self.suffix); } - let formatted_name = properties::FormattedName::from_text( - match text::Text::from_string(formatted_name) { - Ok(formatted_name) => formatted_name, - Err(_) => return Err(Error{ - msg: String::from("Illegal character in formatted name.") // If I see this right, this error should never occur. - }), - } - ); - Ok(formatted_name) + full_name } } \ No newline at end of file diff --git a/src/viewmodel/telephone.rs b/src/viewmodel/telephone.rs index f90df5d..e5f63f0 100644 --- a/src/viewmodel/telephone.rs +++ b/src/viewmodel/telephone.rs @@ -1,6 +1,3 @@ -use vcard::properties; -use vcard::parameters; -use vcard::values; use std::collections::HashSet; use super::*; use crate::view::telephone::*; @@ -20,7 +17,7 @@ pub struct Telephone { pub text_phone: bool, } -impl VCardPropertyInputObject for Telephone { +impl VCardPropertyInputObject for Telephone { fn new() -> Self { Self { number: String::new(), @@ -112,55 +109,4 @@ impl VCardPropertyInputObject for Telephon self.number.is_empty() && self.extension.is_empty() } - fn to_vcard_property(&self) -> Result { // TODO error handling - let mut telephone = properties::Telephone::from_telephone_value( - values::telephone_value::TelephoneValue::from_telephone_number_str( - self.number.clone(), - match self.extension.is_empty() { - true => None::<&str>, - false => Some(&self.extension), - }, - ).unwrap() - ); - - let type_values = { - let mut type_values = HashSet::new(); - - if self.work { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Work); - } - if self.home { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Home); - } - if self.text { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Text); - } - if self.voice { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Voice); - } - if self.fax { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Fax); - } - if self.cell { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Cell); - } - if self.video { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Video); - } - if self.pager { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Pager); - } - if self.text_phone { - type_values.insert(values::type_value::TypeValueWithTelephoneType::TextPhone); - } - - vcard::Set::from_hash_set(type_values).unwrap() - }; - - if let properties::Telephone::TelephoneValue { ref mut typ, .. } = telephone { - *typ = Some(parameters::typ::TypeWithTelType::from_type_values(type_values)); - } - - Ok(telephone) - } } \ No newline at end of file -- cgit v1.2.3