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