commit 3ad1a9fd70b7f68915987477e88968ea56a8199f
parent d4642ad6f84c4815b16261eb99467e8f6e61e458
Author: lash <dev@holbrook.no>
Date: Thu, 25 Jul 2024 21:50:24 +0100
Add public metadata setters
Diffstat:
5 files changed, 75 insertions(+), 4 deletions(-)
diff --git a/crier-lib/Cargo.lock b/crier-lib/Cargo.lock
@@ -29,6 +29,7 @@ dependencies = [
[[package]]
name = "atom_syndication"
version = "0.12.3"
+source = "git+git://holbrook.no/contrib/atom_syndication?rev=9985c1610b2b819f5bd2f7a719567ee0b5419b85#9985c1610b2b819f5bd2f7a719567ee0b5419b85"
dependencies = [
"chrono",
"derive_builder",
diff --git a/crier-lib/src/lib.rs b/crier-lib/src/lib.rs
@@ -20,6 +20,7 @@ use atom_syndication::Content as OutContent;
use atom_syndication::Person as OutPerson;
use atom_syndication::Category as OutCategory;
use atom_syndication::FixedDateTime;
+use atom_syndication::Person;
use itertools::Itertools;
pub mod io;
@@ -77,6 +78,18 @@ impl<'a> Sequencer<'a> {
return self;
}
+ pub fn set_author(&mut self, name: &str) -> bool {
+ self.metadata.set_author(Person{
+ name: String::from(name),
+ email: None,
+ uri: None,
+ })
+ }
+
+ pub fn set_title(&mut self, title: &str) -> bool {
+ self.metadata.set_title(String::from(title))
+ }
+
pub fn add(&mut self, entry: Entry) -> bool {
let w: &mut dyn Write;
let mut id: String;
diff --git a/crier-lib/src/meta.rs b/crier-lib/src/meta.rs
@@ -12,7 +12,7 @@ pub struct FeedMetadata {
pub author: Person,
pub title: String,
pub id: String,
- incomplete: bool,
+ flag: u8,
}
impl Default for FeedMetadata {
@@ -25,18 +25,37 @@ impl Default for FeedMetadata {
},
title: String::from("?"),
id: Uuid::new_v4().to_string(),
- incomplete: true,
+ flag: 0,
}
}
+
+
}
impl FeedMetadata {
pub fn force(&mut self) {
- self.incomplete = false;
+ self.flag |= 3;
+ }
+
+ fn check_complete(&self) -> bool {
+ self.flag >= 3
+ }
+
+ pub fn set_author(&mut self, author: Person) -> bool {
+ self.author = author;
+ self.flag |= 1;
+ self.check_complete()
+ }
+
+
+ pub fn set_title(&mut self, title: String) -> bool {
+ self.title = title;
+ self.flag |= 2;
+ self.check_complete()
}
pub fn apply(&self, feed: &mut Feed) -> Result<(), Error> {
- if self.incomplete {
+ if !self.check_complete() {
return Err(Error::IncompleteFeedMetadata);
}
let mut persons = Vec::<Person>::new();
diff --git a/crier-lib/src/tests.rs b/crier-lib/src/tests.rs
@@ -10,11 +10,15 @@ use chrono::DateTime;
use tempfile::NamedTempFile;
use tempfile::tempdir;
use atom_syndication::Entry as OutEntry;
+use atom_syndication::Feed as OutFeed;
+use atom_syndication::Person;
use quick_xml::Reader as XMLReader;
use quick_xml::events::Event as XMLEvent;
use crate::Sequencer;
use crate::io::FeedGet;
+use crate::meta::FeedMetadata;
+use crate::Feed;
use crate::io::fs::FsCache;
#[cfg(feature = "fs")]
@@ -249,3 +253,33 @@ fn test_sequence_order() {
r = seq.next().unwrap();
check_xml_title(r, "clyde");
}
+
+#[test]
+fn test_meta() {
+ let mut o = FeedMetadata::default();
+ let mut feed = OutFeed::default();
+
+ match o.apply(&mut feed) {
+ Ok(r) => {
+ panic!("metadata should not be ready");
+ },
+ Err(e) => {},
+ };
+
+ o.set_title(String::from("foo"));
+ match o.apply(&mut feed) {
+ Ok(r) => {
+ panic!("metadata should not be ready");
+ },
+ Err(e) => {},
+ };
+
+ o.set_author(Person{
+ name: String::from("Foo Bar"),
+ email: Some("foo@bar.com".to_string()),
+ uri: Some("foo.bar.com".to_string()),
+ }
+ );
+ o.apply(&mut feed).unwrap();
+}
+
diff --git a/src/main.rs b/src/main.rs
@@ -86,6 +86,10 @@ fn main() {
let mut cache = MemCache::new();
let mut seq = Sequencer::new().with_cache(&mut cache);
+ seq.set_title("my new feed");
+ seq.set_author("Foo Bar");
+
+
#[cfg(feature = "logging")]
env_logger::init();