Skip to main content

easyofd_core/extensions/
property.rs

1//! 扩展属性。
2
3/// 对应 Java: org.ofdrw.core.extendObj.Property
4///
5/// 扩展属性,以键值对形式存储扩展的配置信息。
6#[derive(Debug, Clone)]
7pub struct Property {
8    /// 属性名。
9    pub name: String,
10    /// 属性值。
11    pub value: String,
12}
13
14impl Property {
15    /// 创建新的扩展属性。
16    #[must_use]
17    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
18        Self {
19            name: name.into(),
20            value: value.into(),
21        }
22    }
23
24    /// 序列化为 XML 字符串。
25    #[must_use]
26    pub fn to_xml_string(&self) -> String {
27        format!(
28            "<Property Name=\"{}\" Value=\"{}\"/>",
29            self.name, self.value
30        )
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn test_property_new() {
40        let p = Property::new("key", "value");
41        assert_eq!(p.name, "key");
42        assert_eq!(p.value, "value");
43    }
44
45    #[test]
46    fn test_property_xml() {
47        let p = Property::new("encoding", "UTF-8");
48        let xml = p.to_xml_string();
49        assert!(xml.contains("Name=\"encoding\""));
50        assert!(xml.contains("Value=\"UTF-8\""));
51        assert!(xml.contains("/>"));
52    }
53}