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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
use super::*;
macro_rules! incorrect_type {
($original:expr, $expected:pat) => {
if let $expected = $original {}
else {
return Err(LDBError::IncorrectType($original, stringify!($expected).to_string()));
};
}
}
macro_rules! collect_number {
(($name:ident) $type:ty = $lazy_type:pat) => {
/// ### Expensive Action
/// ( Loads the entire file's data into memory )
///
/// ---
/// Collects the `LazyData` as an unsigned integer.
///
/// Returns `LDBError::IncorrectType` if the LazyData type is not the correct unsigned integer
pub fn $name(self) -> Result<$type, LDBError> {
incorrect_type!(self.lazy_type, $lazy_type);
// Expensive and best to be avoided if possible
let bytes = self.wrapper.read_to_end()?;
const LENGTH: usize = <$type>::BITS as usize / 8usize;
// Check if the size is correct
if bytes.len() != LENGTH {
return Err(LDBError::InvalidNumberByteLength(bytes.len() as u8,
stringify!($lazy_type).to_string()))
};
// Convert to number
let value = <$type>::from_be_bytes(unsafe { *(bytes.as_ptr() as *const [u8; LENGTH]) });
Ok(value)
}
};
(signed ($name:ident) $type:ty = $lazy_type:pat) => {
/// ### Expensive Action
/// ( Loads the entire file's data into memory )
///
/// ---
/// Collects the `LazyData` as a signed integer.
///
/// Returns `LDBError::IncorrectType` if the LazyData type is not the correct signed integer
pub fn $name(self) -> Result<$type, LDBError> {
incorrect_type!(self.lazy_type, $lazy_type);
// Expensive and best to be avoided if possible
let bytes = self.wrapper.read_to_end()?;
const LENGTH: usize = <$type>::BITS as usize / 8usize;
// Check if the size is correct
if bytes.len() != LENGTH {
return Err(LDBError::InvalidNumberByteLength(bytes.len() as u8,
stringify!($lazy_type).to_string()))
};
// Convert to number
let value = <$type>::from_be_bytes(unsafe { *(bytes.as_ptr() as *const [u8; LENGTH]) });
Ok(value)
}
}
}
impl LazyData {
/// ### Expensive Action
/// ( Reads all of the contents of the file and stores it on the heap )
///
/// ---
/// Collects the `LazyData` as a `Box<[u8]>`.
///
/// Returns `LDBError::IncorrectType` if the LazyData type is not `LazyType::Binary`
pub fn collect_binary(self) -> Result<Box<[u8]>, LDBError> {
incorrect_type!(self.lazy_type, LazyType::Binary);
self.wrapper.read_to_end()
}
/// ### Expensive Action
/// ( Loads the entire file's data into memory )
///
/// ---
/// Collects the `LazyData` as a `String`
///
/// Returns `LDBError::IncorrectType` if LazyData type is not `LazyType::String`
/// Returns `LDBError::IOError` if there is an io error while reading file contents
pub fn collect_string(self) -> Result<String, LDBError> {
incorrect_type!(self.lazy_type, LazyType::String);
// Expensive and best to be avoided if possible
let bytes = self.wrapper.read_to_end()?;
if let Ok(x) = String::from_utf8(bytes.to_vec()) {
Ok(x)
} else {
Err(LDBError::InvalidUTF8String(bytes))
}
}
// Unsigned numbers
collect_number!((collect_u8) u8 = LazyType::U8);
collect_number!((collect_u16) u16 = LazyType::U16);
collect_number!((collect_u32) u32 = LazyType::U32);
collect_number!((collect_u64) u64 = LazyType::U64);
collect_number!((collect_u128) u128 = LazyType::U128);
// Signed numbers
collect_number!(signed (collect_i8) i8 = LazyType::I8);
collect_number!(signed (collect_i16) i16 = LazyType::I16);
collect_number!(signed (collect_i32) i32 = LazyType::I32);
collect_number!(signed (collect_i64) i64 = LazyType::I64);
collect_number!(signed (collect_i128) i128 = LazyType::I128);
/* Floating point numbers */
/// ### Expensive Action
/// ( Loads the entire file's data into memory )
///
/// ---
/// Collects the `LazyData` as an `f32`.
///
/// Returns `LDBError::IncorrectType` if the LazyData type is not ``LazyFloat::F32`
pub fn collect_f32(self) -> Result<f32, LDBError> {
incorrect_type!(self.lazy_type, LazyType::F32);
// Expensive and best to be avoided if possible
let bytes = self.wrapper.read_to_end()?;
// Check if the size is correct
if bytes.len() != 4 {
return Err(LDBError::InvalidNumberByteLength(bytes.len() as u8,
stringify!($lazy_type).to_string()))
};
// Convert to number
let value = f32::from_be_bytes(unsafe { *(bytes.as_ptr() as *const [u8; 4]) });
Ok(value)
}
/// ### Expensive Action
/// ( Loads the entire file's data into memory )
///
/// ---
/// Collects the `LazyData` as an `f64`.
///
/// Returns `LDBError::IncorrectType` if the LazyData type is ``LazyFloatType::F64`
pub fn collect_f64(self) -> Result<f64, LDBError> {
incorrect_type!(self.lazy_type, LazyType::F64);
// Expensive and best to be avoided if possible
let bytes = self.wrapper.read_to_end()?;
// Check if the size is correct
if bytes.len() != 8 {
return Err(LDBError::InvalidNumberByteLength(bytes.len() as u8,
stringify!($lazy_type).to_string()))
};
// Convert to number
let value = f64::from_be_bytes(unsafe { *(bytes.as_ptr() as *const [u8; 8]) });
Ok(value)
}
/// ### Inexpensive Action
/// ( Just reads the type field of `LazyData` )
///
/// ---
/// Collects the `LazyData` as a boolean
///
/// Returns `LDBError::IncorrectType` if the type is not of boolean
pub fn collect_bool(self) -> Result<bool, LDBError> {
match self.lazy_type {
LazyType::True => Ok(true),
LazyType::False => Ok(false),
_ => Err(LDBError::IncorrectType(self.lazy_type, String::from("Boolean"))),
}
}
/// ### Inexpensive Action
/// ( Loads `LazyData` specified at path )
///
/// ---
/// Collects the `LazyData` as a path and converts that into another `LazyData`.
pub fn collect_link(self, database: LazyDB) -> Result<LazyData, LDBError> {
incorrect_type!(self.lazy_type, LazyType::Link);
// Loads string as a path
// Expensive and best to be avoided if possible
let bytes = self.wrapper.read_to_end()?;
let string = if let Ok(x) = String::from_utf8(bytes.to_vec()) {
x
} else {
return Err(LDBError::InvalidUTF8String(bytes))
};
database.as_container()?.read_data(string)
}
}