Skip to main content

vcard_parser/vcard/parameter/
parameter_xname.rs

1use crate::vcard::value::value_text::ValueTextData;
2use crate::vcard::value::Value;
3use crate::vcard::value::Value::ValueText;
4use crate::{HasName, HasValue, VcardError};
5
6#[derive(Clone, Debug, PartialEq)]
7pub struct XNameParameterData {
8    pub name: String,
9    pub value: Value,
10}
11
12impl XNameParameterData {
13    pub fn default(name: &str) -> Self {
14        Self {
15            name: name.to_string(),
16            value: Value::from(ValueTextData::default()),
17        }
18    }
19}
20
21impl HasName for XNameParameterData {
22    fn name(&self) -> &str {
23        &self.name
24    }
25}
26
27impl HasValue for XNameParameterData {
28    fn get_value(&self) -> &Value {
29        &self.value
30    }
31
32    fn set_value(&mut self, value: Value) -> Result<(), VcardError> {
33        if !matches!(value, ValueText(_)) {
34            return Err(VcardError::ValueNotAllowed(value.to_string(), self.name().to_string()));
35        }
36
37        self.value = value;
38
39        Ok(())
40    }
41}
42
43impl TryFrom<(&str, &str)> for XNameParameterData {
44    type Error = VcardError;
45    fn try_from((name, value): (&str, &str)) -> Result<Self, Self::Error> {
46        Ok(Self {
47            name: name.to_string(),
48            value: ValueText(ValueTextData::from(value)),
49        })
50    }
51}