From 3634108c9f4c6e8f01e85f4b06d2a3c78f300fc3 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Fri, 8 Jul 2022 23:58:29 -0700 Subject: feat(blog): rss feed for blogs --- src/xml.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/xml.rs (limited to 'src/xml.rs') diff --git a/src/xml.rs b/src/xml.rs new file mode 100644 index 0000000..0709723 --- /dev/null +++ b/src/xml.rs @@ -0,0 +1,98 @@ +// 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}; + +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)| { + acc.push_str(&format!("<{}>{}", 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)| { + acc.push_str(&format!("<{}>{}", k, v, k)); + acc + }), + self.link, + self.content + ) + } +} -- cgit v1.2.3