Improve bad Unicode error handling in txt, CSV, and directory handling

This commit is contained in:
Lucien Greathouse
2019-10-09 12:55:24 -07:00
parent 28156bcaf2
commit 9f947ae2c5
3 changed files with 18 additions and 7 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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! {