From 4542fa5cd0d1d86ca72070e59e5e946d06d0b9f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki?= Date: Sun, 2 Sep 2018 14:43:17 +0100 Subject: [PATCH] add walker --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 46 +++++++++++++++++++++++++++++++++++----------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9801163..e01a5e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -112,6 +112,7 @@ dependencies = [ "dir-diff 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index fb947b5..d786f87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Cyryl PÅ‚otnicki "] [dependencies] +walkdir = "2.2" [dev-dependencies] tempfile = "3.0" diff --git a/src/main.rs b/src/main.rs index 16e44b8..b2752ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,40 @@ extern crate crypto; -extern crate tempfile; extern crate dir_diff; +extern crate tempfile; +extern crate walkdir; use std::path::Path; +use walkdir::DirEntry; +use walkdir::Error; +use walkdir::WalkDir; -struct BackupEngine; -impl BackupEngine { - fn new(source_path: &Path, repository_path: &Path) -> Self { - BackupEngine {} +struct BackupEngine<'a> { + source_path: &'a Path, + repository_path: &'a Path, +} + +impl<'a> BackupEngine<'a> { + fn new(source_path: &'a Path, repository_path: &'a Path) -> Self { + BackupEngine { + source_path, + repository_path, + } } - fn backup(&self) {} + fn backup(&self) -> Result<(), Error> { + let walker = WalkDir::new(self.source_path); + for maybe_entry in walker { + match maybe_entry { + Ok(entry) => self.process_entry(entry), + Err(error) => return Err(error), + } + } + Ok(()) + } + + fn process_entry(&self, entry: DirEntry) { + println!("{:?}", entry.path()); + } } struct RestoreEngine; @@ -30,14 +54,14 @@ mod rustback { mod should { use super::*; + use dir_diff::is_different; + use std::fs::write; use std::fs::File; use std::io::Error; use std::io::{self, Write}; use tempfile::tempdir; use tempfile::tempfile_in; use tempfile::TempDir; - use dir_diff::is_different; - use std::fs::write; #[test] fn be_able_to_restore_backed_up_files() -> Result<(), Error> { @@ -55,11 +79,11 @@ mod rustback { let restore_engine = RestoreEngine::new(&repository.path(), &restore_target.path()); restore_engine.restore(); - let is_source_and_destination_different = is_different(&source.path(), &restore_target.path()).unwrap(); - assert!(!is_source_and_destination_different); + let are_source_and_target_different = + is_different(&source.path(), &restore_target.path()).unwrap(); + assert!(!are_source_and_target_different); Ok(()) } } } -