Skip to main content

umya_spreadsheet/structs/
protection.rs

1// protection
2use std::io::Cursor;
3
4use md5::Digest;
5use quick_xml::{
6    Reader,
7    Writer,
8    events::BytesStart,
9};
10
11use super::BooleanValue;
12use crate::{
13    reader::driver::{
14        get_attribute,
15        set_string_from_xml,
16    },
17    writer::driver::write_start_tag,
18};
19
20#[derive(Default, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
21pub struct Protection {
22    locked: BooleanValue,
23    hidden: BooleanValue,
24}
25
26impl Protection {
27    #[inline]
28    #[must_use]
29    pub fn locked(&self) -> bool {
30        self.locked.value()
31    }
32
33    #[inline]
34    #[must_use]
35    #[deprecated(since = "3.0.0", note = "Use locked()")]
36    pub fn get_locked(&self) -> bool {
37        self.locked()
38    }
39
40    #[inline]
41    pub fn set_locked(&mut self, value: bool) {
42        self.locked.set_value(value);
43    }
44
45    #[inline]
46    pub fn hidden(&mut self) -> bool {
47        self.hidden.value()
48    }
49
50    #[inline]
51    #[deprecated(since = "3.0.0", note = "Use hidden()")]
52    pub fn get_hidden(&mut self) -> bool {
53        self.hidden()
54    }
55
56    #[inline]
57    pub fn set_hidden(&mut self, value: bool) {
58        self.hidden.set_value(value);
59    }
60
61    #[inline]
62    #[allow(dead_code)]
63    pub(crate) fn hash_code(&self) -> String {
64        format!(
65            "{:x}",
66            md5::Md5::digest(format!(
67                "{}{}",
68                self.locked.hash_string(),
69                self.hidden.hash_string()
70            ))
71        )
72    }
73
74    #[inline]
75    #[allow(dead_code)]
76    #[deprecated(since = "3.0.0", note = "Use hash_code()")]
77    pub(crate) fn get_hash_code(&self) -> String {
78        self.hash_code()
79    }
80
81    #[inline]
82    pub(crate) fn set_attributes<R: std::io::BufRead>(
83        &mut self,
84        _reader: &mut Reader<R>,
85        e: &BytesStart,
86    ) {
87        set_string_from_xml!(self, e, locked, "locked");
88        set_string_from_xml!(self, e, hidden, "hidden");
89    }
90
91    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
92        // protection
93        let mut attributes: crate::structs::AttrCollection = Vec::new();
94        if self.locked.has_value() {
95            attributes.push(("locked", self.locked.value_string()).into());
96        }
97        if self.hidden.has_value() {
98            attributes.push(("hidden", self.hidden.value_string()).into());
99        }
100        write_start_tag(writer, "protection", attributes, true);
101    }
102}