Skip to content

Commit

Permalink
[storage] Construct FileStorage with filename and create a file #53 (#58
Browse files Browse the repository at this point in the history
)

* Update file_storage.rs

* Update file_storage.rs

* Update file_storage.rs

* Update file_storage.rs
  • Loading branch information
michaelvlach authored Aug 20, 2022
1 parent 9f8258b commit bd7fa43
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/storage/file_storage.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
use std::fs::{File, OpenOptions};

#[allow(dead_code)]
#[derive(Default)]
pub(crate) struct FileStorage {}
pub(crate) struct FileStorage {
filename: String,
file: File,
}

impl From<&str> for FileStorage {
fn from(filename: &str) -> Self {
FileStorage::from(filename.to_string())
}
}

impl From<String> for FileStorage {
fn from(filename: String) -> Self {
FileStorage {
file: OpenOptions::new()
.write(true)
.create(true)
.read(true)
.open(&filename)
.unwrap(),
filename,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_utilities::test_file::TestFile;
use std::path::Path;

#[test]
fn create_new_file() {
let test_file = TestFile::from("./file_storage_test01.agdb");
let _storage = FileStorage::from(test_file.file_name().as_str());

assert!(Path::new(test_file.file_name()).exists());
}

#[test]
fn file_storage() {
let _storage = FileStorage::default();
fn open_existing_file() {
let test_file = TestFile::from("./file_storage_test02.agdb");
File::create(test_file.file_name()).unwrap();
let _storage = FileStorage::from(test_file.file_name().clone());
}
}

0 comments on commit bd7fa43

Please sign in to comment.