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