lazy_db/
lib.rs

1//! > **A simple, bare-bones and lazily loaded database for small projects**
2//! 
3//! ## Concepts
4//! This will be quite short as `lazy-db` is *bare-bones*.
5//! - ### `LazyDB`
6//!     - An obstraction of a `LazyContainer` that makes sure that incompatible databases won't be corrupted by incompatible versions of `lazy-db`
7//!     - The actual 'Lazy Database'
8//! - ### `LazyData`
9//!     - Binary files that only support the primative types; nothing more nothing less
10//! - ### `LazyContainer`
11//!     - A collection of `LazyData`, think of it like an object from `OOP` or a directory in a file system
12//!     - An abstaction of the underlying filesystem directory
13//! 
14//! ## Examples
15//! ### Some basic usage
16//! Here is a really basic `LazyDB` that holds some information about a hypothetical person named *'Dave'*
17//! ```rust
18//! use lazy_db::*;
19//! 
20//! let path = "example_db"; // path to the database
21//! let database = LazyDB::init_db(path).unwrap(); // initialise the database
22//! 
23//! // Writing to the database with a concise macro
24//! // The individual containers are separated by `/` while the `LazyData` is separted with `::`.
25//! // The assigning `=` sign indicates the `LazyData` that is being written to the path
26//! // The function after the `=` sign is formatted like this: new_<primative_type>
27//! write_database!((&database) /people/Dave::fav_colour = new_string("Blue")).unwrap();
28//! write_database!((&database) /people/Dave::age = new_u8(21)).unwrap();
29//! write_database!((&database) /people/Dave::unemployed = new_bool(true)).unwrap();
30//! 
31//! // Reading from the database with a concise macro
32//! // Same path as before
33//! // The search macro only creates a `LazyData` object, you must collect it with a collect function formatted like this: collect_<primative>
34//! let fav_colour: String = search_database!((&database) /people/Dave::fav_colour).unwrap().collect_string().unwrap();
35//! let age: u8 = search_database!((&database) /people/Dave::age).unwrap().collect_u8().unwrap();
36//! let unemployed: bool = search_database!((&database) /people/Dave::unemployed).unwrap().collect_bool().unwrap();
37//! ```
38//! ### A Lazy Object
39//! An example implementation of LazyObject
40//! ```rust
41//! use lazy_db::*;
42//! struct Person {
43//!     container: LazyContainer,
44//!     name: Option<String>,
45//!     age: Option<u8>,
46//! }
47//!
48//! impl LazyObject for Person {
49//!     fn as_container(&self) -> &LazyContainer {
50//!         &self.container
51//!     }
52//!
53//!     fn store_lazy(&self) -> Result<(), LDBError> {
54//!         if let Some(x) = &self.name {
55//!             LazyData::new_string(self.container.data_writer("name")?, x)?;
56//!         };
57//!
58//!         if let Some(x) = self.age {
59//!             LazyData::new_u8(self.container.data_writer("name")?, x)?
60//!         };
61//!
62//!         Ok(())
63//!     }
64//!
65//!     fn load_lazy(container: LazyContainer) -> Self {
66//!         Self {
67//!             container,
68//!             name: None,
69//!             age: None,
70//!         }
71//!     }
72//!
73//!     fn clear_cache(&mut self) {
74//!         self.name = None;
75//!         self.age = None;
76//!     }
77//! }
78//!
79//! impl Drop for Person {
80//!     fn drop(&mut self) {
81//!         let _ = self.store_lazy();
82//!     }
83//! }
84//! ```
85
86pub mod error;
87pub mod lazy_type;
88pub mod lazy_data;
89pub mod version;
90pub mod lazy_database;
91pub mod lazy_container;
92pub mod lazy_trait;
93mod lazy_archive;
94
95// Prelude
96pub use crate::{
97    error::LDBError,
98    lazy_type::*,
99    lazy_data::*,
100    lazy_database::*,
101    lazy_container::*,
102    lazy_trait::*,
103};
104
105pub const VERSION: version::Version = version::Version::new(1, 2, 1);
106
107#[macro_export]
108macro_rules! const_eval {
109    (($type:ty) $code:expr) => {{
110        const RESULT: $type = $code;
111        RESULT
112    }};
113}
114
115#[macro_export]
116macro_rules! unwrap_result {
117    (($result:expr) $err:ident => $wrapper:expr) => {
118        match $result {
119            Ok(x) => x,
120            Err($err) => return Err($wrapper),
121        }
122    }
123}