Files
zesdex/crates/zesdex-cms/src/application/memory_service.rs
T

47 lines
1.4 KiB
Rust
Raw Normal View History

//! Memory use-case implementations.
//!
//! `MemoryServiceImpl` is generic over `R: MemoryRepository`, delegating
//! all persistence to that adapter.
use anyhow::{Context, Result};
use crate::domain::memory::Memory;
use crate::domain::repository::MemoryRepository;
use crate::domain::service::MemoryService;
/// Generic memory service backed by an injected repository.
pub struct MemoryServiceImpl<R> {
pub repo: R,
pub memory_dir: std::path::PathBuf,
}
impl<R: MemoryRepository> MemoryServiceImpl<R> {
/// Create a new service with the given repository and memory directory.
pub fn new(repo: R, memory_dir: impl Into<std::path::PathBuf>) -> Self {
Self {
repo,
memory_dir: memory_dir.into(),
}
}
}
impl<R: MemoryRepository> MemoryService for MemoryServiceImpl<R> {
fn list_memories(&self) -> Result<Vec<String>> {
self.repo
.list(&self.memory_dir)
.context("failed to list memories")
}
fn save_memory(&self, memory: &Memory) -> Result<()> {
self.repo
.save(&self.memory_dir, memory)
.with_context(|| format!("failed to save memory '{}'", memory.name))
}
fn delete_memory(&self, name: &str) -> Result<()> {
self.repo
.delete(&self.memory_dir, name)
.with_context(|| format!("failed to delete memory '{name}'"))
}
}