mirror of
https://github.com/chmln/sd
synced 2024-11-25 12:40:16 +00:00
Use own Error type
This commit is contained in:
parent
4a9ddeaebd
commit
3bac3945c3
3 changed files with 22 additions and 6 deletions
15
src/error.rs
Normal file
15
src/error.rs
Normal 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>;
|
10
src/input.rs
10
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())?;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue