use std::borrow::Borrow; use heromodels_core::{Index, Model}; use serde::{Deserialize, Serialize}; pub mod hero; pub trait Db { /// Error type returned by database operations. type Error: std::fmt::Debug; /// Open the collection for a specific model. This method must create the collection if it does not exist. fn collection(&self) -> Result, Error>; } /// A collection stores a specific model under a specific key pub trait Collection where K: Serialize, V: Serialize + for<'a> Deserialize<'a>, { /// Error type for database operations type Error: std::fmt::Debug; /// Get all items where the given index field is equal to key. fn get(&self, key: &Q) -> Result, Error> where I: Index, I::Key: Borrow, Q: ToString + ?Sized; /// Get an object from its ID. This does not use an index lookup fn get_by_id(&self, id: u32) -> Result, Error>; /// Store an item in the DB. fn set(&self, value: &V) -> Result<(), Error>; /// Delete all items from the db with a given index. fn delete(&self, key: &Q) -> Result<(), Error> where I: Index, I::Key: Borrow, Q: ToString + ?Sized; /// Delete an object with a given ID fn delete_by_id(&self, id: u32) -> Result<(), Error>; /// Get all objects from the colelction fn get_all(&self) -> Result, Error>; } /// Errors returned by the DB implementation #[derive(Debug)] pub enum Error { /// Error in the underlying database DB(E), /// Error decoding a stored model Decode(bincode::error::DecodeError), /// Error encoding a model for storage Encode(bincode::error::EncodeError), } impl From for Error { fn from(value: bincode::error::DecodeError) -> Self { Error::Decode(value) } } impl From for Error { fn from(value: bincode::error::EncodeError) -> Self { Error::Encode(value) } }