From 3bac3945c31a3fb3cb4eb76a082e3b9436af29fd Mon Sep 17 00:00:00 2001 From: Greg Date: Mon, 30 Dec 2019 16:13:05 -0500 Subject: [PATCH] Use own Error type --- src/error.rs | 15 +++++++++++++++ src/input.rs | 10 +++++----- src/main.rs | 3 ++- 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/error.rs diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..24c61f1 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,15 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum Error { + #[error("invalid regex {0}")] + Regex(#[from] regex::Error), + #[error("{0}")] + File(#[from] std::io::Error), + #[error("failed to move file: {0}")] + TempfilePersist(#[from] tempfile::PersistError), + #[error("file doesn't have parent path: {0}")] + InvalidPath(std::path::PathBuf), +} + +pub type Result = std::result::Result; diff --git a/src/input.rs b/src/input.rs index 42f3844..53a7b81 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,4 +1,4 @@ -use crate::{err, utils, Result}; +use crate::{utils, Error, Result}; use regex::bytes::Regex; use std::{fs::File, io::prelude::*}; @@ -94,10 +94,10 @@ impl Replacer { let mmap_source = unsafe { Mmap::map(&source)? }; let replaced = self.replace(&mmap_source); - let target = - tempfile::NamedTempFile::new_in(path.parent().ok_or_else( - || err!("Invalid path given: {}", path.display()), - )?)?; + let target = tempfile::NamedTempFile::new_in( + path.parent() + .ok_or_else(|| Error::InvalidPath(path.to_path_buf()))?, + )?; let file = target.as_file(); file.set_len(replaced.len() as u64)?; file.set_permissions(meta.permissions())?; diff --git a/src/main.rs b/src/main.rs index 8d56f5c..8753c99 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ mod app; +mod error; mod input; pub(crate) mod utils; pub(crate) use self::input::{Replacer, Source}; -pub(crate) use anyhow::{anyhow as err, Result}; +pub(crate) use error::{Error, Result}; fn main() -> Result<()> { use structopt::StructOpt;