#[cfg(feature = "csv")]
mod csv;
#[cfg(feature = "csv")]
pub use csv::CsvAdapter;
#[cfg(feature = "json")]
mod json;
#[cfg(feature = "json")]
pub use json::{JsonAdapter, JsonPartitionedAdapter};
#[cfg(feature = "ron")]
mod ron;
#[cfg(feature = "ron")]
pub use ron::{RonAdapter, RonPartitionedAdapter};
use crate::{JoydbError, state::State};
use crate::{Model, Relation};
use std::marker::PhantomData;
use std::path::Path;
mod fs_utils;
pub trait Adapter: Send + 'static {
type Target: BlanketAdapter<Target = Self>;
fn write_state<S: State>(&self, state: &S) -> Result<(), JoydbError> {
Self::Target::write_state(self, state)
}
fn load_state<S: State>(&self) -> Result<S, JoydbError> {
Self::Target::load_state(self)
}
}
pub trait BlanketAdapter {
type Target;
fn write_state<S: State>(target: &Self::Target, state: &S) -> Result<(), JoydbError>;
fn load_state<S: State>(target: &Self::Target) -> Result<S, JoydbError>;
}
pub struct Unified<UA: UnifiedAdapter>(PhantomData<UA>);
impl<UA: UnifiedAdapter> BlanketAdapter for Unified<UA> {
type Target = UA;
fn write_state<S: State>(target: &UA, state: &S) -> Result<(), JoydbError> {
target.write_state(state)
}
fn load_state<S: State>(target: &UA) -> Result<S, JoydbError> {
target.load_state()
}
}
pub struct Partitioned<PA: PartitionedAdapter>(PhantomData<PA>);
impl<PA: PartitionedAdapter> BlanketAdapter for Partitioned<PA> {
type Target = PA;
fn write_state<S: State>(target: &PA, state: &S) -> Result<(), JoydbError> {
S::write_with_partitioned_adapter(state, target)
}
fn load_state<S: State>(target: &PA) -> Result<S, JoydbError> {
target.load_state()
}
}
pub trait UnifiedAdapter {
fn write_state<S: State>(&self, state: &S) -> Result<(), JoydbError>;
fn load_state<S: State>(&self) -> Result<S, JoydbError>;
}
pub trait PartitionedAdapter {
fn write_relation<M: Model>(&self, relation: &Relation<M>) -> Result<(), JoydbError>;
fn load_state<S: State>(&self) -> Result<S, JoydbError>;
fn load_relation<M: Model>(&self) -> Result<Relation<M>, JoydbError>;
}
pub trait FromPath {
fn from_path<P: AsRef<Path>>(path: P) -> Self;
}