From 918e175d20374206397166c02deba35fd30a6e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki?= Date: Sat, 30 Oct 2021 20:41:06 +0100 Subject: [PATCH] Add naive search --- src/repository/mod.rs | 11 +++++++++++ tests/system_tests.rs | 22 +++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/repository/mod.rs b/src/repository/mod.rs index 04876b8..e9acc15 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -160,6 +160,17 @@ impl<'a> Repository { } } + pub fn find_latest_by_path_fragment(&self, path_fragment: &str) -> Result> { + let index_item = self + .index + .newest_items() + .find(|item| item.original_source_path().contains(path_fragment)); + match index_item { + None => Ok(None), + Some(item) => Ok(Some(self.repository_item(&item)?)), + } + } + pub fn newest_items(&self) -> RepositoryItemIterator { RepositoryItemIterator { repository: self, diff --git a/tests/system_tests.rs b/tests/system_tests.rs index fbc8143..2323d99 100644 --- a/tests/system_tests.rs +++ b/tests/system_tests.rs @@ -2,6 +2,7 @@ mod must { use anyhow::Result; use bakare::backup; + use bakare::test::assertions::in_memory::*; use bakare::{repository::Repository, test::source::TestSource}; use tempfile::tempdir; @@ -111,7 +112,26 @@ mod must { assert!(error.is_err()); Ok(()) } - // TODO: index corruption + + #[test] + fn allow_searching_by_filename() -> Result<()> { + let source = TestSource::new().unwrap(); + let dir = tempdir()?; + let repository_path = dir.path(); + Repository::init(repository_path)?; + + backup_file_with_text_contents(&source, repository_path, "first", "first contents")?; + backup_file_with_text_contents(&source, repository_path, "second", "second contents")?; + backup_file_with_text_contents(&source, repository_path, "third", "third contents")?; + + let repository = Repository::open(repository_path)?; + + let second_file = repository.find_latest_by_path_fragment("second")?.unwrap(); + assert_eq!(second_file.original_source_path(), source.file_path("second")?.as_os_str()); + + Ok(()) + } + // TODO: encryption // TODO: resume from sleep while backup in progress }