misc cleanup

This commit is contained in:
Greg 2019-10-22 18:09:27 -04:00
parent 238bda429e
commit 2edc696544
No known key found for this signature in database
GPG key ID: 2E44FAEEDC94B1E2
5 changed files with 17 additions and 63 deletions

View file

@ -2,12 +2,9 @@ use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(
// hide author from help
author = "",
about = "",
raw(setting = "structopt::clap::AppSettings::ColoredHelp"),
raw(setting = "structopt::clap::AppSettings::NextLineHelp"),
raw(setting = "structopt::clap::AppSettings::UnifiedHelpMessage"),
setting(structopt::clap::AppSettings::ColoredHelp),
setting(structopt::clap::AppSettings::NextLineHelp),
setting(structopt::clap::AppSettings::UnifiedHelpMessage)
)]
pub(crate) struct Options {
#[structopt(short = "p", long = "preview")]

View file

@ -1,34 +0,0 @@
pub(crate) struct Error {
// user-facing error output
pub(crate) message: String,
}
impl<T> From<T> for Error
where
T: std::error::Error,
{
fn from(err: T) -> Error {
Error {
message: format!("{}", err),
}
}
}
impl Error {
pub(crate) fn log(err: Self) -> Self {
println!("{}", err);
err
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}

View file

@ -1,4 +1,4 @@
use crate::{utils, Error, Result};
use crate::{err, utils, Result};
use regex::bytes::Regex;
use std::{fs::File, io::prelude::*};
@ -47,23 +47,23 @@ impl Replacer {
match c {
'c' => {
regex.case_insensitive(false);
}
},
'i' => {
regex.case_insensitive(true);
}
},
'm' => {
regex.multi_line(true);
}
},
's' => {
regex.dot_matches_new_line(true);
}
},
'w' => {
regex = regex::bytes::RegexBuilder::new(&format!(
"\\b{}\\b",
look_for
));
}
_ => {}
},
_ => {},
};
}
};
@ -106,9 +106,7 @@ impl Replacer {
let replaced = self.replace(mmap_source.as_ref());
let target = tempfile::NamedTempFile::new_in(
path.parent().ok_or_else(|| Error {
message: "Invalid path given".to_owned(),
})?,
path.parent().ok_or_else(|| err!("Invalid path given"))?,
)?;
let file = target.as_file();
file.set_len(replaced.len() as u64)?;
@ -143,16 +141,16 @@ impl Replacer {
}
Ok(())
}
},
(Source::Files(paths), true) => {
use rayon::prelude::*;
paths
.par_iter()
.map(|p| self.replace_file(p).map_err(Error::log))
.map(|p| self.replace_file(p))
.collect::<Vec<Result<()>>>();
Ok(())
}
},
(Source::Files(paths), false) => {
let stdout = std::io::stdout();
let mut handle = stdout.lock();
@ -167,7 +165,7 @@ impl Replacer {
})
.collect::<Result<Vec<()>>>()?;
Ok(())
}
},
}
}
}
@ -232,5 +230,4 @@ mod tests {
fn full_word_replace() {
replace("abc", "def", false, Some("w"), "abcd abc", "abcd def");
}
}

View file

@ -1,13 +1,9 @@
mod app;
mod error;
mod input;
pub(crate) mod utils;
pub(crate) use self::{
error::Error,
input::{Replacer, Source},
utils::Result,
};
pub(crate) use self::input::{Replacer, Source};
pub(crate) use anyhow::{anyhow as err, Result};
fn main() -> Result<()> {
use structopt::StructOpt;

View file

@ -1,5 +1,3 @@
pub(crate) type Result<T> = std::result::Result<T, crate::Error>;
pub(crate) fn unescape(s: &str) -> Option<String> {
unescape::unescape(s)
}