Skip to content

Commit 34dd5b0

Browse files
authored
Run cargo fmt (#44)
1 parent 2a24f95 commit 34dd5b0

File tree

10 files changed

+152
-128
lines changed

10 files changed

+152
-128
lines changed

build.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ fn main() {
1919
fn create_man_page() {
2020
use man::prelude::*;
2121
let page = Manual::new("rep")
22-
.flag(
23-
Flag::new()
24-
.short("-w")
25-
.long("--write")
26-
.help(
22+
.flag(Flag::new().short("-w").long("--write").help(
2723
r#"Write the output to files directly (instead of outputting a patch)
2824
2925
If this flag is not present, and a patch is output, then the default pager is `less`. The
@@ -42,11 +38,7 @@ environment variable REP_PAGER can be used to override the pager.
4238
.long("--string-mode")
4339
.help("Treat expressions as non-regex strings."),
4440
)
45-
.flag(
46-
Flag::new()
47-
.long("--no-color")
48-
.help("Disable color."),
49-
)
41+
.flag(Flag::new().long("--no-color").help("Disable color."))
5042
.flag(
5143
Flag::new()
5244
.long("--color")
@@ -72,8 +64,7 @@ w - match full words only
7264
.arg(Arg::new("replace_with"))
7365
.render();
7466

75-
let mut man_path =
76-
std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
67+
let mut man_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
7768
man_path.push("rep.1");
7869
std::fs::write(man_path, page).expect("Error writing man page");
7970
}

src/cli.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ pub(crate) struct Options {
1111
///
1212
/** Write the output to files directly (instead of outputting a patch)
1313
14-
If this flag is not present, and a patch is output, then the default pager is `less`. The
15-
environment variable REP_PAGER can be used to override the pager.
16-
*/
14+
If this flag is not present, and a patch is output, then the default pager is `less`. The
15+
environment variable REP_PAGER can be used to override the pager.
16+
*/
1717
pub write: bool,
1818

1919
#[structopt(short = "d", long = "delete-lines")]

src/edit.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
use std::path::PathBuf;
2-
use std::io::StdinLock;
1+
use indexmap::IndexMap;
32
use regex::Regex;
43
use std::io::prelude::*;
5-
use indexmap::IndexMap;
4+
use std::io::StdinLock;
5+
use std::path::PathBuf;
66

77
#[derive(Debug)]
88
pub(crate) struct Edit {
99
pub(crate) file: PathBuf,
1010
pub(crate) text: String,
11-
pub(crate) line_number: u32
11+
pub(crate) line_number: u32,
1212
}
1313

14-
#[derive(Debug)]
15-
#[derive(thiserror::Error)]
14+
#[derive(Debug, thiserror::Error)]
1615
pub enum Error {
17-
#[error("No file, number, and text matches")]
18-
Match,
16+
#[error("No file, number, and text matches")]
17+
Match,
1918
}
2019

2120
impl Edit {
2221
pub(crate) fn new(file: PathBuf, text: String, line_number: u32) -> Edit {
23-
Edit { file, text, line_number }
22+
Edit {
23+
file,
24+
text,
25+
line_number,
26+
}
2427
}
2528

26-
pub(crate) fn parse (
27-
reader: StdinLock<'_>
29+
pub(crate) fn parse(
30+
reader: StdinLock<'_>,
2831
) -> Result<IndexMap<PathBuf, Vec<Edit>>, std::io::Error> {
2932
let mut path_to_edits = IndexMap::new();
3033
for line in reader.lines() {
@@ -73,11 +76,7 @@ impl Edit {
7376
None => return Err(Error::Match),
7477
};
7578

76-
return Ok(Edit::new(
77-
file,
78-
text,
79-
number,
80-
))
79+
return Ok(Edit::new(file, text, number));
8180
}
8281
}
8382

src/input.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
use crate::{Replacer, Result, edit::Edit, patcher::Patcher, writer::Writer, output::OutputType};
2-
use std::io::prelude::*;
1+
use crate::{edit::Edit, output::OutputType, patcher::Patcher, writer::Writer, Replacer, Result};
32
use std::fs::File;
3+
use std::io::prelude::*;
44

55
pub(crate) struct App {
6-
replacer: Option<Replacer>
6+
replacer: Option<Replacer>,
77
}
88

99
impl App {
1010
pub(crate) fn new(replacer: Option<Replacer>) -> Self {
1111
Self { replacer }
1212
}
1313

14-
pub(crate) fn run(&self, preview: bool, delete: bool, color: bool, stdout: bool, pager: Option<String>) -> Result<()> {
14+
pub(crate) fn run(
15+
&self,
16+
preview: bool,
17+
delete: bool,
18+
color: bool,
19+
stdout: bool,
20+
pager: Option<String>,
21+
) -> Result<()> {
1522
{
1623
let stdin = std::io::stdin();
1724
let handle = stdin.lock();
@@ -37,7 +44,7 @@ impl App {
3744
for (path, edits) in path_to_edits {
3845
let patcher = Patcher::new(edits, self.replacer.as_ref());
3946
if let Err(_) = Self::check_not_empty(File::open(&path)?) {
40-
continue // FIXME:
47+
continue; // FIXME:
4148
}
4249
let writer = Writer::new(path.to_path_buf(), &patcher);
4350
let text = match writer.patch_preview(color, delete) {
@@ -59,10 +66,10 @@ impl App {
5966
}
6067
}
6168
}
62-
},
69+
}
6370
Err(_) => {
6471
return Ok(()); // FIXME:
65-
},
72+
}
6673
}
6774
drop(output_type);
6875
}

src/main.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
mod cli;
2+
mod edit;
23
mod error;
34
mod input;
4-
mod edit;
5-
mod patcher;
6-
mod writer;
5+
mod less;
76
mod output;
7+
mod patcher;
88
pub(crate) mod replacer;
99
pub(crate) mod utils;
10-
mod less;
10+
mod writer;
1111

12-
use std::process;
13-
use std::env;
1412
pub(crate) use self::input::App;
1513
pub(crate) use error::Result;
1614
use replacer::Replacer;
15+
use std::env;
16+
use std::process;
1717

1818
fn main() -> Result<()> {
1919
use structopt::StructOpt;
@@ -33,15 +33,13 @@ fn main() -> Result<()> {
3333
let pager = env::var("REP_PAGER").ok();
3434

3535
if let (Some(find), Some(replace_with)) = (options.find, options.replace_with) {
36-
App::new(
37-
Some(Replacer::new(
38-
find,
39-
replace_with,
40-
options.literal_mode,
41-
options.flags,
42-
options.replacements,
43-
)?),
44-
)
36+
App::new(Some(Replacer::new(
37+
find,
38+
replace_with,
39+
options.literal_mode,
40+
options.flags,
41+
options.replacements,
42+
)?))
4543
.run(!options.write, options.delete, color, options.stdout, pager)?;
4644
} else {
4745
App::new(None).run(!options.write, options.delete, color, options.stdout, pager)?;

src/output.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use std::process::{Child, Command, Stdio};
55

66
use super::less::retrieve_less_version;
77

8-
#[derive(Debug)]
9-
#[derive(thiserror::Error)]
8+
#[derive(Debug, thiserror::Error)]
109
pub enum Error {
1110
#[error("Could not parse pager command")]
1211
ParseError(String),
@@ -28,10 +27,7 @@ pub enum OutputType {
2827
}
2928

3029
impl OutputType {
31-
pub fn for_pager(
32-
pager: Option<String>,
33-
quit_if_one_screen: bool,
34-
) -> Result<Self, Error> {
30+
pub fn for_pager(pager: Option<String>, quit_if_one_screen: bool) -> Result<Self, Error> {
3531
let replace_arguments_to_less = pager.is_none();
3632
let pager = pager.unwrap_or_else(|| String::from("less"));
3733
let pagerflags = match shell_words::split(&pager) {
@@ -79,7 +75,7 @@ impl OutputType {
7975
Some(stdin) => return Ok(stdin),
8076
None => return Err(Error::PagerError),
8177
};
82-
},
78+
}
8379
OutputType::Stdout(ref mut handle) => Ok(handle),
8480
}
8581
}

src/patcher.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::str;
66

77
pub(crate) struct Patcher<'a> {
88
edits: Vec<Edit>,
9-
replacer: Option<&'a Replacer>
9+
replacer: Option<&'a Replacer>,
1010
}
1111

1212
#[derive(Debug, thiserror::Error)]
@@ -69,37 +69,43 @@ mod tests {
6969

7070
#[test]
7171
fn patch_bad_number() {
72-
let patcher = Patcher::new(vec![
73-
Edit {
74-
file: PathBuf::from("f"),
75-
line_number: 1,
76-
text: "foo".to_string(),
77-
},
78-
Edit {
79-
file: PathBuf::from("f"),
80-
line_number: 3,
81-
text: "bar".to_string(),
82-
},
83-
], None);
72+
let patcher = Patcher::new(
73+
vec![
74+
Edit {
75+
file: PathBuf::from("f"),
76+
line_number: 1,
77+
text: "foo".to_string(),
78+
},
79+
Edit {
80+
file: PathBuf::from("f"),
81+
line_number: 3,
82+
text: "bar".to_string(),
83+
},
84+
],
85+
None,
86+
);
8487
let lines = vec!["a".to_string(), "b".to_string()];
8588
let result = patcher.patch(lines, false);
8689
assert!(matches!(result, Err(Error::LineNumber(3))));
8790
}
8891

8992
#[test]
9093
fn patch() {
91-
let patcher = Patcher::new(vec![
92-
Edit {
93-
file: PathBuf::from("f"),
94-
line_number: 2,
95-
text: "foo".to_string(),
96-
},
97-
Edit {
98-
file: PathBuf::from("f"),
99-
line_number: 3,
100-
text: "bar".to_string(),
101-
},
102-
], None);
94+
let patcher = Patcher::new(
95+
vec![
96+
Edit {
97+
file: PathBuf::from("f"),
98+
line_number: 2,
99+
text: "foo".to_string(),
100+
},
101+
Edit {
102+
file: PathBuf::from("f"),
103+
line_number: 3,
104+
text: "bar".to_string(),
105+
},
106+
],
107+
None,
108+
);
103109
let lines = vec!["a".to_string(), "b".to_string(), "c".to_string()];
104110
let result = patcher.patch(lines, false);
105111
assert!(result.is_ok());

src/replacer.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,16 @@ impl Replacer {
6363
})
6464
}
6565

66-
pub(crate) fn replace<'a>(
67-
&'a self,
68-
content: &'a [u8],
69-
) -> std::borrow::Cow<'a, [u8]> {
66+
pub(crate) fn replace<'a>(&'a self, content: &'a [u8]) -> std::borrow::Cow<'a, [u8]> {
7067
if self.is_literal {
7168
self.regex.replacen(
7269
&content,
7370
self.replacements,
7471
regex::bytes::NoExpand(&self.replace_with),
7572
)
7673
} else {
77-
self.regex.replacen(
78-
&content,
79-
self.replacements,
80-
&*self.replace_with,
81-
)
74+
self.regex
75+
.replacen(&content, self.replacements, &*self.replace_with)
8276
}
8377
}
8478
}

0 commit comments

Comments
 (0)