use std::borrow::Cow; use async_trait::async_trait; #[derive(Debug, thiserror::Error)] pub enum Error { #[error("Unable to connect")] Connect, #[error("Entry is invalid")] InvalidEntry, #[error("Can't invalidate keys {0:?}")] Invalidate(Vec), #[error("Failed to parse config")] InvalidConfig, } pub type CResult = Result; pub struct Config(pub String); impl Config { pub fn config(self) -> Result { toml::from_str(&self.0) } } #[derive(Debug)] pub enum InvalidatePattern<'s> { StartsWith(Cow<'s, str>), EndsWith(Cow<'s, str>), Contains(Cow<'s, str>), Const(Cow<'s, str>), } #[async_trait] pub trait CacheAdapter: Sized { async fn new(config: Config) -> CResult; async fn read(&mut self, key: &str) -> CResult> where T: serde::de::DeserializeOwned; async fn set( &mut self, key: &str, data: T, expires_in: Option, ) -> CResult<()> where T: serde::Serialize + Send; async fn invalidate(&mut self, pattern: InvalidatePattern<'_>) -> CResult; async fn clear(&mut self) -> CResult; } pub struct CacheStorage {}