crier

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

commit e7d54dac90dd564098beb5c7088d07cde720372e
parent 29e13c50a6bb061a89b920ea54929097f6809170
Author: lash <dev@holbrook.no>
Date:   Sat,  6 Jul 2024 17:54:37 +0100

Complete fs cache module

Diffstat:
Msrc/io/fs.rs | 44+++++++++++++++++++++++++++++++++++++++++---
Msrc/lib.rs | 2+-
Msrc/tests.rs | 52++++++++++++++++++++++++++++------------------------
3 files changed, 70 insertions(+), 28 deletions(-)

diff --git a/src/io/fs.rs b/src/io/fs.rs @@ -3,10 +3,13 @@ use super::FeedGet; use feed_rs::model::Feed; use feed_rs::parser; //use http::Uri; -//use std::path::Path; +use std::path::Path; +use std::path::PathBuf; use std::fs::File; +use std::collections::HashMap; //use core::str::FromStr; //use std::io::stderr; +use std::io::Write; use crate::cache::Cache; @@ -14,6 +17,11 @@ use crate::cache::Cache; pub struct FsFeed { } +pub struct FsCache { + dir: PathBuf, + files: HashMap<String, File>, +} + impl FeedGet for FsFeed { fn get(&self, s: &str, _method: Option<FeedMethod>) -> Result<Feed, u64> { //let uri = Uri::from_str(s).unwrap(); @@ -23,6 +31,36 @@ impl FeedGet for FsFeed { } } -//impl Cache for FsCache { +impl FsCache { + pub fn new(path: PathBuf) -> FsCache { + FsCache{ + dir: path, + files: HashMap::new(), + } + } +} + +impl Cache for FsCache { + fn open(&mut self, id: String) -> &mut dyn Write { + let p: &Path; + let fp: PathBuf; + let s: &str; + let mut f: File; -//} + if !self.files.contains_key(&id) { + p = Path::new(self.dir.as_path()); + fp = p.join(id.clone()); + s = fp.to_str().unwrap(); + f = File::create(s).unwrap(); + self.files.insert(id.clone(), f); + } + return self.files.get_mut(&id).unwrap(); + } + + fn close(&mut self, id: String) -> usize { + if self.files.contains_key(&id) { + self.files.remove(&id); + } + 0 + } +} diff --git a/src/lib.rs b/src/lib.rs @@ -60,7 +60,7 @@ impl<'a> Sequencer<'a> { o } - pub fn with_cache(&mut self, w: &'a mut impl Cache) -> &Sequencer<'a> { + pub fn with_cache(mut self, w: &'a mut impl Cache) -> Sequencer<'a> { self.cache = Some(w); return self; } diff --git a/src/tests.rs b/src/tests.rs @@ -4,9 +4,11 @@ use std::fs::File; use feed_rs::model::Entry; use chrono::DateTime; use tempfile::NamedTempFile; +use tempfile::TempDir; -use super::Sequencer; -use super::io::FeedGet; +use crate::Sequencer; +use crate::io::FeedGet; +use crate::io::fs::FsCache; #[cfg(feature = "fs")] use crate::io::fs::FsFeed; @@ -92,25 +94,27 @@ fn test_feed_write() { assert_eq!(fr.metadata().unwrap().len(), 254); } -//#[test] -//#[cfg(feature = "fs")] -//fn test_feed_write_extcache() { -// let r: usize; -// let fs = FsFeed{}; -// let mut f: NamedTempFile; -// let fw: File; -// let fr: File; -// -// f = NamedTempFile::new().unwrap(); -// fw = f.reopen().unwrap(); -// -// let feed = fs.get("testdata/test.atom.xml", None).unwrap(); -// let mut seq = Sequencer::new().with_cache(&mut fw); -// -// seq.add_from(feed); -// 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_feed_write_extcache() { + let r: usize; + let fs = FsFeed{}; + let d: TempDir; + let f: NamedTempFile; + let fr: File; + let mut cache: FsCache; + + d = TempDir::new().unwrap(); + cache = FsCache::new(d.into_path()); + + let feed = fs.get("testdata/test.atom.xml", None).unwrap(); + let mut seq = Sequencer::new(); + seq = seq.with_cache(&mut cache); + + seq.add_from(feed); + 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); +}