mirror of
https://github.com/chmln/sd
synced 2024-11-22 03:03:03 +00:00
Fix warnings and clippy lints (#185)
* Fix `cargo check` warnings * Placate clippy * Invert conditional
This commit is contained in:
parent
9d1f97bc97
commit
c4a50a2b8f
5 changed files with 24 additions and 28 deletions
|
@ -6,7 +6,7 @@ use structopt::{clap::AppSettings, StructOpt};
|
||||||
setting(AppSettings::NextLineHelp),
|
setting(AppSettings::NextLineHelp),
|
||||||
setting(AppSettings::UnifiedHelpMessage)
|
setting(AppSettings::UnifiedHelpMessage)
|
||||||
)]
|
)]
|
||||||
pub(crate) struct Options {
|
pub struct Options {
|
||||||
#[structopt(short = "p", long = "preview")]
|
#[structopt(short = "p", long = "preview")]
|
||||||
/// Output result into stdout and do not modify files.
|
/// Output result into stdout and do not modify files.
|
||||||
pub preview: bool,
|
pub preview: bool,
|
||||||
|
|
|
@ -72,8 +72,7 @@ impl App {
|
||||||
let print_path = paths.len() > 1;
|
let print_path = paths.len() > 1;
|
||||||
|
|
||||||
paths.iter().try_for_each(|path| {
|
paths.iter().try_for_each(|path| {
|
||||||
if let Err(_) = Replacer::check_not_empty(File::open(path)?)
|
if Replacer::check_not_empty(File::open(path)?).is_err() {
|
||||||
{
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let file =
|
let file =
|
||||||
|
|
|
@ -14,7 +14,7 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
let source = if options.recursive {
|
let source = if options.recursive {
|
||||||
Source::recursive()?
|
Source::recursive()?
|
||||||
} else if options.files.len() > 0 {
|
} else if !options.files.is_empty() {
|
||||||
Source::Files(options.files)
|
Source::Files(options.files)
|
||||||
} else {
|
} else {
|
||||||
Source::Stdin
|
Source::Stdin
|
||||||
|
|
|
@ -23,7 +23,7 @@ impl Replacer {
|
||||||
(
|
(
|
||||||
look_for,
|
look_for,
|
||||||
utils::unescape(&replace_with)
|
utils::unescape(&replace_with)
|
||||||
.unwrap_or_else(|| replace_with)
|
.unwrap_or(replace_with)
|
||||||
.into_bytes(),
|
.into_bytes(),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
@ -40,7 +40,7 @@ impl Replacer {
|
||||||
'm' => {},
|
'm' => {},
|
||||||
'e' => { regex.multi_line(false); },
|
'e' => { regex.multi_line(false); },
|
||||||
's' => {
|
's' => {
|
||||||
if !flags.contains("m") {
|
if !flags.contains('m') {
|
||||||
regex.multi_line(false);
|
regex.multi_line(false);
|
||||||
}
|
}
|
||||||
regex.dot_matches_new_line(true);
|
regex.dot_matches_new_line(true);
|
||||||
|
@ -80,16 +80,13 @@ impl Replacer {
|
||||||
) -> std::borrow::Cow<'a, [u8]> {
|
) -> std::borrow::Cow<'a, [u8]> {
|
||||||
if self.is_literal {
|
if self.is_literal {
|
||||||
self.regex.replacen(
|
self.regex.replacen(
|
||||||
&content,
|
content,
|
||||||
self.replacements,
|
self.replacements,
|
||||||
regex::bytes::NoExpand(&self.replace_with),
|
regex::bytes::NoExpand(&self.replace_with),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
self.regex.replacen(
|
self.regex
|
||||||
&content,
|
.replacen(content, self.replacements, &*self.replace_with)
|
||||||
self.replacements,
|
|
||||||
&*self.replace_with,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,7 +100,7 @@ impl Replacer {
|
||||||
self.regex.split(content).for_each(|sur_text| {
|
self.regex.split(content).for_each(|sur_text| {
|
||||||
use regex::bytes::Replacer;
|
use regex::bytes::Replacer;
|
||||||
|
|
||||||
&v.extend(sur_text);
|
v.extend(sur_text);
|
||||||
if let Some(capture) = captures.next() {
|
if let Some(capture) = captures.next() {
|
||||||
v.extend_from_slice(
|
v.extend_from_slice(
|
||||||
ansi_term::Color::Green.prefix().to_string().as_bytes(),
|
ansi_term::Color::Green.prefix().to_string().as_bytes(),
|
||||||
|
@ -127,7 +124,7 @@ impl Replacer {
|
||||||
use memmap2::{Mmap, MmapMut};
|
use memmap2::{Mmap, MmapMut};
|
||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
|
|
||||||
if let Err(_) = Self::check_not_empty(File::open(path)?) {
|
if Self::check_not_empty(File::open(path)?).is_err() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,7 +159,7 @@ impl Replacer {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn replace<'a>(
|
fn replace(
|
||||||
look_for: impl Into<String>,
|
look_for: impl Into<String>,
|
||||||
replace_with: impl Into<String>,
|
replace_with: impl Into<String>,
|
||||||
literal: bool,
|
literal: bool,
|
||||||
|
|
26
tests/cli.rs
26
tests/cli.rs
|
@ -28,13 +28,13 @@ mod cli {
|
||||||
#[test]
|
#[test]
|
||||||
fn in_place() -> Result<()> {
|
fn in_place() -> Result<()> {
|
||||||
let mut file = tempfile::NamedTempFile::new()?;
|
let mut file = tempfile::NamedTempFile::new()?;
|
||||||
file.write(b"abc123def")?;
|
file.write_all(b"abc123def")?;
|
||||||
let path = file.into_temp_path();
|
let path = file.into_temp_path();
|
||||||
|
|
||||||
sd().args(&["abc\\d+", "", path.to_str().unwrap()])
|
sd().args(["abc\\d+", "", path.to_str().unwrap()])
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
assert_file(&path.to_path_buf(), "def");
|
assert_file(&path, "def");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -42,13 +42,13 @@ mod cli {
|
||||||
#[test]
|
#[test]
|
||||||
fn in_place_with_empty_result_file() -> Result<()> {
|
fn in_place_with_empty_result_file() -> Result<()> {
|
||||||
let mut file = tempfile::NamedTempFile::new()?;
|
let mut file = tempfile::NamedTempFile::new()?;
|
||||||
file.write(b"a7c")?;
|
file.write_all(b"a7c")?;
|
||||||
let path = file.into_temp_path();
|
let path = file.into_temp_path();
|
||||||
|
|
||||||
sd().args(&["a\\dc", "", path.to_str().unwrap()])
|
sd().args(["a\\dc", "", path.to_str().unwrap()])
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
assert_file(&path.to_path_buf(), "");
|
assert_file(&path, "");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -63,11 +63,11 @@ mod cli {
|
||||||
create_soft_link(&file, &link)?;
|
create_soft_link(&file, &link)?;
|
||||||
std::fs::write(&file, "abc123def")?;
|
std::fs::write(&file, "abc123def")?;
|
||||||
|
|
||||||
sd().args(&["abc\\d+", "", link.to_str().unwrap()])
|
sd().args(["abc\\d+", "", link.to_str().unwrap()])
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
|
|
||||||
assert_file(&file.to_path_buf(), "def");
|
assert_file(&file, "def");
|
||||||
assert!(std::fs::symlink_metadata(link)?.file_type().is_symlink());
|
assert!(std::fs::symlink_metadata(link)?.file_type().is_symlink());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -76,15 +76,15 @@ mod cli {
|
||||||
#[test]
|
#[test]
|
||||||
fn replace_into_stdout() -> Result<()> {
|
fn replace_into_stdout() -> Result<()> {
|
||||||
let mut file = tempfile::NamedTempFile::new()?;
|
let mut file = tempfile::NamedTempFile::new()?;
|
||||||
file.write(b"abc123def")?;
|
file.write_all(b"abc123def")?;
|
||||||
|
|
||||||
sd().args(&["-p", "abc\\d+", "", file.path().to_str().unwrap()])
|
sd().args(["-p", "abc\\d+", "", file.path().to_str().unwrap()])
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(format!(
|
.stdout(format!(
|
||||||
"{}{}def\n",
|
"{}{}def\n",
|
||||||
ansi_term::Color::Green.prefix().to_string(),
|
ansi_term::Color::Green.prefix(),
|
||||||
ansi_term::Color::Green.suffix().to_string()
|
ansi_term::Color::Green.suffix()
|
||||||
));
|
));
|
||||||
|
|
||||||
assert_file(file.path(), "abc123def");
|
assert_file(file.path(), "abc123def");
|
||||||
|
@ -94,7 +94,7 @@ mod cli {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn stdin() -> Result<()> {
|
fn stdin() -> Result<()> {
|
||||||
sd().args(&["abc\\d+", ""])
|
sd().args(["abc\\d+", ""])
|
||||||
.write_stdin("abc123def")
|
.write_stdin("abc123def")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
|
|
Loading…
Reference in a new issue