Skip to main content

umya_spreadsheet/structs/
protection.rs

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