use chrono::{DateTime, Utc};
use serde::{de::DeserializeOwned, ser::Serialize};
use sled::IVec;
use std::collections::HashSet;
use crate::{encoding::Encoding, error::Result, expiring, structured};
pub trait DbExt {
fn open_expiring_tree<V, E, F>(&self, name: &str) -> expiring::TreeBuilder<V, E, F>
where
E: Encoding<HashSet<IVec>> + Encoding<DateTime<Utc>> + 'static,
F: Encoding<V> + 'static;
fn open_structured_tree<V, E>(&self, name: &str) -> Result<structured::Tree<V, E>>
where
E: Encoding<V> + 'static;
fn open_expiring_structured_tree<V, E>(
&self,
name: &str,
) -> structured::expiring::TreeBuilder<V, E>
where
E: Encoding<V> + Encoding<HashSet<IVec>> + Encoding<DateTime<Utc>> + 'static,
V: DeserializeOwned + Serialize + 'static,
{
self.open_expiring_tree(name)
}
fn open_expiring_plain_tree<E>(&self, name: &str) -> expiring::plain::TreeBuilder<E>
where
E: Encoding<HashSet<IVec>> + Encoding<DateTime<Utc>> + 'static,
{
self.open_expiring_tree(name)
}
#[cfg(feature = "bincode")]
fn open_bincode_tree<V>(&self, name: &str) -> Result<crate::bincode::Tree<V>>
where
V: DeserializeOwned + Serialize + 'static,
{
self.open_structured_tree(name)
}
#[cfg(feature = "bincode")]
fn open_expiring_bincode_tree<V>(&self, name: &str) -> crate::bincode::expiring::TreeBuilder<V>
where
V: DeserializeOwned + Serialize + 'static,
{
self.open_expiring_tree(name)
}
#[cfg(feature = "bincode")]
fn open_expiring_plain_bincode_tree(
&self,
name: &str,
) -> expiring::plain::TreeBuilder<crate::bincode::BincodeEncoding> {
self.open_expiring_tree(name)
}
#[cfg(feature = "cbor")]
fn open_cbor_tree<V>(&self, name: &str) -> Result<crate::cbor::Tree<V>>
where
V: DeserializeOwned + Serialize + 'static,
{
self.open_structured_tree(name)
}
#[cfg(feature = "cbor")]
fn open_expiring_cbor_tree<V>(&self, name: &str) -> crate::cbor::expiring::TreeBuilder<V>
where
V: DeserializeOwned + Serialize + 'static,
{
self.open_expiring_tree(name)
}
#[cfg(feature = "cbor")]
fn open_expiring_plain_cbor_tree(
&self,
name: &str,
) -> expiring::plain::TreeBuilder<crate::cbor::CborEncoding> {
self.open_expiring_tree(name)
}
#[cfg(feature = "json")]
fn open_json_tree<V>(&self, name: &str) -> Result<crate::json::Tree<V>>
where
V: DeserializeOwned + Serialize + 'static,
{
self.open_structured_tree(name)
}
#[cfg(feature = "json")]
fn open_expiring_json_tree<V>(&self, name: &str) -> crate::json::expiring::TreeBuilder<V>
where
V: DeserializeOwned + Serialize + 'static,
{
self.open_expiring_tree(name)
}
#[cfg(feature = "json")]
fn open_expiring_plain_json_tree(
&self,
name: &str,
) -> expiring::plain::TreeBuilder<crate::json::JsonEncoding> {
self.open_expiring_tree(name)
}
}
impl DbExt for sled::Db {
fn open_expiring_tree<V, E, F>(&self, name: &str) -> expiring::TreeBuilder<V, E, F>
where
E: Encoding<HashSet<IVec>> + Encoding<DateTime<Utc>> + 'static,
F: Encoding<V> + 'static,
{
expiring::TreeBuilder::new(self, name)
}
fn open_structured_tree<V, E>(&self, name: &str) -> Result<structured::Tree<V, E>>
where
E: Encoding<V> + 'static,
{
structured::Tree::new(self, name)
}
}