pub trait OfdElement {
fn ofd_element_name(&self) -> &'static str;
fn ofd_attributes(&self) -> Vec<(String, String)> {
Vec::new()
}
fn to_ofd_xml(&self) -> String {
let name = self.ofd_element_name();
let attrs = self.ofd_attributes();
let mut xml = String::from("<ofd:");
xml.push_str(name);
for (key, value) in &attrs {
xml.push(' ');
xml.push_str(key);
xml.push_str("=\"");
xml.push_str(value);
xml.push('"');
}
xml.push_str(" />");
xml
}
}
#[derive(Debug, Clone)]
pub struct DefaultElementProxy {
pub element_name: String,
pub attributes: Vec<(String, String)>,
}
impl DefaultElementProxy {
#[must_use]
pub fn new(element_name: impl Into<String>) -> Self {
Self {
element_name: element_name.into(),
attributes: Vec::new(),
}
}
#[must_use]
pub fn attr(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.attributes.push((key.into(), value.into()));
self
}
}
impl OfdElement for DefaultElementProxy {
fn ofd_element_name(&self) -> &'static str {
"DefaultElement"
}
fn ofd_attributes(&self) -> Vec<(String, String)> {
self.attributes.clone()
}
}
pub trait OfdSimpleTypeElement {
fn ofd_value(&self) -> String;
fn from_ofd_value(s: &str) -> Result<Self, String>
where
Self: Sized;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_element_proxy_new() {
let proxy = DefaultElementProxy::new("TestElement");
assert_eq!(proxy.element_name, "TestElement");
assert!(proxy.attributes.is_empty());
}
#[test]
fn test_default_element_proxy_with_attrs() {
let proxy = DefaultElementProxy::new("Page")
.attr("ID", "1")
.attr("Boundary", "0 0 210 297");
assert_eq!(proxy.attributes.len(), 2);
assert_eq!(proxy.attributes[0].0, "ID");
assert_eq!(proxy.attributes[1].1, "0 0 210 297");
}
#[test]
fn test_ofd_element_trait_default() {
let proxy = DefaultElementProxy::new("Test");
let xml = proxy.to_ofd_xml();
assert!(xml.contains("<ofd:DefaultElement"));
assert!(xml.contains("/>"));
}
#[test]
fn test_ofd_element_with_attrs() {
let proxy = DefaultElementProxy::new("Test").attr("Key", "Value");
let attrs = proxy.ofd_attributes();
assert_eq!(attrs.len(), 1);
assert_eq!(attrs[0].0, "Key");
}
}