Skip to main content

easyofd_core/graph/tight/method/
close.rs

1//! 闭合路径方法。
2//!
3//! 对应 Java: org.ofdrw.core.graph.tight.method.Close
4
5use crate::xml_element::{XmlElement, XmlElementError, XmlNode};
6
7/// 闭合路径方法。
8///
9/// 自动闭合到当前路径的起始点,并以该点为当前点。
10///
11/// 对应 Java: org.ofdrw.core.graph.tight.method.Close
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct Close;
14
15impl Close {
16    /// 创建闭合命令。
17    #[must_use]
18    pub fn new() -> Self {
19        Self
20    }
21
22    /// 序列化为缩写数据字符串(C 命令格式)。
23    #[must_use]
24    pub fn to_abbreviated_string(&self) -> &'static str {
25        "C"
26    }
27}
28
29impl Default for Close {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl std::fmt::Display for Close {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        f.write_str("C")
38    }
39}
40
41impl XmlElement for Close {
42    /// 对应 Java: Close 元素名 "Close"。
43    fn element_name(&self) -> &'static str {
44        "Close"
45    }
46
47    fn attributes(&self) -> Vec<(String, String)> {
48        Vec::new()
49    }
50
51    /// 覆写 write_xml:文本内容为 "C"。
52    fn write_xml(&self, out: &mut String) {
53        out.push_str("<Close>C</Close>");
54    }
55
56    fn from_xml(_node: &XmlNode) -> Result<Self, XmlElementError> {
57        Ok(Self)
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64    use crate::xml_parse::parse_xml_to_nodes;
65
66    #[test]
67    fn close_new() {
68        let c = Close::new();
69        assert_eq!(c.to_abbreviated_string(), "C");
70    }
71
72    #[test]
73    fn close_display() {
74        assert_eq!(format!("{Close}"), "C");
75    }
76
77    #[test]
78    fn close_default() {
79        let c = Close;
80        assert_eq!(c.to_abbreviated_string(), "C");
81    }
82
83    #[test]
84    fn test_xml_element_name() {
85        assert_eq!(Close.element_name(), "Close");
86    }
87
88    #[test]
89    fn test_xml_element_roundtrip() {
90        let xml = Close.to_xml();
91        assert_eq!(xml, "<Close>C</Close>");
92        let node = parse_xml_to_nodes(&xml).unwrap();
93        let c2 = Close::from_xml(&node).unwrap();
94        assert_eq!(Close, c2);
95    }
96}