use serde;
use anyhow::Error;
use bincode::{serialize, deserialize};
use crate::store::Store;
use crate::store::Continue;
use crate::signal::Signaler;
use crate::signal::SigType;
pub trait Model: serde::Serialize +
serde::de::DeserializeOwned {
fn key(&self) -> String;
fn db() -> &'static str { "default" }
fn tob(&self) -> Result<Vec<u8>, Error> {
let encoded: Vec<u8> = serialize(self)?;
Ok(encoded)
}
fn fromb(data: &[u8]) -> Result<Self, Error> {
let decoded: Self = deserialize(data)?;
Ok(decoded)
}
fn store<S: Store>(&self, store: &S)
-> Result<(), Error> {
store.push(Self::db(), &self.key(), self.tob()?)
}
fn store_sig<S: Store, G: Signaler>(&self, store: &S, sig: &G)
-> Result<(), Error> {
self.store(store)
.and_then(|out| {
sig.emit(SigType::Update, &self.key())?;
Ok(out)
})
}
fn delete<S: Store>(&self, store: &S)
-> Result<(), Error> {
store.rm(Self::db(), &self.key())
}
fn delete_sig<S: Store, G: Signaler>(&self, store: &S, sig: &G)
-> Result<(), Error> {
self.delete(store)
.and_then(|out| {
sig.emit(SigType::Delete, &self.key())?;
Ok(out)
})
}
fn get<S: Store>(store: &S, key: &str) -> Result<Self, Error> {
store.pull(Self::db(), key, Self::fromb)
}
fn all<S: Store>(store: &S, prefix: &str)
-> Result<Vec<Self>, Error> {
store.all(Self::db(), prefix, Self::fromb)
}
fn iter<S, F>(store: &S, prefix: &str, f: F) -> Result<(), Error>
where S: Store,
F: Fn(Self) -> Continue {
store.iter(Self::db(), prefix, move |data| {
match Self::fromb(data) {
Ok(obj) => f(obj),
_ => Continue(true)
}
})
}
}