// This file is part of Locus . // Copyright (C) 2022-2022 Fuwn // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, version 3. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // Copyright (C) 2022-2022 Fuwn // SPDX-License-Identifier: GPL-3.0-only 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, k); 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: "".to_string(), } } 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, k); acc }), self.link, self.content ) } }