extract source

This commit is contained in:
Cyryl Płotnicki 2018-12-22 16:09:22 +00:00
parent a19cbf72be
commit 58a08eed7a
5 changed files with 62 additions and 31 deletions

View file

@ -8,8 +8,8 @@ license = "AGPL-3.0"
[dependencies]
walkdir = "2.2"
rust-crypto = "0.2"
tempfile = "3.0"
[dev-dependencies]
tempfile = "3.0"
rust-crypto = "0.2"
dir-diff = "0.3"

View file

@ -10,8 +10,25 @@ pub struct Engine<'a> {
repository_path: &'a Path,
}
trait Index {}
struct InMemoryIndex {}
impl InMemoryIndex {
fn new() -> Self {
InMemoryIndex {}
}
}
impl Index for InMemoryIndex {}
impl<'a> Engine<'a> {
pub fn new(source_path: &'a Path, repository_path: &'a Path) -> Self {
let index = InMemoryIndex::new();
Engine::new_with_index(source_path, repository_path, index)
}
fn new_with_index(source_path: &'a Path, repository_path: &'a Path, index: impl Index) -> Self {
Engine {
source_path,
repository_path,
@ -51,12 +68,26 @@ impl<'a> Engine<'a> {
#[cfg(test)]
mod should {
use super::*;
use tempfile::tempdir;
use crate::source::Source;
#[test]
fn store_file_where_index_tells_it() {
// fake index, all files stores at the same path
fn store_file_where_index_tells_it() -> Result<(), io::Error> {
let index = FakeIndex {};
let source = Source::new()?;
let repository = tempdir()?;
let engine = Engine::new_with_index(source.path(), repository.path(), index);
// backup
// see if repo contains one file at the faked path
assert!(false);
Ok(())
}
struct FakeIndex {}
impl Index for FakeIndex {}
}

View file

@ -1,12 +1,6 @@
extern crate core;
extern crate crypto;
#[cfg(test)]
extern crate dir_diff;
#[cfg(test)]
extern crate tempfile;
extern crate walkdir;
pub mod backup;
pub mod restore;
pub mod source;
mod storage;

24
src/source.rs Normal file
View file

@ -0,0 +1,24 @@
use std::fs::File;
use std::io::Error;
use std::io::Write;
use std::path::Path;
use tempfile::tempdir;
use tempfile::TempDir;
pub struct Source {
directory: TempDir,
}
impl Source {
pub fn new() -> Result<Self, Error> {
Ok(Self { directory: tempdir()? })
}
pub fn write_text_to_file(&self, filename: &str, text: &str) -> Result<(), Error> {
Ok(File::create(self.directory.path().join(filename))?.write_all(text.as_bytes())?)
}
pub fn path(&self) -> &Path {
self.directory.path()
}
}

View file

@ -2,14 +2,14 @@ use bakare::backup;
use bakare::restore;
use bakare::restore::WhatToRestore::SpecificPath;
use bakare::source::Source;
use dir_diff::is_different;
use std::fs::File;
use std::io::Error;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use tempfile::tempdir;
use tempfile::TempDir;
#[test]
fn restore_backed_up_files() -> Result<(), Error> {
@ -70,21 +70,3 @@ fn assert_same_after_restore(source_path: &Path, repository_path: &Path) -> Resu
assert!(!are_source_and_target_different);
Ok(())
}
struct Source {
directory: TempDir,
}
impl Source {
fn new() -> Result<Self, Error> {
Ok(Self { directory: tempdir()? })
}
fn write_text_to_file(&self, filename: &str, text: &str) -> Result<(), Error> {
Ok(File::create(self.directory.path().join(filename))?.write_all(text.as_bytes())?)
}
fn path(&self) -> &Path {
self.directory.path()
}
}