easyofd_core/signatures/
signatures.rs1use super::signature::Signature;
11
12#[derive(Debug, Clone)]
16pub struct Signatures {
17 pub max_sign_id: Option<String>,
21 pub signatures: Vec<Signature>,
23}
24
25impl Signatures {
26 #[must_use]
28 pub fn new() -> Self {
29 Self {
30 max_sign_id: None,
31 signatures: Vec::new(),
32 }
33 }
34
35 #[must_use]
37 pub fn max_sign_id(mut self, max_sign_id: impl Into<String>) -> Self {
38 self.max_sign_id = Some(max_sign_id.into());
39 self
40 }
41
42 #[must_use]
44 pub fn add_signature(mut self, signature: Signature) -> Self {
45 self.signatures.push(signature);
46 self
47 }
48
49 #[must_use]
51 pub fn to_xml_string(&self) -> String {
52 use std::fmt::Write;
53
54 let mut xml = String::from(r#"<?xml version="1.0" encoding="UTF-8"?>"#);
55 xml.push('\n');
56 xml.push_str(r#"<ofd:Signatures xmlns:ofd="http://www.ofdspec.org/2016">"#);
57
58 if let Some(ref max_id) = self.max_sign_id {
59 xml.push('\n');
60 let _ = write!(xml, "<ofd:MaxSignId>{max_id}</ofd:MaxSignId>");
61 }
62
63 for sig in &self.signatures {
64 xml.push('\n');
65 xml.push_str(&sig.to_xml_string());
66 }
67
68 xml.push_str("\n</ofd:Signatures>");
69 xml
70 }
71}
72
73impl Default for Signatures {
74 fn default() -> Self {
75 Self::new()
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn test_signatures_new() {
85 let sigs = Signatures::new();
86 assert!(sigs.max_sign_id.is_none());
87 assert!(sigs.signatures.is_empty());
88 }
89
90 #[test]
91 fn test_signatures_builder() {
92 let sigs = Signatures::new()
93 .max_sign_id("s2")
94 .add_signature(Signature::new("s1", "/Doc_0/Signs/Sign_0/Signature.xml"))
95 .add_signature(Signature::new("s2", "/Doc_0/Signs/Sign_1/Signature.xml"));
96 assert_eq!(sigs.max_sign_id.as_deref(), Some("s2"));
97 assert_eq!(sigs.signatures.len(), 2);
98 }
99
100 #[test]
101 fn test_signatures_xml() {
102 let sigs = Signatures::new()
103 .max_sign_id("s1")
104 .add_signature(Signature::new("s1", "/Doc_0/Signs/Sign_0/Signature.xml"));
105 let xml = sigs.to_xml_string();
106 assert!(xml.contains("<?xml version=\"1.0\""));
107 assert!(xml.contains("ofd:Signatures"));
108 assert!(xml.contains("<ofd:MaxSignId>s1</ofd:MaxSignId>"));
109 assert!(xml.contains(r#"ID="s1""#));
110 assert!(xml.contains("</ofd:Signatures>"));
111 }
112
113 #[test]
114 fn test_signatures_default() {
115 let sigs = Signatures::default();
116 assert!(sigs.signatures.is_empty());
117 }
118}