Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ path = "examples/save_big_data.rs"
name= "load_big_data"
path = "examples/load_big_data.rs"

[features]
default = []
no_std = ["not-io"]

[dependencies]
not-io = { version = "0.1.0-alpha", optional = true }

[dev-dependencies]
serde_json = "1.0"
69 changes: 61 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@
//! ```
//! You can find more examples [here](https://github.com/behemehal/SafeEn/tree/main/examples)

#![no_std]

extern crate alloc;

/// Formatter for tables and types
use core::fmt;
/// FileSystem utilities for saving and loading database
use std::{fs::File, io::Write};
use std::io::BufReader;
// String
use alloc::{string::String, vec::Vec};
/// Database types
use table::{Table, TableRow, TypeDefs, Types};
/// Database table
Expand Down Expand Up @@ -66,7 +71,7 @@ impl Database {
pub fn new() -> Self {
Database {
tables: Vec::new(),
name: "".to_string(),
name: String::new(),
size: 0,
}
}
Expand Down Expand Up @@ -184,6 +189,58 @@ impl Database {
}
}

#[cfg(feature = "no_std")]
fn load_file<T: not_io::Read>(&mut self, file: T) -> Result<(), LoadError> {
let db_name: String = utils::read_data(&mut file, TypeDefs::String).into();
let table_len: u64 = utils::read_data(&mut file, TypeDefs::U64).into();
self.set_name(&db_name);
for _ in 0..table_len {
let table_name: String = utils::read_data(&mut file, TypeDefs::String).into();
let table_headers_len: u64 = utils::read_data(&mut file, TypeDefs::U64).into();

let mut table_rows: Vec<TableRow> = Vec::new();

for _ in 0..table_headers_len {
let table_header: String = utils::read_data(&mut file, TypeDefs::String).into();
let base_header_type: i8 = utils::read_one(&mut file);
let second_header_type: i8 = utils::read_one(&mut file);
let row = TableRow::new(
&table_header,
TypeDefs::from_base_and_second_layer(
base_header_type as u8,
second_header_type as u8,
),
);
table_rows.push(row);
}

//Create table from collected rows
match self.create_table(&table_name, table_rows.clone()) {
Ok(it) => it,
Err(_) => return Err(LoadError),
};

let table_rows_len: u64 = utils::read_data(&mut file, TypeDefs::U64).into();

for _ in 0..table_rows_len {
let mut tables = vec![];
for table_row in &table_rows {
let row_value = utils::read_data(&mut file, table_row.rtype.clone());
tables.push(row_value);
}
match self.table(&table_name) {
Some(it) => match it.insert(tables.clone()) {
Ok(_) => (),
Err(_) => return Err(LoadError),
},
None => return Err(LoadError),
}
}
}
Ok(())
}

#[cfg(not(feature = "no_std"))]
/// Load database from file
/// ## Parameters
/// * `path` - The path to the file
Expand All @@ -192,11 +249,7 @@ impl Database {
/// use safe_en::Database;
/// let db = Database::load("db.sfn");
/// ```
fn load_file(&mut self, path: &str) -> Result<(), LoadError> {
let mut file = match File::open(path) {
Ok(it) => it,
Err(_) => return Err(LoadError),
};
fn load_file<T: std::io::Read>(&mut self, file: T) -> Result<(), LoadError> {
let db_name: String = utils::read_data(&mut file, TypeDefs::String).into();
let table_len: u64 = utils::read_data(&mut file, TypeDefs::U64).into();
self.set_name(&db_name);
Expand Down
12 changes: 10 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ use std::{fs::File, io::Read};
#[derive(Debug)]
pub(crate) struct RawType {
pub type_size: usize,
pub type_data: Vec<u8>,
pub type_data: alloc::vec::Vec<u8>,
}

pub(crate) fn read_one(data: &mut File) -> i8 {
#[cfg(not(feature = "no_std"))]
pub(crate) fn read_one<T: std::io::Read>(data: &mut T) -> i8 {
let mut buffer = [0; 1];
data.read_exact(&mut buffer).unwrap();
buffer[0] as i8
}

#[cfg(feature = "no_std")]
pub(crate) fn read_one<T: not_io::Read>(data: &mut T) -> i8 {
let mut buffer = [0; 1];
data.read_exact(&mut buffer).unwrap();
buffer[0] as i8
Expand Down