fs.rs (1673B)
1 use std::path::Path; 2 use std::path::PathBuf; 3 use std::fs::File; 4 use std::collections::HashMap; 5 use std::io::Write; 6 7 use atom_syndication::Feed; 8 9 use super::FeedMethod; 10 use super::FeedGet; 11 use crate::cache::Cache; 12 use crate::rss::from_file as rss_from_file; 13 14 15 pub struct FsFeed { 16 } 17 18 pub struct FsCache { 19 dir: PathBuf, 20 files: HashMap<String, File>, 21 } 22 23 impl FeedGet for FsFeed { 24 fn get(&self, s: &str, _method: Option<FeedMethod>) -> Result<Feed, u64> { 25 let feed: Feed; 26 match rss_from_file(s, false) { 27 Ok(v) => { 28 feed = v; 29 }, 30 Err(e) => { 31 return Err(0); 32 }, 33 }; 34 Ok(feed) 35 } 36 } 37 38 impl FsCache { 39 pub fn new(path: PathBuf) -> FsCache { 40 FsCache{ 41 dir: path, 42 files: HashMap::new(), 43 } 44 } 45 } 46 47 impl Cache for FsCache { 48 fn open(&mut self, id: String) -> &mut dyn Write { 49 let p: &Path; 50 let fp: PathBuf; 51 let mut s: String; 52 let mut ids: String; 53 let mut f: File; 54 55 if !self.files.contains_key(&id) { 56 p = Path::new(self.dir.as_path()); 57 58 ids = id.clone(); 59 ids = ids.replace("/", "%2F"); 60 ids = ids.replace("\\", "%5C"); 61 62 fp = p.join(ids); 63 s = String::from(fp.to_str().unwrap()); 64 f = File::create(s.as_str()).unwrap(); 65 self.files.insert(id.clone(), f); 66 } 67 return self.files.get_mut(&id).unwrap(); 68 } 69 70 fn close(&mut self, id: String) -> usize { 71 if self.files.contains_key(&id) { 72 self.files.remove(&id); 73 } 74 0 75 } 76 }