easyofd_core/
ofd_element.rs1pub trait OfdElement {
16 fn ofd_element_name(&self) -> &'static str;
18
19 fn ofd_attributes(&self) -> Vec<(String, String)> {
21 Vec::new()
22 }
23
24 fn to_ofd_xml(&self) -> String {
26 let name = self.ofd_element_name();
27 let attrs = self.ofd_attributes();
28 let mut xml = String::from("<ofd:");
29 xml.push_str(name);
30 for (key, value) in &attrs {
31 xml.push(' ');
32 xml.push_str(key);
33 xml.push_str("=\"");
34 xml.push_str(value);
35 xml.push('"');
36 }
37 xml.push_str(" />");
38 xml
39 }
40}
41
42#[derive(Debug, Clone)]
49pub struct DefaultElementProxy {
50 pub element_name: String,
52 pub attributes: Vec<(String, String)>,
54}
55
56impl DefaultElementProxy {
57 #[must_use]
59 pub fn new(element_name: impl Into<String>) -> Self {
60 Self {
61 element_name: element_name.into(),
62 attributes: Vec::new(),
63 }
64 }
65
66 #[must_use]
68 pub fn attr(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
69 self.attributes.push((key.into(), value.into()));
70 self
71 }
72}
73
74impl OfdElement for DefaultElementProxy {
75 fn ofd_element_name(&self) -> &'static str {
76 "DefaultElement"
80 }
81
82 fn ofd_attributes(&self) -> Vec<(String, String)> {
83 self.attributes.clone()
84 }
85}
86
87pub trait OfdSimpleTypeElement {
94 fn ofd_value(&self) -> String;
96
97 fn from_ofd_value(s: &str) -> Result<Self, String>
99 where
100 Self: Sized;
101}
102
103#[cfg(test)]
104mod tests {
105 use super::*;
106
107 #[test]
108 fn test_default_element_proxy_new() {
109 let proxy = DefaultElementProxy::new("TestElement");
110 assert_eq!(proxy.element_name, "TestElement");
111 assert!(proxy.attributes.is_empty());
112 }
113
114 #[test]
115 fn test_default_element_proxy_with_attrs() {
116 let proxy = DefaultElementProxy::new("Page")
117 .attr("ID", "1")
118 .attr("Boundary", "0 0 210 297");
119 assert_eq!(proxy.attributes.len(), 2);
120 assert_eq!(proxy.attributes[0].0, "ID");
121 assert_eq!(proxy.attributes[1].1, "0 0 210 297");
122 }
123
124 #[test]
125 fn test_ofd_element_trait_default() {
126 let proxy = DefaultElementProxy::new("Test");
127 let xml = proxy.to_ofd_xml();
128 assert!(xml.contains("<ofd:DefaultElement"));
129 assert!(xml.contains("/>"));
130 }
131
132 #[test]
133 fn test_ofd_element_with_attrs() {
134 let proxy = DefaultElementProxy::new("Test").attr("Key", "Value");
135 let attrs = proxy.ofd_attributes();
136 assert_eq!(attrs.len(), 1);
137 assert_eq!(attrs[0].0, "Key");
138 }
139}