diff --git a/src/snapshot_middleware/csv.rs b/src/snapshot_middleware/csv.rs index 3b38c3aa..fe3e6753 100644 --- a/src/snapshot_middleware/csv.rs +++ b/src/snapshot_middleware/csv.rs @@ -10,6 +10,7 @@ use crate::{ }; use super::{ + error::SnapshotError, meta_file::AdjacentMetadata, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, }; @@ -25,9 +26,14 @@ impl SnapshotMiddleware for SnapshotCsv { return Ok(None); } - let file_name = entry.path().file_name().unwrap().to_string_lossy(); + let extension = match entry.path().extension() { + Some(x) => x + .to_str() + .ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?, + None => return Ok(None), + }; - if !file_name.ends_with(".csv") { + if extension != "csv" { return Ok(None); } @@ -35,7 +41,8 @@ impl SnapshotMiddleware for SnapshotCsv { .path() .file_stem() .expect("Could not extract file stem") - .to_string_lossy() + .to_str() + .ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))? .to_string(); let meta_path = entry diff --git a/src/snapshot_middleware/dir.rs b/src/snapshot_middleware/dir.rs index 9c365594..970cc92b 100644 --- a/src/snapshot_middleware/dir.rs +++ b/src/snapshot_middleware/dir.rs @@ -8,6 +8,7 @@ use crate::{ }; use super::{ + error::SnapshotError, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, snapshot_from_imfs, snapshot_from_instance, }; @@ -38,7 +39,7 @@ impl SnapshotMiddleware for SnapshotDir { .file_name() .expect("Could not extract file name") .to_str() - .unwrap() + .ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))? .to_string(); Ok(Some(InstanceSnapshot { diff --git a/src/snapshot_middleware/txt.rs b/src/snapshot_middleware/txt.rs index f76e4e6d..a939a100 100644 --- a/src/snapshot_middleware/txt.rs +++ b/src/snapshot_middleware/txt.rs @@ -9,6 +9,7 @@ use crate::{ }; use super::{ + error::SnapshotError, meta_file::AdjacentMetadata, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, }; @@ -25,7 +26,9 @@ impl SnapshotMiddleware for SnapshotTxt { } let extension = match entry.path().extension() { - Some(x) => x.to_str().unwrap(), + Some(x) => x + .to_str() + .ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?, None => return Ok(None), }; @@ -38,12 +41,12 @@ impl SnapshotMiddleware for SnapshotTxt { .file_stem() .expect("Could not extract file stem") .to_str() - .unwrap() + .ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))? .to_string(); let contents = entry.contents(imfs)?; let contents_str = str::from_utf8(contents) - .expect("File content was not valid UTF-8") + .map_err(|err| SnapshotError::file_contents_bad_unicode(err, entry.path()))? .to_string(); let properties = hashmap! {