vcard/properties/
key.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, media_type::MediaType, preference::Preference,
9            property_id::PropertyID, Parameter,
10        },
11        values::{key_value::KeyValue, Value},
12        Set,
13    },
14    *,
15};
16
17#[derive(Clone, Debug, PartialEq, Eq, Hash)]
18pub struct Key {
19    pub typ:            Option<MediaType>,
20    pub property_id:    Option<PropertyID>,
21    pub preference:     Option<Preference>,
22    pub alternative_id: Option<AlternativeID>,
23    pub any:            Option<Set<Any>>,
24    pub value:          KeyValue,
25}
26
27impl Key {
28    pub fn from_key_value(key_value: KeyValue) -> Key {
29        Key {
30            typ: None,
31
32            property_id:    None,
33            preference:     None,
34            alternative_id: None,
35            any:            None,
36            value:          key_value,
37        }
38    }
39}
40
41impl Property for Key {
42    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
43        f.write_str("KEY")?;
44
45        macro_rules! fmt {
46            ($c:tt, $p:ident) => {
47                fmt_g!($c, Parameter, self, $p, f);
48            };
49        }
50
51        fmt!(0, typ);
52        fmt!(0, property_id);
53        fmt!(0, preference);
54        fmt!(0, alternative_id);
55        fmt!(2, any);
56
57        if let KeyValue::Text(_) = self.value {
58            f.write_str(";VALUE=text:")?;
59        } else {
60            f.write_char(':')?;
61        }
62
63        Value::fmt(&self.value, f)?;
64
65        f.write_str("\r\n")?;
66
67        Ok(())
68    }
69}
70
71impl Display for Key {
72    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
73        Property::fmt(self, f)
74    }
75}
76
77impl Validated for Key {}
78
79impl ValidatedWrapper for Key {
80    type Error = &'static str;
81
82    fn from_string(_from_string_input: String) -> Result<Self, Self::Error> {
83        unimplemented!();
84    }
85
86    fn from_str(_from_str_input: &str) -> Result<Self, Self::Error> {
87        unimplemented!();
88    }
89}