summaryrefslogtreecommitdiff
path: root/src/viewmodel/name.rs
blob: e0c3a5b87bec7eed69676a32f806372d8cc9ee18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use vcard::properties;
use vcard::values::{self, text};
use super::*;
use crate::view::name::*;

#[derive(Clone)]
pub struct Name {
    pub prefix: String,
    pub first_name: String,
    pub middle_name: String,
    pub last_name: String,
    pub suffix: String,
}

impl VCardPropertyInputObject<properties::Name, NameView> for Name {
    fn new() -> Self {
        Self {
            prefix: String::new(),
            first_name: String::new(),
            middle_name: String::new(),
            last_name: String::new(),
            suffix: String::new(),
        }
    }
    fn get_input_fields(&self, link: &ComponentLink<NameView>) -> std::vec::Vec<VCardPropertyInputField> {
        vec![
            VCardPropertyInputField::Text{
                label: "Prefix".to_string(),
                id: Some("prefix".to_string()),
                placeholder: Some("Sir".to_string()),
                oninput: link.callback(|e: InputData| Msg::UpdatePrefix(e.value)),
                value: self.prefix.clone(),
            },
            VCardPropertyInputField::Text{
                label: "First Name".to_string(),
                id: Some("first_name".to_string()),
                placeholder: Some("Arthur".to_string()),
                oninput: link.callback(|e: InputData| Msg::UpdateFirstName(e.value)),
                value: self.first_name.clone(),
            },
            VCardPropertyInputField::Text{
                label: "Middle Name".to_string(),
                id: Some("middle_name".to_string()),
                placeholder: Some("Charles".to_string()),
                oninput: link.callback(|e: InputData| Msg::UpdateMiddleName(e.value)),
                value: self.middle_name.clone(),
            },
            VCardPropertyInputField::Text{
                label: "Last Name".to_string(),
                id: Some("last_name".to_string()),
                placeholder: Some("Clarke".to_string()),
                oninput: link.callback(|e: InputData| Msg::UpdateLastName(e.value)),
                value: self.last_name.clone(),
            },
            VCardPropertyInputField::Text{
                label: "Suffix".to_string(),
                id: Some("suffix".to_string()),
                placeholder: Some("CBE FRAS".to_string()),
                oninput: link.callback(|e: InputData| Msg::UpdateSuffix(e.value)),
                value: self.suffix.clone(),
            },
        ]
    }
    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(
                    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<properties::FormattedName, Error> {
        let mut formatted_name = String::new();

        if !self.prefix.is_empty() {
            formatted_name.push_str(&self.prefix);
        }
        if !self.first_name.is_empty() {
            formatted_name.push_str(" ");
            formatted_name.push_str(&self.first_name);
        }
        if !self.middle_name.is_empty() {
            formatted_name.push_str(" ");
            formatted_name.push_str(&self.middle_name);
        }
        if !self.last_name.is_empty() {
            formatted_name.push_str(" ");
            formatted_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."),
            });
        }

        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)
    }
}