use std::{ collections::HashMap, fmt::{Display, Write}, }; pub struct Item { fields: HashMap, } impl Item { pub fn builder() -> Self { Self { fields: HashMap::new() } } pub fn add_field(&mut self, key: &str, value: &str) -> &mut Self { self.fields.insert(key.to_string(), value.to_string()); self } } impl Display for Item { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}", self.fields.iter().fold(String::new(), |mut acc, (k, v)| { let _ = write!(acc, "<{k}>{v}"); acc }) ) } } #[derive(Clone)] pub struct Writer { content: String, fields: HashMap, link: String, } impl Writer { pub fn builder() -> Self { Self { content: String::new(), fields: HashMap::default(), link: String::new(), } } pub fn add_link(&mut self, link: &str) -> &mut Self { self.link = link.to_string(); self } pub fn add_field(&mut self, key: &str, value: &str) -> &mut Self { self.fields.insert(key.to_string(), value.to_string()); self } pub fn add_item(&mut self, item: &Item) -> &mut Self { self.content.push_str(&item.to_string()); self } } impl Display for Writer { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}{}", self.fields.iter().fold(String::new(), |mut acc, (k, v)| { let _ = write!(acc, "<{k}>{v}"); acc }), self.link, self.content ) } }