vcard/properties/
nickname.rs

1use std::fmt::{self, Display, Formatter, Write};
2
3use validators::{Validated, ValidatedWrapper};
4
5use super::{
6    super::{
7        parameters::{
8            alternative_id::AlternativeID, any::Any, language::Language, preference::Preference,
9            property_id::PropertyID, typ::Type, Parameter,
10        },
11        values::{text::Text, Value},
12        Set,
13    },
14    *,
15};
16
17#[derive(Clone, Debug, PartialEq, Eq, Hash)]
18pub struct NickName {
19    pub typ:            Option<Type>,
20    pub language:       Option<Language>,
21    pub property_id:    Option<PropertyID>,
22    pub preference:     Option<Preference>,
23    pub alternative_id: Option<AlternativeID>,
24    pub any:            Option<Set<Any>>,
25    pub value:          Set<Text>,
26}
27
28impl NickName {
29    pub fn from_text_list(text_list: Set<Text>) -> NickName {
30        NickName {
31            typ:      None,
32            language: None,
33
34            property_id:    None,
35            preference:     None,
36            alternative_id: None,
37            any:            None,
38            value:          text_list,
39        }
40    }
41
42    pub fn is_empty(&self) -> bool {
43        for e in self.value.as_hash_set() {
44            if !e.is_empty() {
45                return false;
46            }
47        }
48
49        true
50    }
51}
52
53impl Property for NickName {
54    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
55        if self.is_empty() {
56            return Ok(());
57        }
58
59        f.write_str("NICKNAME")?;
60
61        macro_rules! fmt {
62            ($c:tt, $p:ident) => {
63                fmt_g!($c, Parameter, self, $p, f);
64            };
65        }
66
67        fmt!(0, typ);
68        fmt!(0, language);
69        fmt!(0, property_id);
70        fmt!(0, preference);
71        fmt!(0, alternative_id);
72        fmt!(2, any);
73
74        f.write_char(':')?;
75
76        Value::fmt(&self.value, f)?;
77
78        f.write_str("\r\n")?;
79
80        Ok(())
81    }
82}
83
84impl Display for NickName {
85    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
86        Property::fmt(self, f)
87    }
88}
89
90impl Validated for NickName {}
91
92impl ValidatedWrapper for NickName {
93    type Error = &'static str;
94
95    fn from_string(_from_string_input: String) -> Result<Self, Self::Error> {
96        unimplemented!();
97    }
98
99    fn from_str(_from_str_input: &str) -> Result<Self, Self::Error> {
100        unimplemented!();
101    }
102}