Skip to content

Commit

Permalink
[storage] Add read() to FileWrapper #86 (#87)
Browse files Browse the repository at this point in the history
* Update file_wrapper.rs

* Update file_wrapper.rs
  • Loading branch information
michaelvlach authored Aug 24, 2022
1 parent bd72a50 commit 40d7608
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/storage/file_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs::{File, OpenOptions};
use std::io::{Seek, SeekFrom};
use std::io::{Read, Seek, SeekFrom};

const ERROR_MESSAGE: &str = "Could not access file";

Expand All @@ -10,6 +10,15 @@ pub(crate) struct FileWrapper {
pub(crate) size: u64,
}

#[allow(dead_code)]
impl FileWrapper {
pub(crate) fn read(&mut self, size: u64) -> Vec<u8> {
let mut buffer = vec![0_u8; size as usize];
self.file.read_exact(&mut buffer).expect(ERROR_MESSAGE);
buffer
}
}

impl From<String> for FileWrapper {
fn from(filename: String) -> Self {
let mut file = OpenOptions::new()
Expand All @@ -32,8 +41,9 @@ impl From<String> for FileWrapper {
#[cfg(test)]
mod tests {
use super::*;
use crate::storage::serialize::Serialize;
use crate::test_utilities::test_file::TestFile;
use std::path::Path;
use std::{io::Write, mem::size_of, path::Path};

#[test]
fn create_new_file() {
Expand All @@ -49,6 +59,20 @@ mod tests {
fn open_existing_file() {
let test_file = TestFile::from("./file_storage_test02.agdb");
File::create(test_file.file_name()).unwrap();
let _storage = FileWrapper::from(test_file.file_name().clone());
let _file = FileWrapper::from(test_file.file_name().clone());
}

#[test]
fn read_bytes() {
let test_file = TestFile::from("./file_storage_test03.agdb");
let mut file = FileWrapper::from(test_file.file_name().clone());
let data = 10_i64.serialize();

file.file.write_all(&data).unwrap();
file.file.seek(SeekFrom::Start(0)).unwrap();

let actual_data = file.read(size_of::<i64>() as u64);

assert_eq!(data, actual_data);
}
}

0 comments on commit 40d7608

Please sign in to comment.