Skip to main content

easyofd_core/action/
gotoa.rs

1//! 附件打开动作。
2//!
3//! 对应 Java: org.ofdrw.core.action.actionType.GotoA
4
5use super::OfdAction;
6
7/// 附件打开动作。
8///
9/// 打开一个已附加到 OFD 文档的附件文件,对应 GB/T 33190 第 15 章的 GotoA 动作。
10///
11/// 对应 Java: org.ofdrw.core.action.actionType.GotoA
12#[derive(Debug, Clone)]
13pub struct GotoA {
14    /// 附件的标识 ID。
15    ///
16    /// 对应 Java: GotoA.attachID (String)
17    pub attach_id: String,
18}
19
20impl GotoA {
21    /// 创建一个新的附件打开动作。
22    ///
23    /// 对应 Java: new GotoA(String attachID)
24    #[must_use]
25    pub fn new(attach_id: impl Into<String>) -> Self {
26        Self {
27            attach_id: attach_id.into(),
28        }
29    }
30}
31
32impl OfdAction for GotoA {
33    fn to_xml_string(&self) -> String {
34        format!("<ofd:GotoA AttachID=\"{}\"/>", self.attach_id)
35    }
36
37    fn clone_box(&self) -> Box<dyn OfdAction> {
38        Box::new(self.clone())
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_gotoa_new() {
48        let gotoa = GotoA::new("att_001");
49        assert_eq!(gotoa.attach_id, "att_001");
50    }
51
52    #[test]
53    fn test_gotoa_to_xml() {
54        let gotoa = GotoA::new("att_002");
55        let xml = gotoa.to_xml_string();
56        assert!(xml.contains("AttachID=\"att_002\""));
57        assert!(xml.contains("<ofd:GotoA"));
58        assert!(xml.ends_with("/>"));
59    }
60
61    #[test]
62    fn test_gotoa_from_string() {
63        let s = String::from("attachment_1");
64        let gotoa = GotoA::new(s);
65        assert_eq!(gotoa.attach_id, "attachment_1");
66    }
67
68    #[test]
69    fn test_gotoa_clone_debug() {
70        let gotoa = GotoA::new("att_1");
71        let gotoa2 = gotoa.clone();
72        assert_eq!(gotoa2.attach_id, "att_1");
73        let dbg = format!("{gotoa:?}");
74        assert!(dbg.contains("GotoA"));
75    }
76}