lazy_db/
lazy_data.rs

1mod reading;
2mod writing;
3mod file_wrapper;
4
5pub use reading::*;
6pub use writing::*;
7pub use file_wrapper::*;
8
9use std::path::{Path, PathBuf};
10use crate::*;
11
12pub struct LazyData {
13    pub path: PathBuf,
14    pub lazy_type: LazyType,
15    wrapper: FileWrapper,
16}
17
18impl LazyData {
19    pub fn load(path: impl AsRef<Path>) -> Result<Self, LDBError> {
20        let path = path.as_ref();
21
22        // Check for the existance of the path and if it's a file
23        if !path.is_file() { return Err(LDBError::FileNotFound(path.to_path_buf())) };
24
25        // Get the reader
26        let mut reader =
27            FileWrapper::new_reader(unwrap_result!((std::fs::File::open(path)) err => LDBError::IOError(err)));
28
29        // Reads the byte repr of it's `LazyType`
30        let lazy_type =
31            LazyType::try_from(reader.read(1)?[0])?;
32
33        Ok(Self {
34            path: path.to_path_buf(),
35            lazy_type,
36            wrapper: reader,
37        })
38    }
39
40    pub fn get_path(&self) -> PathBuf {
41        self.path.clone()
42    }
43}