1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use super::BooleanValue;
use super::Cell;
use super::Cells;
use super::DoubleValue;
use super::SharedStringTable;
use super::Style;
use super::Stylesheet;
use super::UInt32Value;
use quick_xml::events::{BytesStart, Event};
use quick_xml::Reader;
use quick_xml::Writer;
use reader::driver::*;
use std::io::Cursor;

use writer::driver::*;

#[derive(Clone, Default, Debug, PartialEq, PartialOrd)]
pub struct Row {
    row_num: UInt32Value,
    height: DoubleValue,
    descent: DoubleValue,
    thick_bot: BooleanValue,
    custom_height: BooleanValue,
    hidden: BooleanValue,
    style: Style,
}

impl Row {
    pub fn get_row_num(&self) -> &u32 {
        self.row_num.get_value()
    }

    pub(crate) fn set_row_num(&mut self, value: u32) -> &mut Self {
        self.row_num.set_value(value);
        self
    }

    pub fn get_height(&self) -> &f64 {
        self.height.get_value()
    }

    pub fn set_height(&mut self, value: f64) -> &mut Self {
        self.height.set_value(value);
        self.custom_height.set_value(true);
        self
    }

    pub fn get_descent(&self) -> &f64 {
        self.descent.get_value()
    }

    pub fn set_descent(&mut self, value: f64) -> &mut Self {
        self.descent.set_value(value);
        self
    }

    pub fn get_thick_bot(&self) -> &bool {
        self.thick_bot.get_value()
    }

    pub fn set_thick_bot(&mut self, value: bool) -> &mut Self {
        self.thick_bot.set_value(value);
        self
    }

    pub fn get_custom_height(&self) -> &bool {
        self.custom_height.get_value()
    }

    pub fn set_custom_height(&mut self, value: bool) -> &mut Self {
        self.custom_height.set_value(value);
        self
    }

    pub fn get_hidden(&self) -> &bool {
        self.hidden.get_value()
    }

    pub fn set_hidden(&mut self, value: bool) -> &mut Self {
        self.hidden.set_value(value);
        self
    }

    pub fn get_style(&self) -> &Style {
        &self.style
    }

    pub fn get_style_mut(&mut self) -> &mut Style {
        &mut self.style
    }

    pub fn set_style(&mut self, value: Style) -> &mut Self {
        self.style = value;
        self
    }

    pub(crate) fn adjustment_insert_coordinate(
        &mut self,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) {
        if self.row_num.get_value() >= root_row_num {
            self.row_num
                .set_value(self.row_num.get_value() + offset_row_num);
        }
    }

    pub(crate) fn adjustment_remove_coordinate(
        &mut self,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) {
        if self.row_num.get_value() >= root_row_num {
            self.row_num
                .set_value(self.row_num.get_value() - offset_row_num);
        }
    }

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        reader: &mut Reader<R>,
        e: &BytesStart,
        cells: &mut Cells,
        shared_string_table: &SharedStringTable,
        stylesheet: &Stylesheet,
        empty_flag: bool,
    ) {
        set_string_from_xml!(self, e, row_num, "r");
        set_string_from_xml!(self, e, height, "ht");
        set_string_from_xml!(self, e, thick_bot, "thickBot");
        set_string_from_xml!(self, e, custom_height, "customHeight");
        set_string_from_xml!(self, e, hidden, "hidden");

        if let Some(v) = get_attribute(e, b"x14ac:dyDescent") {
            if !v.is_empty() {
                self.descent.set_value_string(v);
            }
        }

        if let Some(v) = get_attribute(e, b"s") {
            let style = stylesheet.get_style(v.parse::<usize>().unwrap());
            self.set_style(style);
        }

        if empty_flag {
            return;
        }

        xml_read_loop!(
            reader,
            Event::Empty(ref e) => {
                if e.name().into_inner() == b"c" {
                    let mut obj = Cell::default();
                    obj.set_attributes(reader, e, shared_string_table, stylesheet, true);
                    cells.set_fast(obj);
                }
            },
            Event::Start(ref e) => {
                if e.name().into_inner() == b"c" {
                    let mut obj = Cell::default();
                    obj.set_attributes(reader, e, shared_string_table, stylesheet, false);
                    cells.set_fast(obj);
                }
            },
            Event::End(ref e) => {
                if e.name().into_inner() == b"row" {
                    return
                }
            },
            Event::Eof => panic!("Error not find {} end element", "row")
        );
    }

    pub(crate) fn write_to(
        &self,
        writer: &mut Writer<Cursor<Vec<u8>>>,
        stylesheet: &mut Stylesheet,
        spans: String,
        empty_flag: bool,
    ) {
        let xf_index_str: String;
        let xf_index = stylesheet.set_style(self.get_style());

        // row
        let mut attributes: Vec<(&str, &str)> = Vec::new();
        let row_num = self.row_num.get_value_string();
        attributes.push(("r", &row_num));
        if !empty_flag {
            attributes.push(("spans", &spans));
        }
        let height = self.height.get_value_string();
        if self.height.get_value() != &0f64 {
            attributes.push(("ht", &height));
        }
        if *self.thick_bot.get_value() {
            attributes.push(("thickBot", self.thick_bot.get_value_string()));
        }
        if *self.custom_height.get_value() {
            attributes.push(("customHeight", self.custom_height.get_value_string()));
        }
        if xf_index > 0 {
            attributes.push(("customFormat", "1"));
        }
        if *self.hidden.get_value() {
            attributes.push(("hidden", self.hidden.get_value_string()));
        }
        let descent = self.descent.get_value_string();
        if self.descent.has_value() {
            attributes.push(("x14ac:dyDescent", &descent));
        }

        if xf_index > 0 {
            xf_index_str = xf_index.to_string();
            attributes.push(("s", &xf_index_str));
        }

        write_start_tag(writer, "row", attributes, empty_flag);
    }
}