vcard_parser/vcard/value/
value_listcomponent.rs1use std::fmt::{Display, Formatter};
2
3use crate::parse::encoding::escape;
4use crate::vcard::value::value_textlist::ValueTextListData;
5use crate::VcardError;
6
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct ValueListComponentData {
9 pub delimiter_child: char,
10 pub delimiter_parent: char,
11 pub value: Vec<Vec<String>>,
12}
13
14impl Default for ValueListComponentData {
15 fn default() -> Self {
16 Self {
17 delimiter_child: ',',
18 delimiter_parent: ';',
19 value: Vec::new(),
20 }
21 }
22}
23
24impl TryFrom<(&str, char, char)> for ValueListComponentData {
25 type Error = VcardError;
26 fn try_from((str, delimiter_parent, delimiter_child): (&str, char, char)) -> Result<Self, Self::Error> {
27 let mut value = Vec::new();
28
29 for string in ValueTextListData::from((str, delimiter_parent)).value {
30 value.push(ValueTextListData::from((string.as_str(), delimiter_child)).value);
31 }
32
33 Ok(ValueListComponentData {
34 delimiter_child,
35 delimiter_parent,
36 value,
37 })
38 }
39}
40
41impl Display for ValueListComponentData {
42 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
43 write!(f, "{}", self.value.iter().map(|child| { child.iter().map(|s| { escape(s) }).collect::<Vec<String>>().join(self.delimiter_child.to_string().as_str()) }).collect::<Vec<String>>().join(self.delimiter_parent.to_string().as_str()))
44 }
45}