crier

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs

commit 96a01f38f9cf3abd96915b702856e2a92dccbffc
parent 7e2a962977ba886dfc5bafec2018fda61f1eb5c8
Author: lash <dev@holbrook.no>
Date:   Mon,  1 Jul 2024 01:55:04 +0100

Create metadata object that can apply itself to feed

Diffstat:
Msrc/lib.rs | 41++++++++++++++++++++++++++++++++++-------
Msrc/meta.rs | 29+++++++++++++++++++++++------
Msrc/tests.rs | 2+-
3 files changed, 58 insertions(+), 14 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs @@ -2,17 +2,25 @@ use std::collections::HashMap; use std::hash::Hasher; use std::hash::Hash; use std::iter::Iterator; +use std::io::Write; +use std::error::Error as StdError; + use feed_rs::model::Entry; use feed_rs::model::Feed; use rs_sha512::Sha512Hasher; use chrono::DateTime; use chrono::Local; use atom_syndication::Feed as OutFeed; -use std::io::Write; + mod meta; mod io; use meta::FeedMetadata; +#[derive(Debug)] +pub enum Error { + WriteError, +} + pub struct Sequencer { metadata: FeedMetadata, @@ -29,13 +37,18 @@ pub struct SequencerEntry { impl Sequencer { pub fn new() -> Sequencer { - Sequencer { + let mut o = Sequencer { metadata: FeedMetadata::default(), items: HashMap::new(), crsr: 0, limit: 0, item_keys: Vec::new(), - } + }; + + #[cfg(test)] + o.metadata.force(); + + o } pub fn add(&mut self, entry: Entry) -> bool { @@ -58,14 +71,28 @@ impl Sequencer { c } - fn write_to(&mut self, w: impl Write) -> Result<usize, atom_syndication::Error> { + fn write_to(&mut self, w: impl Write) -> Result<usize, Error> { let mut r: usize; let mut feed = OutFeed::default(); feed.set_id("urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6"); - feed.set_title("Mixed feed"); feed.set_updated(Local::now().to_utc()); - self.metadata.apply(&mut feed); - feed.write_to(w)?; + + match self.metadata.apply(&mut feed) { + Err(v) => { + return Err(Error::WriteError); + }, + Ok(_) => { + }, + + } + + match feed.write_to(w) { + Err(v) => { + return Err(Error::WriteError); + }, + Ok(_) => { + }, + } r = 0; for v in self { diff --git a/src/meta.rs b/src/meta.rs @@ -1,28 +1,45 @@ use atom_syndication::Person; use atom_syndication::Feed; +#[derive(Debug)] +pub enum Error { + IncompleteFeedMetadata, +} + + pub struct FeedMetadata { pub author: Person, - complete: bool, + pub title: String, + incomplete: bool, } impl Default for FeedMetadata { fn default() -> FeedMetadata { FeedMetadata{ author: Person{ - name: "No One".to_string(), - email: Some("none@devnull.com".to_string()), - uri: Some("https://devnull.com".to_string()), + name: "?".to_string(), + email: Some("?".to_string()), + uri: Some("?".to_string()), }, - complete: false, + title: String::from("?"), + incomplete: true, } } } impl FeedMetadata { - pub fn apply(&self, feed: &mut Feed) { + pub fn force(&mut self) { + self.incomplete = false; + } + + pub fn apply(&self, feed: &mut Feed) -> Result<(), Error> { + if self.incomplete { + return Err(Error::IncompleteFeedMetadata); + } let mut persons = Vec::<Person>::new(); persons.push(self.author.clone()); feed.set_authors(persons); + feed.set_title(self.title.clone()); + Ok(()) } } diff --git a/src/tests.rs b/src/tests.rs @@ -91,5 +91,5 @@ fn test_feed_write() { fr = f.reopen().unwrap(); r = seq.write_to(f).unwrap(); assert_eq!(r, 15); - assert_eq!(fr.metadata().unwrap().len(), 301); + assert_eq!(fr.metadata().unwrap().len(), 254); }