umya_spreadsheet/structs/
cache_source.rs

1// cacheSource
2use crate::structs::EnumValue;
3use crate::structs::SourceValues;
4use crate::structs::WorksheetSource;
5
6use crate::helper::const_str::*;
7use crate::reader::driver::*;
8use crate::writer::driver::*;
9use quick_xml::events::{BytesStart, Event};
10use quick_xml::Reader;
11use quick_xml::Writer;
12use std::io::Cursor;
13
14#[derive(Clone, Default, Debug)]
15pub struct CacheSource {
16    r#type: EnumValue<SourceValues>,
17    worksheet_source: Option<WorksheetSource>,
18}
19
20impl CacheSource {
21    pub fn get_type(&self) -> &SourceValues {
22        self.r#type.get_value()
23    }
24
25    pub fn set_type(&mut self, value: SourceValues) -> &mut Self {
26        self.r#type.set_value(value);
27        self
28    }
29
30    pub fn get_worksheet_source(&self) -> Option<&WorksheetSource> {
31        self.worksheet_source.as_ref()
32    }
33
34    pub fn get_worksheet_source_mut(&mut self) -> Option<&mut WorksheetSource> {
35        self.worksheet_source.as_mut()
36    }
37
38    pub fn set_worksheet_source_mut(&mut self, value: WorksheetSource) -> &mut Self {
39        self.worksheet_source = Some(value);
40        self
41    }
42
43    pub(crate) fn set_attributes<R: std::io::BufRead>(
44        &mut self,
45        reader: &mut Reader<R>,
46        e: &BytesStart,
47        empty_flg: bool,
48    ) {
49        set_string_from_xml!(self, e, r#type, "type");
50
51        if empty_flg {
52            return;
53        }
54
55        xml_read_loop!(
56            reader,
57            Event::Empty(ref e) => {
58                if e.name().into_inner() == b"worksheetSource" {
59                    let mut obj = WorksheetSource::default();
60                    obj.set_attributes(reader, e);
61                    self.set_worksheet_source_mut(obj);
62                }
63            },
64            Event::End(ref e) => {
65                if e.name().into_inner() == b"cacheSource" {
66                    return
67                }
68            },
69            Event::Eof => panic!("Error: Could not find {} end element", "cacheSource")
70        );
71    }
72
73    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
74        // cacheSource
75        let empty_flg = self.worksheet_source.is_none();
76        let mut attributes: Vec<(&str, &str)> = Vec::new();
77        attributes.push(("type", self.r#type.get_hash_string()));
78        write_start_tag(writer, "cacheSource", attributes, empty_flg);
79
80        if !empty_flg {
81            // worksheetSource
82            match &self.worksheet_source {
83                Some(v) => v.write_to(writer),
84                None => {}
85            }
86
87            write_end_tag(writer, "cacheSource");
88        }
89    }
90}