1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use crate::{game::AnkiDB, storage::Storage};

///A dummy database - works only in memory
#[derive(Default)]
pub struct DummyStorage(AnkiDB);

impl Storage for DummyStorage {
    type ErrorType = ();

    fn read_db(&self) -> Result<AnkiDB, Self::ErrorType> {
        Ok(self.0.clone())
    }

    fn write_db(&mut self, db: &AnkiDB) -> Result<(), Self::ErrorType> {
        self.0 = db.clone();
        Ok(())
    }
}

///Trait for [`Storage`] that implements methods that take `dyn` trait objects over owned `self`s.
pub trait DynStorage<E: std::fmt::Debug> {
    ///Reads the database from `S` and sets the owned database from `&mut self` to that read in database.
    fn read_custom(&mut self, s: &dyn Storage<ErrorType = E>) -> Result<(), E>;

    ///Writes the owned database to `S`
    fn write_custom(&mut self, s: &mut dyn Storage<ErrorType = E>) -> Result<(), E>;

    ///Exits the application
    fn exit_custom(&mut self, s: &mut dyn Storage<ErrorType = E>);
}