Skip to main content

easyofd_core/action/
uri.rs

1//! URI 超链接动作。
2//!
3//! 对应 Java: org.ofdrw.core.action.actionType.URI
4
5use super::OfdAction;
6
7/// URI 超链接动作。
8///
9/// 打开一个 URI(统一资源标识符),对应 GB/T 33190 第 15 章的 URI 动作。
10///
11/// 对应 Java: org.ofdrw.core.action.actionType.URI
12#[derive(Debug, Clone)]
13pub struct URI {
14    /// 超链接的 URI 地址。
15    ///
16    /// 对应 Java: URI.uri (String)
17    pub uri: String,
18}
19
20impl URI {
21    /// 创建一个新的 URI 动作。
22    ///
23    /// 对应 Java: new URI(String uri)
24    #[must_use]
25    pub fn new(uri: impl Into<String>) -> Self {
26        Self { uri: uri.into() }
27    }
28}
29
30impl OfdAction for URI {
31    fn to_xml_string(&self) -> String {
32        format!("<ofd:URI URI=\"{}\"/>", self.uri)
33    }
34
35    fn clone_box(&self) -> Box<dyn OfdAction> {
36        Box::new(self.clone())
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn test_uri_new() {
46        let uri = URI::new("https://example.com");
47        assert_eq!(uri.uri, "https://example.com");
48    }
49
50    #[test]
51    fn test_uri_to_xml() {
52        let uri = URI::new("https://example.com/path?q=1");
53        let xml = uri.to_xml_string();
54        assert!(xml.contains("URI=\"https://example.com/path?q=1\""));
55        assert!(xml.contains("<ofd:URI"));
56        assert!(xml.ends_with("/>"));
57    }
58
59    #[test]
60    fn test_uri_from_string() {
61        let s = String::from("http://test.org");
62        let uri = URI::new(s);
63        assert_eq!(uri.uri, "http://test.org");
64    }
65
66    #[test]
67    fn test_uri_clone_debug() {
68        let uri = URI::new("https://a.b");
69        let uri2 = uri.clone();
70        assert_eq!(uri2.uri, "https://a.b");
71        let dbg = format!("{uri:?}");
72        assert!(dbg.contains("URI"));
73    }
74}