Use own Error type

This commit is contained in:
Greg 2019-12-30 16:13:05 -05:00
parent 4a9ddeaebd
commit 3bac3945c3
No known key found for this signature in database
GPG key ID: 2E44FAEEDC94B1E2
3 changed files with 22 additions and 6 deletions

15
src/error.rs Normal file
View file

@ -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<T, E = Error> = std::result::Result<T, E>;

View file

@ -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())?;

View file

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