use std::fmt::{self, Debug};
use std::fs::File;
use std::path::PathBuf;
use crate::models::{Task, Todo};
use crate::Action;
use super::{db, fs};
pub trait Persister: fmt::Debug {
fn boxed(self) -> Box<dyn Persister>;
fn to_string(&self) -> String;
fn create(&self) -> crate::Result<()>;
fn exists(&self) -> crate::Result<bool>;
fn view(&self) -> crate::Result<()>;
fn tasks(&self) -> crate::Result<Vec<Task>>;
fn edit(&self, todo: &Todo, ids: &[u32], action: &Action) -> crate::Result<()>;
fn save(&self, todo: &Todo) -> crate::Result<()>;
fn replace(&self, todo: &Todo) -> crate::Result<()>;
fn clean(&self) -> crate::Result<()>;
fn remove(&self) -> crate::Result<()>;
}
impl PartialEq for Box<dyn Persister> {
#[inline]
fn eq(&self, other: &Self) -> bool {
(self.to_string() == other.to_string()) && (self.tasks().unwrap() == other.tasks().unwrap())
}
}
impl Clone for Box<dyn Persister> {
#[inline]
fn clone(&self) -> Self {
crate::Postit::get_persister(Some(self.to_string())).unwrap()
}
}
pub trait FilePersister: Debug {
fn boxed(self) -> Box<dyn FilePersister>;
fn path(&self) -> &PathBuf;
fn default(&self) -> String;
fn tasks(&self) -> fs::Result<Vec<Task>>;
fn open(&self) -> fs::Result<File>;
fn write(&self, todo: &Todo) -> fs::Result<()>;
fn clean(&self) -> fs::Result<()>;
fn remove(&self) -> fs::Result<()>;
}
impl PartialEq for Box<dyn FilePersister> {
#[inline]
fn eq(&self, other: &Self) -> bool {
(self.path() == other.path()) && (self.tasks().unwrap() == other.tasks().unwrap())
}
}
pub trait DbPersister: Debug {
fn boxed(self) -> Box<dyn DbPersister>;
fn conn(&self) -> String;
fn table(&self) -> String;
fn database(&self) -> String;
fn exists(&self) -> db::Result<bool>;
fn tasks(&self) -> db::Result<Vec<Task>>;
fn count(&self) -> db::Result<u32>;
fn create(&self) -> db::Result<()>;
fn insert(&self, todo: &Todo) -> db::Result<()>;
fn update(&self, todo: &Todo, ids: &[u32], action: &Action) -> db::Result<()>;
fn delete(&self, ids: &[u32]) -> db::Result<()>;
fn drop_table(&self) -> db::Result<()>;
fn drop_database(&self) -> db::Result<()>;
fn clean(&self) -> db::Result<()>;
}
impl PartialEq for Box<dyn DbPersister> {
#[inline]
fn eq(&self, other: &Self) -> bool {
(self.conn() == other.conn()) && (self.tasks().unwrap() == other.tasks().unwrap())
}
}