use rustic_core::{ReadBackend, WriteBackend, FileType, Id, RusticResult};
use bytes::Bytes;
use std::collections::HashMap;
pub struct CustomBackend {
data: HashMap<(FileType, Id), Vec<u8>>,
}
impl ReadBackend for CustomBackend {
fn location(&self) -> String {
"custom://my-backend".to_string()
}
fn list_with_size(&self, tpe: FileType) -> RusticResult<Vec<(Id, u32)>> {
let items: Vec<_> = self.data
.iter()
.filter(|((t, _), _)| *t == tpe)
.map(|((_, id), data)| (*id, data.len() as u32))
.collect();
Ok(items)
}
fn read_full(&self, tpe: FileType, id: &Id) -> RusticResult<Bytes> {
self.data
.get(&(tpe, *id))
.map(|data| Bytes::from(data.clone()))
.ok_or_else(|| /* error */)
}
fn read_partial(
&self,
tpe: FileType,
id: &Id,
_cacheable: bool,
offset: u32,
length: u32,
) -> RusticResult<Bytes> {
let data = self.data.get(&(tpe, *id))
.ok_or_else(|| /* error */)?;
let start = offset as usize;
let end = start + length as usize;
Ok(Bytes::from(data[start..end].to_vec()))
}
fn warmup_path(&self, tpe: FileType, id: &Id) -> String {
format!("{}/{}", tpe.dirname(), id)
}
}
impl WriteBackend for CustomBackend {
fn write_bytes(
&self,
tpe: FileType,
id: &Id,
_cacheable: bool,
buf: Bytes,
) -> RusticResult<()> {
// Implementation
Ok(())
}
fn remove(
&self,
tpe: FileType,
id: &Id,
_cacheable: bool,
) -> RusticResult<()> {
// Implementation
Ok(())
}
}