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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// sheetProtection
use super::BooleanValue;
use super::StringValue;
use super::UInt32Value;
use helper::crypt::*;
use md5::Digest;
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(Default, Debug, Clone, PartialEq, PartialOrd)]
pub struct SheetProtection {
    algorithm_name: StringValue,
    hash_value: StringValue,
    salt_value: StringValue,
    spin_count: UInt32Value,
    password: StringValue,
    sheet: BooleanValue,
    objects: BooleanValue,
    delete_rows: BooleanValue,
    insert_columns: BooleanValue,
    delete_columns: BooleanValue,
    insert_hyperlinks: BooleanValue,
    auto_filter: BooleanValue,
    scenarios: BooleanValue,
    format_cells: BooleanValue,
    format_columns: BooleanValue,
    insert_rows: BooleanValue,
    format_rows: BooleanValue,
    pivot_tables: BooleanValue,
    select_locked_cells: BooleanValue,
    select_unlocked_cells: BooleanValue,
    sort: BooleanValue,
}
impl SheetProtection {
    pub fn get_algorithm_name(&self) -> &str {
        self.algorithm_name.get_value()
    }

    pub fn set_algorithm_name<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.algorithm_name.set_value(value);
        self
    }

    pub fn get_hash_value(&self) -> &str {
        self.hash_value.get_value()
    }

    pub fn set_hash_value<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.hash_value.set_value(value);
        self
    }

    pub fn get_salt_value(&self) -> &str {
        self.salt_value.get_value()
    }

    pub fn set_salt_value<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.salt_value.set_value(value);
        self
    }

    pub fn get_spin_count(&self) -> &u32 {
        self.spin_count.get_value()
    }

    pub fn set_spin_count(&mut self, value: u32) -> &mut Self {
        self.spin_count.set_value(value);
        self
    }

    pub fn get_password_raw(&self) -> &str {
        self.password.get_value()
    }

    pub fn set_password_raw<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.password.set_value(value);
        self
    }

    pub fn remove_password_raw(&mut self) -> &mut Self {
        self.password.remove_value();
        self
    }

    pub fn get_sheet(&self) -> &bool {
        self.sheet.get_value()
    }

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

    pub fn get_objects(&self) -> &bool {
        self.objects.get_value()
    }

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

    pub fn get_delete_rows(&self) -> &bool {
        self.delete_rows.get_value()
    }

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

    pub fn get_insert_columns(&self) -> &bool {
        self.insert_columns.get_value()
    }

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

    pub fn get_delete_columns(&self) -> &bool {
        self.delete_columns.get_value()
    }

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

    pub fn get_insert_hyperlinks(&self) -> &bool {
        self.insert_hyperlinks.get_value()
    }

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

    pub fn get_auto_filter(&self) -> &bool {
        self.auto_filter.get_value()
    }

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

    pub fn get_scenarios(&self) -> &bool {
        self.scenarios.get_value()
    }

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

    pub fn get_format_cells(&self) -> &bool {
        self.format_cells.get_value()
    }

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

    pub fn get_format_columns(&self) -> &bool {
        self.format_columns.get_value()
    }

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

    pub fn get_insert_rows(&self) -> &bool {
        self.insert_rows.get_value()
    }

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

    pub fn get_format_rows(&self) -> &bool {
        self.format_rows.get_value()
    }

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

    pub fn get_pivot_tables(&self) -> &bool {
        self.pivot_tables.get_value()
    }

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

    pub fn get_select_locked_cells(&self) -> &bool {
        self.select_locked_cells.get_value()
    }

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

    pub fn get_select_unlocked_cells(&self) -> &bool {
        self.select_unlocked_cells.get_value()
    }

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

    pub fn get_sort(&self) -> &bool {
        self.sort.get_value()
    }

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

    pub fn set_password(&mut self, password: &str) -> &mut Self {
        encrypt_sheet_protection(password, self);
        self
    }

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        _reader: &mut Reader<R>,
        e: &BytesStart,
    ) {
        set_string_from_xml!(self, e, algorithm_name, "algorithmName");
        set_string_from_xml!(self, e, hash_value, "hashValue");
        set_string_from_xml!(self, e, salt_value, "saltValue");
        set_string_from_xml!(self, e, spin_count, "spinCount");
        set_string_from_xml!(self, e, password, "password");
        set_string_from_xml!(self, e, sheet, "sheet");
        set_string_from_xml!(self, e, objects, "objects");
        set_string_from_xml!(self, e, delete_rows, "deleteRows");
        set_string_from_xml!(self, e, insert_columns, "insertColumns");
        set_string_from_xml!(self, e, delete_columns, "deleteColumns");
        set_string_from_xml!(self, e, insert_hyperlinks, "insertHyperlinks");
        set_string_from_xml!(self, e, auto_filter, "autoFilter");
        set_string_from_xml!(self, e, scenarios, "scenarios");
        set_string_from_xml!(self, e, format_cells, "formatCells");
        set_string_from_xml!(self, e, format_columns, "formatColumns");
        set_string_from_xml!(self, e, insert_rows, "insertRows");
        set_string_from_xml!(self, e, format_rows, "formatRows");
        set_string_from_xml!(self, e, pivot_tables, "pivotTables");
        set_string_from_xml!(self, e, select_locked_cells, "selectLockedCells");
        set_string_from_xml!(self, e, select_unlocked_cells, "selectUnlockedCells");
        set_string_from_xml!(self, e, sort, "sort");
    }

    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
        // sheetProtection
        let mut attributes: Vec<(&str, &str)> = Vec::new();
        if self.algorithm_name.has_value() {
            attributes.push(("algorithmName", self.algorithm_name.get_value_string()));
        }
        if self.hash_value.has_value() {
            attributes.push(("hashValue", self.hash_value.get_value_string()));
        }
        if self.salt_value.has_value() {
            attributes.push(("saltValue", self.salt_value.get_value_string()));
        }
        let spin_count = self.spin_count.get_value_string();
        if self.spin_count.has_value() {
            attributes.push(("spinCount", &spin_count));
        }
        if self.password.has_value() {
            attributes.push(("password", self.password.get_value_string()));
        }
        if self.sheet.has_value() {
            attributes.push(("sheet", self.sheet.get_value_string()));
        }
        if self.objects.has_value() {
            attributes.push(("objects", self.objects.get_value_string()));
        }
        if self.delete_rows.has_value() {
            attributes.push(("deleteRows", self.delete_rows.get_value_string()));
        }
        if self.insert_columns.has_value() {
            attributes.push(("insertColumns", self.insert_columns.get_value_string()));
        }
        if self.delete_columns.has_value() {
            attributes.push(("deleteColumns", self.delete_columns.get_value_string()));
        }
        if self.insert_hyperlinks.has_value() {
            attributes.push((
                "insertHyperlinks",
                self.insert_hyperlinks.get_value_string(),
            ));
        }
        if self.auto_filter.has_value() {
            attributes.push(("autoFilter", self.auto_filter.get_value_string()));
        }
        if self.scenarios.has_value() {
            attributes.push(("scenarios", self.scenarios.get_value_string()));
        }
        if self.format_cells.has_value() {
            attributes.push(("formatCells", self.format_cells.get_value_string()));
        }
        if self.format_columns.has_value() {
            attributes.push(("formatColumns", self.format_columns.get_value_string()));
        }
        if self.insert_rows.has_value() {
            attributes.push(("insertRows", self.insert_rows.get_value_string()));
        }
        if self.format_rows.has_value() {
            attributes.push(("formatRows", self.format_rows.get_value_string()));
        }
        if self.pivot_tables.has_value() {
            attributes.push(("pivotTables", self.pivot_tables.get_value_string()));
        }
        if self.select_locked_cells.has_value() {
            attributes.push((
                "selectLockedCells",
                self.select_locked_cells.get_value_string(),
            ));
        }
        if self.select_unlocked_cells.has_value() {
            attributes.push((
                "selectUnlockedCells",
                self.select_unlocked_cells.get_value_string(),
            ));
        }
        if self.sort.has_value() {
            attributes.push(("sort", self.sort.get_value_string()));
        }

        write_start_tag(writer, "sheetProtection", attributes, true);
    }
}