Mark the unwrap and Sweep the unwrap a bit more.

This commit is contained in:
Andrés N. Robalino 2019-08-21 10:48:04 -05:00
parent 8d5fd6f379
commit 1e8793135a
3 changed files with 27 additions and 17 deletions

View file

@ -79,15 +79,19 @@ fn cp(
if entry.is_file() { if entry.is_file() {
let strategy = |(source_file, _depth_level)| { let strategy = |(source_file, _depth_level)| {
if destination.exists() { if destination.exists() {
let mut new_dst = dunce::canonicalize(destination.clone()).unwrap(); let mut new_dst = dunce::canonicalize(destination.clone())?;
new_dst.push(entry.file_name().unwrap()); if let Some(name) = entry.file_name() {
(source_file, new_dst) new_dst.push(name);
}
Ok((source_file, new_dst))
} else { } else {
(source_file, destination.clone()) Ok((source_file, destination.clone()))
} }
}; };
for (ref src, ref dst) in sources.paths_applying_with(strategy) { let sources = sources.paths_applying_with(strategy)?;
for (ref src, ref dst) in sources {
if src.is_file() { if src.is_file() {
match std::fs::copy(src, dst) { match std::fs::copy(src, dst) {
Err(e) => { Err(e) => {
@ -118,7 +122,7 @@ fn cp(
let strategy = |(source_file, depth_level)| { let strategy = |(source_file, depth_level)| {
let mut new_dst = destination.clone(); let mut new_dst = destination.clone();
let path = dunce::canonicalize(&source_file).unwrap(); let path = dunce::canonicalize(&source_file)?;
let mut comps: Vec<_> = path let mut comps: Vec<_> = path
.components() .components()
@ -133,10 +137,12 @@ fn cp(
new_dst.push(fragment); new_dst.push(fragment);
} }
(PathBuf::from(&source_file), PathBuf::from(new_dst)) Ok((PathBuf::from(&source_file), PathBuf::from(new_dst)))
}; };
for (ref src, ref dst) in sources.paths_applying_with(strategy) { let sources = sources.paths_applying_with(strategy)?;
for (ref src, ref dst) in sources {
if src.is_dir() { if src.is_dir() {
if !dst.exists() { if !dst.exists() {
match std::fs::create_dir_all(dst) { match std::fs::create_dir_all(dst) {
@ -189,8 +195,8 @@ fn cp(
}; };
let strategy = |(source_file, depth_level)| { let strategy = |(source_file, depth_level)| {
let mut new_dst = dunce::canonicalize(&destination).unwrap(); let mut new_dst = dunce::canonicalize(&destination)?;
let path = dunce::canonicalize(&source_file).unwrap(); let path = dunce::canonicalize(&source_file)?;
let mut comps: Vec<_> = path let mut comps: Vec<_> = path
.components() .components()
@ -205,10 +211,12 @@ fn cp(
new_dst.push(fragment); new_dst.push(fragment);
} }
(PathBuf::from(&source_file), PathBuf::from(new_dst)) Ok((PathBuf::from(&source_file), PathBuf::from(new_dst)))
}; };
for (ref src, ref dst) in sources.paths_applying_with(strategy) { let sources = sources.paths_applying_with(strategy)?;
for (ref src, ref dst) in sources {
if src.is_dir() { if src.is_dir() {
if !dst.exists() { if !dst.exists() {
match std::fs::create_dir_all(dst) { match std::fs::create_dir_all(dst) {

View file

@ -181,7 +181,7 @@ fn mv(
let strategy = |(source_file, depth_level)| { let strategy = |(source_file, depth_level)| {
let mut new_dst = destination.clone(); let mut new_dst = destination.clone();
let path = dunce::canonicalize(&source_file).unwrap(); let path = dunce::canonicalize(&source_file)?;
let mut comps: Vec<_> = path let mut comps: Vec<_> = path
.components() .components()
@ -196,10 +196,12 @@ fn mv(
new_dst.push(fragment); new_dst.push(fragment);
} }
(PathBuf::from(&source_file), PathBuf::from(new_dst)) Ok((PathBuf::from(&source_file), PathBuf::from(new_dst)))
}; };
for (ref src, ref dst) in sources.paths_applying_with(strategy) { let sources = sources.paths_applying_with(strategy)?;
for (ref src, ref dst) in sources {
if src.is_dir() { if src.is_dir() {
if !dst.exists() { if !dst.exists() {
match std::fs::create_dir_all(dst) { match std::fs::create_dir_all(dst) {

View file

@ -105,9 +105,9 @@ impl FileStructure {
self.root = path.to_path_buf(); self.root = path.to_path_buf();
} }
pub fn paths_applying_with<F>(&mut self, to: F) -> Vec<(PathBuf, PathBuf)> pub fn paths_applying_with<F>(&mut self, to: F) -> Result<Vec<(PathBuf, PathBuf)>, Box<dyn std::error::Error>>
where where
F: Fn((PathBuf, usize)) -> (PathBuf, PathBuf), F: Fn((PathBuf, usize)) -> Result<(PathBuf, PathBuf), Box<dyn std::error::Error>>,
{ {
self.resources self.resources
.iter() .iter()