use super::error::HandlerError;
use std::collections::HashMap;
use std::path::PathBuf;
pub trait Cacher {
fn get_file_path(&self, _url: &str) -> Result<Option<PathBuf>, HandlerError> {
Ok(None)
}
fn read(&mut self, url: &str) -> Result<Option<String>, HandlerError>;
fn write(&mut self, url: &str, content: &str) -> Result<(), HandlerError>;
}
pub type BoxedCacher = Box<dyn Cacher>;
#[derive(Default)]
#[doc(hidden)]
pub struct MemoryCache {
cache: HashMap<String, String>,
}
impl Cacher for MemoryCache {
fn read(&mut self, url: &str) -> Result<Option<String>, HandlerError> {
Ok(self.cache.get(url).map(|v| v.to_owned()))
}
fn write(&mut self, url: &str, content: &str) -> Result<(), HandlerError> {
self.cache.insert(url.to_owned(), content.to_owned());
Ok(())
}
}