Skip to main content

umya_spreadsheet/structs/
color_scale.rs

1use std::io::Cursor;
2
3use quick_xml::{
4    Reader,
5    Writer,
6    events::{
7        BytesStart,
8        Event,
9    },
10};
11
12use super::{
13    Color,
14    ConditionalFormatValueObject,
15};
16use crate::{
17    reader::driver::xml_read_loop,
18    writer::driver::{
19        write_end_tag,
20        write_start_tag,
21    },
22};
23
24#[derive(Clone, Default, Debug)]
25pub struct ColorScale {
26    cfvo_collection:  Vec<ConditionalFormatValueObject>,
27    color_collection: Vec<Color>,
28}
29
30impl ColorScale {
31    #[inline]
32    #[must_use]
33    pub fn cfvo_collection(&self) -> &[ConditionalFormatValueObject] {
34        &self.cfvo_collection
35    }
36
37    #[inline]
38    #[must_use]
39    #[deprecated(since = "3.0.0", note = "Use cfvo_collection()")]
40    pub fn get_cfvo_collection(&self) -> &[ConditionalFormatValueObject] {
41        self.cfvo_collection()
42    }
43
44    #[inline]
45    pub fn set_cfvo_collection(&mut self, value: Vec<ConditionalFormatValueObject>) -> &mut Self {
46        self.cfvo_collection = value;
47        self
48    }
49
50    #[inline]
51    pub fn add_cfvo_collection(&mut self, value: ConditionalFormatValueObject) -> &mut Self {
52        self.cfvo_collection.push(value);
53        self
54    }
55
56    #[inline]
57    #[must_use]
58    pub fn color_collection(&self) -> &[Color] {
59        &self.color_collection
60    }
61
62    #[inline]
63    #[must_use]
64    #[deprecated(since = "3.0.0", note = "Use color_collection()")]
65    pub fn get_color_collection(&self) -> &[Color] {
66        self.color_collection()
67    }
68
69    #[inline]
70    pub fn set_color_collection(&mut self, value: impl Into<Vec<Color>>) -> &mut Self {
71        self.color_collection = value.into();
72        self
73    }
74
75    #[inline]
76    pub fn add_color_collection(&mut self, value: Color) -> &mut Self {
77        self.color_collection.push(value);
78        self
79    }
80
81    pub(crate) fn set_attributes<R: std::io::BufRead>(
82        &mut self,
83        reader: &mut Reader<R>,
84        _e: &BytesStart,
85    ) {
86        xml_read_loop!(
87            reader,
88            ref n @ (Event::Empty(ref e) | Event::Start(ref e)) => {
89                let is_empty = matches!(n, Event::Empty(_));
90                match e.name().into_inner() {
91                    b"cfvo" => {
92                        let mut obj = ConditionalFormatValueObject::default();
93                        obj.set_attributes(reader, e, is_empty);
94                        self.cfvo_collection.push(obj);
95                    }
96                    b"color" => {
97                        let mut obj = Color::default();
98                        obj.set_attributes(reader, e, is_empty);
99                        self.color_collection.push(obj);
100                    }
101                    _ => (),
102                }
103            },
104            Event::End(ref e) => {
105                if e.name().into_inner() == b"colorScale" {
106                    return
107                }
108            },
109            Event::Eof => panic!("Error: Could not find {} end element", "colorScale")
110        );
111    }
112
113    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
114        // colorScale
115        write_start_tag(writer, "colorScale", vec![], false);
116
117        // cfvo
118        for v in &self.cfvo_collection {
119            v.write_to(writer);
120        }
121
122        // color
123        for v in &self.color_collection {
124            v.write_to_color(writer);
125        }
126
127        write_end_tag(writer, "colorScale");
128    }
129}