Skip to main content

easyofd_core/doc/permission/
print.rs

1//! 打印权限。
2
3/// 对应 Java: org.ofdrw.core.basicStructure.Print
4///
5/// 打印权限控制,限制可打印份数。
6#[derive(Debug, Clone)]
7pub struct Print {
8    /// 是否允许打印。默认 true。
9    pub printable: bool,
10    /// 最大打印份数。None 表示不限制。
11    pub copies: Option<u32>,
12}
13
14impl Print {
15    /// 创建允许打印的权限。
16    #[must_use]
17    pub fn new() -> Self {
18        Self {
19            printable: true,
20            copies: None,
21        }
22    }
23
24    /// 创建禁止打印的权限。
25    #[must_use]
26    pub fn disabled() -> Self {
27        Self {
28            printable: false,
29            copies: None,
30        }
31    }
32
33    /// 设置最大打印份数。
34    #[must_use]
35    pub fn with_copies(mut self, copies: u32) -> Self {
36        self.copies = Some(copies);
37        self
38    }
39
40    /// 序列化为 XML 字符串。
41    #[must_use]
42    pub fn to_xml_string(&self) -> String {
43        let copies_attr = match self.copies {
44            Some(c) => format!(" Copies=\"{c}\""),
45            None => String::new(),
46        };
47        format!("<Print Printable=\"{}\"{copies_attr}/>", self.printable)
48    }
49}
50
51impl Default for Print {
52    fn default() -> Self {
53        Self::new()
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_print_new() {
63        let p = Print::new();
64        assert!(p.printable);
65        assert!(p.copies.is_none());
66        let p2 = Print::default();
67        assert!(p2.printable);
68    }
69
70    #[test]
71    fn test_print_disabled_and_xml() {
72        let p = Print::disabled();
73        let xml = p.to_xml_string();
74        assert!(xml.contains("Printable=\"false\""));
75
76        let p3 = Print::new().with_copies(3);
77        let xml3 = p3.to_xml_string();
78        assert!(xml3.contains("Copies=\"3\""));
79        assert!(xml3.contains("Printable=\"true\""));
80    }
81}