umya_spreadsheet/structs/custom_properties/
properties.rs

1use crate::helper::const_str::*;
2use crate::reader::driver::*;
3use crate::structs::custom_properties::CustomDocumentProperty;
4use crate::writer::driver::*;
5use quick_xml::events::BytesStart;
6use quick_xml::events::Event;
7use quick_xml::Reader;
8use quick_xml::Writer;
9use std::borrow::Cow;
10use std::io::Cursor;
11use thin_vec::ThinVec;
12
13#[derive(Default, Debug, Clone)]
14pub struct Properties {
15    custom_document_property_list: ThinVec<CustomDocumentProperty>,
16}
17
18impl Properties {
19    #[inline]
20    pub fn get_custom_document_property_list(&self) -> &[CustomDocumentProperty] {
21        &self.custom_document_property_list
22    }
23
24    #[inline]
25    pub fn get_custom_document_property_list_mut(
26        &mut self,
27    ) -> &mut ThinVec<CustomDocumentProperty> {
28        &mut self.custom_document_property_list
29    }
30
31    #[inline]
32    pub fn set_custom_document_property_list(
33        &mut self,
34        value: impl Into<ThinVec<CustomDocumentProperty>>,
35    ) -> &mut Self {
36        self.custom_document_property_list = value.into();
37        self
38    }
39
40    #[inline]
41    pub fn add_custom_document_property_list(
42        &mut self,
43        value: CustomDocumentProperty,
44    ) -> &mut Self {
45        self.custom_document_property_list.push(value);
46        self
47    }
48
49    #[inline]
50    pub fn remove_custom_document_property_list(
51        &mut self,
52        value: CustomDocumentProperty,
53    ) -> &mut Self {
54        self.custom_document_property_list.clear();
55        self
56    }
57
58    pub(crate) fn set_attributes<R: std::io::BufRead>(
59        &mut self,
60        reader: &mut Reader<R>,
61        _e: &BytesStart,
62    ) {
63        let mut value: String = String::new();
64        xml_read_loop!(
65            reader,
66            Event::Empty(ref e) => {
67                if e.name().into_inner() == b"property" {
68                    let mut obj = CustomDocumentProperty::default();
69                    obj.set_attributes(reader, e, true);
70                    self.add_custom_document_property_list(obj);
71                }
72            },
73            Event::Start(ref e) => {
74                if e.name().into_inner() == b"property" {
75                    let mut obj = CustomDocumentProperty::default();
76                    obj.set_attributes(reader, e, false);
77                    self.add_custom_document_property_list(obj);
78                }
79            },
80            Event::End(ref e) => {
81                if e.name().into_inner() == b"Properties" {
82                    return
83                }
84            },
85            Event::Eof => panic!("Error: Could not find {} end element", "Properties")
86        );
87    }
88
89    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
90        write_start_tag(
91            writer,
92            "Properties",
93            vec![("xmlns", CUSTOM_PROPS_NS), ("xmlns:vt", VTYPES_NS)],
94            false,
95        );
96        let mut pid = 2;
97        for v in &self.custom_document_property_list {
98            v.write_to(writer, &pid);
99            pid += 1;
100        }
101        write_end_tag(writer, "Properties");
102    }
103}