From 3f0892368bcf475fdae39f24ef51b82b3013535f Mon Sep 17 00:00:00 2001 From: jelemux Date: Thu, 28 Jan 2021 17:41:49 +0100 Subject: try to fix problem with vcard mutable references --- src/viewmodel/name.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 8 deletions(-) (limited to 'src/viewmodel/name.rs') diff --git a/src/viewmodel/name.rs b/src/viewmodel/name.rs index 55c1b4c..e0c3a5b 100644 --- a/src/viewmodel/name.rs +++ b/src/viewmodel/name.rs @@ -61,27 +61,69 @@ impl VCardPropertyInputObject for Name { }, ] } - fn to_vcard_property(&self) -> std::result::Result { + fn is_empty(&self) -> bool { + self.prefix.is_empty() && + self.first_name.is_empty() && + self.middle_name.is_empty() && + 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(text::Component::from_str(&self.last_name).unwrap()), + 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(text::Component::from_str(&self.first_name).unwrap()), + 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(text::Component::from_str(&self.middle_name).unwrap()), + 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(text::Component::from_str(&self.prefix).unwrap()), + 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(text::Component::from_str(&self.suffix).unwrap()), + false => Some( + match text::Component::from_str(&self.suffix) { + Ok(suffix) => suffix, + Err(_) => return Err(Error{ + msg: String::from("Illegal character in suffix."), + }), + } + ), }, ); @@ -90,7 +132,7 @@ impl VCardPropertyInputObject for Name { } impl Name { - pub fn formatted_name(&self) -> String { + pub fn formatted_name(&self) -> Result { let mut formatted_name = String::new(); if !self.prefix.is_empty() { @@ -113,6 +155,20 @@ impl Name { formatted_name.push_str(&self.suffix); } - formatted_name + 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."), + }); + } + + 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) } } \ No newline at end of file -- cgit v1.2.3