extract source
This commit is contained in:
parent
a19cbf72be
commit
58a08eed7a
5 changed files with 62 additions and 31 deletions
|
@ -8,8 +8,8 @@ license = "AGPL-3.0"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
walkdir = "2.2"
|
walkdir = "2.2"
|
||||||
rust-crypto = "0.2"
|
rust-crypto = "0.2"
|
||||||
|
tempfile = "3.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3.0"
|
|
||||||
rust-crypto = "0.2"
|
rust-crypto = "0.2"
|
||||||
dir-diff = "0.3"
|
dir-diff = "0.3"
|
||||||
|
|
|
@ -10,8 +10,25 @@ pub struct Engine<'a> {
|
||||||
repository_path: &'a Path,
|
repository_path: &'a Path,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait Index {}
|
||||||
|
|
||||||
|
struct InMemoryIndex {}
|
||||||
|
|
||||||
|
impl InMemoryIndex {
|
||||||
|
fn new() -> Self {
|
||||||
|
InMemoryIndex {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Index for InMemoryIndex {}
|
||||||
|
|
||||||
impl<'a> Engine<'a> {
|
impl<'a> Engine<'a> {
|
||||||
pub fn new(source_path: &'a Path, repository_path: &'a Path) -> Self {
|
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 {
|
Engine {
|
||||||
source_path,
|
source_path,
|
||||||
repository_path,
|
repository_path,
|
||||||
|
@ -51,12 +68,26 @@ impl<'a> Engine<'a> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod should {
|
mod should {
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use tempfile::tempdir;
|
||||||
|
|
||||||
|
use crate::source::Source;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn store_file_where_index_tells_it() {
|
fn store_file_where_index_tells_it() -> Result<(), io::Error> {
|
||||||
// fake index, all files stores at the same path
|
let index = FakeIndex {};
|
||||||
|
|
||||||
|
let source = Source::new()?;
|
||||||
|
let repository = tempdir()?;
|
||||||
|
let engine = Engine::new_with_index(source.path(), repository.path(), index);
|
||||||
|
|
||||||
// backup
|
// backup
|
||||||
// see if repo contains one file at the faked path
|
// see if repo contains one file at the faked path
|
||||||
assert!(false);
|
assert!(false);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct FakeIndex {}
|
||||||
|
|
||||||
|
impl Index for FakeIndex {}
|
||||||
}
|
}
|
||||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -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 backup;
|
||||||
pub mod restore;
|
pub mod restore;
|
||||||
|
|
||||||
|
pub mod source;
|
||||||
|
|
||||||
mod storage;
|
mod storage;
|
||||||
|
|
24
src/source.rs
Normal file
24
src/source.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,14 +2,14 @@ use bakare::backup;
|
||||||
use bakare::restore;
|
use bakare::restore;
|
||||||
use bakare::restore::WhatToRestore::SpecificPath;
|
use bakare::restore::WhatToRestore::SpecificPath;
|
||||||
|
|
||||||
|
use bakare::source::Source;
|
||||||
|
|
||||||
use dir_diff::is_different;
|
use dir_diff::is_different;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Error;
|
use std::io::Error;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::io::Write;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
use tempfile::TempDir;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn restore_backed_up_files() -> Result<(), Error> {
|
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);
|
assert!(!are_source_and_target_different);
|
||||||
Ok(())
|
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue