From d706826279838cc0bd3d3235034ce2bea0dd201f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki?= Date: Sun, 8 Nov 2020 14:47:45 +0000 Subject: [PATCH] Remove all unwraps from production code --- src/index/io.rs | 22 +++++++++++++++++----- src/repository.rs | 5 ++++- src/repository_item.rs | 4 +++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/index/io.rs b/src/index/io.rs index a4052d8..dde0944 100644 --- a/src/index/io.rs +++ b/src/index/io.rs @@ -34,9 +34,9 @@ impl Index { pub fn save(&mut self) -> Result<()> { let lock_id = Uuid::new_v4(); - let lock = Lock::new(&self.index_directory())?; + let lock = Lock::new(&self.index_directory()?)?; if self.index_file_path().exists() { - let index = Index::load_from_file(&Index::index_file_path_for_repository_path(&self.index_directory()))?; + let index = Index::load_from_file(&Index::index_file_path_for_repository_path(&self.index_directory()?))?; self.merge_items_by_file_id(index.items_by_file_id); self.merge_newest_items(index.newest_items_by_source_path); self.version = max(self.version.clone(), index.version); @@ -52,7 +52,12 @@ impl Index { where T: AsRef, { - fs::create_dir_all(path.as_ref().parent().unwrap()).context("create index directory")?; + fs::create_dir_all( + path.as_ref() + .parent() + .ok_or(anyhow!("cannot compute parent path for {}", path.as_ref().to_string_lossy()))?, + ) + .context("create index directory")?; let file = AtomicFile::new(&path, AllowOverwrite); @@ -99,7 +104,14 @@ impl Index { path.join("index") } - fn index_directory(&self) -> PathBuf { - self.index_file_path().parent().unwrap().to_path_buf() + fn index_directory(&self) -> Result { + Ok(self + .index_file_path() + .parent() + .ok_or(anyhow!( + "cannot compute parent path for {}", + self.index_file_path().to_string_lossy() + ))? + .to_path_buf()) } } diff --git a/src/repository.rs b/src/repository.rs index 02a4291..0b7bfe7 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -113,7 +113,10 @@ impl<'a> Repository<'a> { let destination_path = Path::new(&destination_path); if source_path.is_file() { - let parent = destination_path.parent().unwrap(); + let parent = destination_path.parent().ok_or(anyhow!( + "cannot compute parent path for {}", + &destination_path.to_string_lossy() + ))?; fs::create_dir_all(parent)?; fs::copy(source_path, destination_path)?; let relative_path = destination_path.strip_prefix(self.path)?; diff --git a/src/repository_item.rs b/src/repository_item.rs index 23f9d83..86df6d7 100644 --- a/src/repository_item.rs +++ b/src/repository_item.rs @@ -34,7 +34,9 @@ impl RepositoryItem { if !target_path.is_absolute() { return Err(anyhow!("path to store not absolute")); } - let parent = target_path.parent().unwrap(); + let parent = target_path + .parent() + .ok_or(anyhow!("cannot compute parent path for {}", &target_path.to_string_lossy()))?; if !parent.exists() { fs::create_dir_all(parent)?; }