summaryrefslogtreecommitdiff
path: root/src/viewmodel
diff options
context:
space:
mode:
authorjelemux <jeremias.weber@protonmail.com>2021-01-28 17:41:49 +0100
committerjelemux <jeremias.weber@protonmail.com>2021-01-28 17:41:49 +0100
commit3f0892368bcf475fdae39f24ef51b82b3013535f (patch)
tree087a0869e618cc37b48e6d462c99bc1b289ddabf /src/viewmodel
parent4a0c73eebb8dfd6a5543945049175f64b9817c96 (diff)
downloadwasm-card-3f0892368bcf475fdae39f24ef51b82b3013535f.tar.gz
wasm-card-3f0892368bcf475fdae39f24ef51b82b3013535f.tar.bz2
try to fix problem with vcard mutable references
Diffstat (limited to 'src/viewmodel')
-rw-r--r--src/viewmodel/address.rs11
-rw-r--r--src/viewmodel/mod.rs9
-rw-r--r--src/viewmodel/name.rs72
-rw-r--r--src/viewmodel/telephone.rs6
-rw-r--r--src/viewmodel/utility.rs4
5 files changed, 87 insertions, 15 deletions
diff --git a/src/viewmodel/address.rs b/src/viewmodel/address.rs
index 757236c..b2257bf 100644
--- a/src/viewmodel/address.rs
+++ b/src/viewmodel/address.rs
@@ -97,7 +97,16 @@ impl VCardPropertyInputObject<properties::Address, AddressView> for Address {
},
]
}
- fn to_vcard_property(&self) -> Result<properties::Address, VCardPropertyInputError> {
+ fn is_empty(&self) -> bool {
+ self.post_office_box.is_empty() &&
+ self.extension.is_empty() &&
+ self.street.is_empty() &&
+ self.locality.is_empty() &&
+ self.region.is_empty() &&
+ self.code.is_empty() &&
+ self.country.is_empty()
+ }
+ fn to_vcard_property(&self) -> Result<properties::Address, Error> { // TODO error handling
let address_value = values::address_value::AddressValue::from_components(
match self.post_office_box.is_empty() {
true => None,
diff --git a/src/viewmodel/mod.rs b/src/viewmodel/mod.rs
index 42ad77b..9dce01c 100644
--- a/src/viewmodel/mod.rs
+++ b/src/viewmodel/mod.rs
@@ -23,12 +23,13 @@ pub trait VCardPropertyInputObject<P: properties::Property, C: VCardPropertyInpu
</div>
}
}
- fn to_vcard_property(&self) -> Result<P, VCardPropertyInputError>;
+ fn is_empty(&self) -> bool;
+ fn to_vcard_property(&self) -> Result<P, Error>;
}
-#[derive(Debug)]
-pub struct VCardPropertyInputError {
- msg: String,
+#[derive(Debug,Clone,PartialEq)]
+pub struct Error {
+ pub msg: String,
}
pub enum VCardPropertyInputField {
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<properties::Name, NameView> for Name {
},
]
}
- fn to_vcard_property(&self) -> std::result::Result<properties::Name, VCardPropertyInputError> {
+ 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<properties::Name, Error> {
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<properties::Name, NameView> for Name {
}
impl Name {
- pub fn formatted_name(&self) -> String {
+ pub fn formatted_name(&self) -> Result<properties::FormattedName, Error> {
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
diff --git a/src/viewmodel/telephone.rs b/src/viewmodel/telephone.rs
index b67b0fb..f90df5d 100644
--- a/src/viewmodel/telephone.rs
+++ b/src/viewmodel/telephone.rs
@@ -108,7 +108,11 @@ impl VCardPropertyInputObject<properties::Telephone, TelephoneView> for Telephon
},
]
}
- fn to_vcard_property(&self) -> Result<properties::Telephone, VCardPropertyInputError> {
+ fn is_empty(&self) -> bool {
+ self.number.is_empty() &&
+ self.extension.is_empty()
+ }
+ fn to_vcard_property(&self) -> Result<properties::Telephone, Error> { // TODO error handling
let mut telephone = properties::Telephone::from_telephone_value(
values::telephone_value::TelephoneValue::from_telephone_number_str(
self.number.clone(),
diff --git a/src/viewmodel/utility.rs b/src/viewmodel/utility.rs
index a296c1e..4a82a42 100644
--- a/src/viewmodel/utility.rs
+++ b/src/viewmodel/utility.rs
@@ -1,3 +1,5 @@
+
+
#[derive(Clone)]
pub struct Download {
pub file_name: String,
@@ -31,7 +33,7 @@ impl MimeType {
}
}
-#[derive(Clone, Copy)]
+#[derive(Clone, Copy, PartialEq)]
pub enum DownloadOption {
PDF,
VCard,