umya_spreadsheet/structs/
fills.rs

1// fills
2use super::Fill;
3use super::Style;
4use crate::reader::driver::*;
5use crate::writer::driver::*;
6use quick_xml::events::{BytesStart, Event};
7use quick_xml::Reader;
8use quick_xml::Writer;
9use std::io::Cursor;
10use thin_vec::ThinVec;
11
12#[derive(Clone, Default, Debug)]
13pub(crate) struct Fills {
14    fill: ThinVec<Fill>,
15}
16
17impl Fills {
18    #[inline]
19    pub(crate) fn get_fill(&self) -> &[Fill] {
20        &self.fill
21    }
22
23    #[inline]
24    pub(crate) fn get_fill_mut(&mut self) -> &mut ThinVec<Fill> {
25        &mut self.fill
26    }
27
28    #[inline]
29    pub(crate) fn set_fill(&mut self, value: Fill) -> &mut Self {
30        self.fill.push(value);
31        self
32    }
33
34    pub(crate) fn set_style(&mut self, style: &Style) -> u32 {
35        match style.get_fill() {
36            Some(v) => {
37                let hash_code = v.get_hash_code();
38                let mut id = 0;
39                for fill in &self.fill {
40                    if fill.get_hash_code() == hash_code {
41                        return id;
42                    }
43                    id += 1;
44                }
45                self.set_fill(v.clone());
46                id
47            }
48            None => 0,
49        }
50    }
51
52    pub(crate) fn set_attributes<R: std::io::BufRead>(
53        &mut self,
54        reader: &mut Reader<R>,
55        _e: &BytesStart,
56    ) {
57        xml_read_loop!(
58            reader,
59            Event::Start(ref e) => {
60                if e.name().into_inner() == b"fill" {
61                    let mut obj = Fill::default();
62                    obj.set_attributes(reader, e);
63                    self.set_fill(obj);
64                }
65            },
66            Event::End(ref e) => {
67                if e.name().into_inner() == b"fills" {
68                    return
69                }
70            },
71            Event::Eof => panic!("Error: Could not find {} end element", "fills")
72        );
73    }
74
75    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
76        if !self.fill.is_empty() {
77            // fills
78            write_start_tag(
79                writer,
80                "fills",
81                vec![("count", &self.fill.len().to_string())],
82                false,
83            );
84
85            // fill
86            for fill in &self.fill {
87                fill.write_to(writer);
88            }
89
90            write_end_tag(writer, "fills");
91        }
92    }
93}