crier

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

commit 1d970a6c39bf770bd69335b663c64c1db5228be4
parent e7d54dac90dd564098beb5c7088d07cde720372e
Author: lash <dev@holbrook.no>
Date:   Sat,  6 Jul 2024 18:39:35 +0100

Implement iterator sorting for sequencer

Diffstat:
MCargo.lock | 17+++++++++++++++++
MCargo.toml | 2++
Msrc/lib.rs | 3++-
Msrc/tests.rs | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -155,6 +155,8 @@ dependencies = [ "digest", "feed-rs", "http", + "itertools", + "mediatype", "rs_sha512", "rss", "tempfile", @@ -257,6 +259,12 @@ dependencies = [ ] [[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + +[[package]] name = "encoding_rs" version = "0.8.34" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -394,6 +402,15 @@ dependencies = [ ] [[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + +[[package]] name = "itoa" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml @@ -21,9 +21,11 @@ http = "^1.0" chrono = "^0.4" atom_syndication = "^0.12" uuid = "^1.9" +itertools = "^0.13" [dev-dependencies] tempfile = "3.3.0" +mediatype = "^0.19" [features] fs = [] diff --git a/src/lib.rs b/src/lib.rs @@ -11,6 +11,7 @@ use rs_sha512::Sha512Hasher; //use chrono::DateTime; use chrono::Local; use atom_syndication::Feed as OutFeed; +use itertools::Itertools; mod meta; mod io; @@ -146,7 +147,7 @@ impl<'a> Iterator for Sequencer<'a> { if self.limit == 0 { self.item_keys = Vec::new(); - for k in self.items.keys() { + for k in self.items.keys().sorted() { self.item_keys.push(k.clone()); self.limit += 1; } diff --git a/src/tests.rs b/src/tests.rs @@ -2,6 +2,8 @@ use std::clone::Clone; use std::fs::File; use feed_rs::model::Entry; +use feed_rs::model::Text; +use mediatype::MediaTypeBuf; use chrono::DateTime; use tempfile::NamedTempFile; use tempfile::TempDir; @@ -115,6 +117,61 @@ fn test_feed_write_extcache() { f = NamedTempFile::new().unwrap(); fr = f.reopen().unwrap(); r = seq.write_to(f).unwrap(); + assert_eq!(r, 15); assert_eq!(fr.metadata().unwrap().len(), 254); } + +#[test] +#[cfg(feature = "fs")] +fn test_sequence_order() { + let mut seq = Sequencer::new(); + let mut entry: Entry; + let mut s: String; + let mut r: Vec<u8>; + + entry = Entry::default(); + entry.id = String::from("g"); + s = String::from("inky"); + entry.title = Some(Text{ + content_type: MediaTypeBuf::from_string(String::from("text/plain")).unwrap(), + src: Some(s.clone()), + content: s, + + }); + entry.published = Some(DateTime::parse_from_rfc3339("2024-06-25T20:46:00+02:00").unwrap().into()); + seq.add(entry); + + + entry = Entry::default(); + entry.id = String::from("b"); + s = String::from("pinky"); + entry.title = Some(Text{ + content_type: MediaTypeBuf::from_string(String::from("text/plain")).unwrap(), + src: Some(s.clone()), + content: s, + + }); + entry.published = Some(DateTime::parse_from_rfc3339("2023-06-25T20:46:00+02:00").unwrap().into()); + seq.add(entry); + + entry = Entry::default(); + entry.id = String::from("a"); + s = String::from("blinky"); + entry.title = Some(Text{ + content_type: MediaTypeBuf::from_string(String::from("text/plain")).unwrap(), + src: Some(s.clone()), + content: s, + + }); + entry.published = Some(DateTime::parse_from_rfc3339("2024-06-25T20:46:00+02:00").unwrap().into()); + seq.add(entry); + + // TODO find value where sort digest is reverse of lexical id + r = seq.next().unwrap(); + assert_eq!(r, Vec::from("b")); + r = seq.next().unwrap(); + assert_eq!(r, Vec::from("g")); + r = seq.next().unwrap(); + assert_eq!(r, Vec::from("a")); +}