Skip to content

Commit

Permalink
Add path to more IO errors for context
Browse files Browse the repository at this point in the history
  • Loading branch information
jwollen committed Feb 9, 2021
1 parent 1e51b65 commit 89441d7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
9 changes: 8 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::result;

use tera;
Expand All @@ -12,6 +12,13 @@ pub(crate) fn new_error(kind: ErrorKind) -> Error {
Error { kind, source: None }
}

/// Map an IO error, providing a path for context.
pub(crate) fn map_io_err<T>(err: io::Result<T>, path: &Path) -> Result<T> {
err.map_err(|err| {
new_error(ErrorKind::Io { err, path: path.to_path_buf() })
})
}

/// A type alias for `Result<T, kickstart::Error>`.
pub type Result<T> = result::Result<T, Error>;

Expand Down
14 changes: 4 additions & 10 deletions src/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use toml;
use walkdir::WalkDir;

use crate::definition::TemplateDefinition;
use crate::errors::{new_error, ErrorKind, Result};
use crate::errors::{map_io_err, new_error, ErrorKind, Result};
use crate::utils::{create_directory, get_source, is_binary, read_file, write_file, Source};

/// The current template being generated
Expand Down Expand Up @@ -148,9 +148,7 @@ impl Template {
let no_render = patterns.iter().map(|p| p.matches_path(&real_path)).any(|x| x);

if no_render || is_binary(&buffer) {
fs::copy(&entry.path(), &real_path).map_err(|err| {
new_error(ErrorKind::Io { err, path: entry.path().to_path_buf() })
})?;
map_io_err(fs::copy(&entry.path(), &real_path), entry.path())?;
continue;
}

Expand All @@ -170,13 +168,9 @@ impl Template {
continue;
}
if path_to_delete.is_dir() {
fs::remove_dir_all(&path_to_delete).map_err(|err| {
new_error(ErrorKind::Io { err, path: path_to_delete.to_path_buf() })
})?;
map_io_err(fs::remove_dir_all(&path_to_delete), &path_to_delete)?;
} else {
fs::remove_file(&path_to_delete).map_err(|err| {
new_error(ErrorKind::Io { err, path: path_to_delete.to_path_buf() })
})?;
map_io_err(fs::remove_file(&path_to_delete), &path_to_delete)?;
}
}
}
Expand Down
19 changes: 7 additions & 12 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};

use memchr::memchr;

use crate::errors::{new_error, ErrorKind, Result};
use crate::errors::{map_io_err, Result};

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Source {
Expand All @@ -13,29 +13,24 @@ pub enum Source {
}

pub fn read_file(p: &Path) -> Result<String> {
let mut f = match File::open(p) {
Ok(f) => f,
Err(err) => return Err(new_error(ErrorKind::Io { err, path: p.to_path_buf() })),
};
let mut f = map_io_err(File::open(p), p)?;

let mut contents = String::new();
match f.read_to_string(&mut contents) {
Ok(_) => (),
Err(err) => return Err(new_error(ErrorKind::Io { err, path: p.to_path_buf() })),
};
map_io_err(f.read_to_string(&mut contents), p)?;

Ok(contents)
}

pub fn write_file(p: &Path, contents: &str) -> Result<()> {
let mut f = File::create(p)?;
f.write_all(contents.as_bytes())?;
let mut f = map_io_err(File::create(p), p)?;
map_io_err(f.write_all(contents.as_bytes()), p)?;

Ok(())
}

pub fn create_directory(path: &Path) -> Result<()> {
if !path.exists() {
create_dir_all(path)?;
map_io_err(create_dir_all(path), path)?;
}

Ok(())
Expand Down

0 comments on commit 89441d7

Please sign in to comment.