lazy-db

A simple, bare-bones and lazily loaded database for small projects

## Examples

Some basic usage

Here is a really basic LazyDB that holds some information about a hypothetical person named 'Dave' ```rust use lazy_db::*;

let path = "exampledb"; // path to the database let database = LazyDB::initdb(path).unwrap(); // initialise the database

// Writing to the database with a concise macro // The individual containers are separated by / while the LazyData is separted with ::. // The assigning = sign indicates the LazyData that is being written to the path // The function after the = sign is formatted like this: new writedatabase!((&database) /people/Dave::favcolour = newstring("Blue")).unwrap(); writedatabase!((&database) /people/Dave::age = newu8(21)).unwrap(); writedatabase!((&database) /people/Dave::unemployed = newbool(true)).unwrap();

// Reading from the database with a concise macro // Same path as before // The search macro only creates a LazyData object, you must collect it with a collect function formatted like this: collect let favcolour: String = searchdatabase!((&database) /people/Dave::favcolour).unwrap().collectstring().unwrap(); let age: u8 = searchdatabase!((&database) /people/Dave::age).unwrap().collectu8().unwrap(); let unemployed: bool = searchdatabase!((&database) /people/Dave::unemployed).unwrap().collect_bool().unwrap(); ```

A Lazy Object

An example implementation of LazyObject ```rust use lazy_db::*; struct Person { container: LazyContainer, name: Option, age: Option, }

impl LazyObject for Person { fn as_container(&self) -> &LazyContainer { &self.container }

fn store_lazy(&self) -> Result<(), LDBError> {
    if let Some(x) = &self.name {
        LazyData::new_string(self.container.data_writer("name")?, x)?;
    };

    if let Some(x) = self.age {
        LazyData::new_u8(self.container.data_writer("name")?, x)?
    };

    Ok(())
}

fn load_lazy(container: LazyContainer) -> Self {
    Self {
        container,
        name: None,
        age: None,
    }
}

fn clear_cache(&mut self) {
    self.name = None;
    self.age = None;
}

}

impl Drop for Person { fn drop(&mut self) { let _ = self.store_lazy(); } } ```