vcard_parser/vcard/property/
property_kind.rs

1use crate::constants::{Cardinality, ParameterName, PropertyKindValues, PropertyName, ValueType};
2use crate::traits::{HasCardinality, HasGroup, HasName, HasParameters, HasValue};
3use crate::vcard::parameter::Parameter;
4use crate::vcard::value::value_text::ValueTextData;
5use crate::vcard::value::Value;
6use crate::vcard::value::Value::ValueText;
7use crate::VcardError;
8
9#[derive(Clone, Debug, PartialEq)]
10pub struct PropertyKindData {
11    group: Option<String>,
12    parameters: Vec<Parameter>,
13    value: Value,
14}
15
16impl HasCardinality for PropertyKindData {
17    fn cardinality(&self) -> &str {
18        Cardinality::SINGLE
19    }
20}
21
22impl HasGroup for PropertyKindData {
23    fn group(&self) -> &Option<String> {
24        &self.group
25    }
26}
27
28impl HasName for PropertyKindData {
29    fn name(&self) -> &str {
30        PropertyName::KIND
31    }
32}
33
34impl HasParameters for PropertyKindData {
35    fn allowed_parameters<'a>(&self) -> Vec<&'a str> {
36        Vec::from([
37            ParameterName::ANY,
38            ParameterName::VALUE,
39        ])
40    }
41
42    fn get_parameters(&self) -> Vec<Parameter> {
43        self.parameters.clone()
44    }
45
46    fn set_parameters(&mut self, parameters: Vec<Parameter>) {
47        self.parameters = parameters;
48    }
49}
50
51impl HasValue for PropertyKindData {
52    fn get_value(&self) -> &Value {
53        &self.value
54    }
55
56    fn set_value(&mut self, value: Value) -> Result<(), VcardError> {
57        if !matches!(value, ValueText(_)) {
58            return Err(VcardError::ValueNotAllowed(value.to_string(), self.name().to_string()));
59        }
60
61        if let Some(value_type) = self.has_value_type() {
62            if matches!(value, ValueText(_)) && value_type != ValueType::TEXT {
63                return Err(VcardError::ValueMismatch(value.to_string(), value_type, self.name().to_string()));
64            }
65        }
66
67        if let ValueText(data) = &value {
68            if !PropertyKindValues::TYPES.contains(&data.value.to_uppercase().as_str()) {
69                return Err(VcardError::ValueInvalid(data.value.to_string(), self.name().to_string()));
70            }
71        }
72
73        self.value = value;
74
75        Ok(())
76    }
77}
78
79impl Default for PropertyKindData {
80    fn default() -> Self {
81        Self {
82            group: None,
83            parameters: Vec::new(),
84            value: ValueText(ValueTextData::from("individual")),
85        }
86    }
87}
88
89impl TryFrom<(Option<String>, &str, Vec<Parameter>)> for PropertyKindData {
90    type Error = VcardError;
91    fn try_from((group, value, parameters): (Option<String>, &str, Vec<Parameter>)) -> Result<Self, Self::Error> {
92        let mut property = Self { group, ..Self::default() };
93
94        property.add_parameters(parameters)?;
95        property.set_value(ValueText(ValueTextData::from(value)))?;
96
97        Ok(property)
98    }
99}