use crate::error::StorageError;
use crate::service::Service;
use doido_core::Result;
use std::collections::HashMap;
use std::sync::Mutex;
pub struct MemoryService {
name: String,
data: Mutex<HashMap<String, Vec<u8>>>,
}
impl MemoryService {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
data: Mutex::new(HashMap::new()),
}
}
}
impl Default for MemoryService {
fn default() -> Self {
Self::new("memory")
}
}
#[async_trait::async_trait]
impl Service for MemoryService {
fn name(&self) -> &str {
&self.name
}
async fn upload(&self, key: &str, data: Vec<u8>, _content_type: Option<&str>) -> Result<()> {
self.data.lock().unwrap().insert(key.to_string(), data);
Ok(())
}
async fn download(&self, key: &str) -> Result<Vec<u8>> {
self.data
.lock()
.unwrap()
.get(key)
.cloned()
.ok_or_else(|| StorageError::NotFound(key.to_string()).into())
}
async fn delete(&self, key: &str) -> Result<()> {
self.data.lock().unwrap().remove(key);
Ok(())
}
async fn exists(&self, key: &str) -> Result<bool> {
Ok(self.data.lock().unwrap().contains_key(key))
}
async fn size(&self, key: &str) -> Result<u64> {
self.data
.lock()
.unwrap()
.get(key)
.map(|b| b.len() as u64)
.ok_or_else(|| StorageError::NotFound(key.to_string()).into())
}
}