Skip to content

Commit 855b92f

Browse files
committed
refactor: add rustfmt.toml
1 parent 01dd128 commit 855b92f

File tree

3 files changed

+34
-73
lines changed

3 files changed

+34
-73
lines changed

rustfmt.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
edition = "2018"
2+
struct_field_align_threshold = 40
3+
max_width = 120
4+
comment_width = 120
5+
reorder_imports = true
6+
fn_single_line = false

src/bin/code-minimap/main.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ use cli::{CompletionOpt, Opt, StructOpt, Subcommand};
88
use code_minimap::LossyReader;
99

1010
fn main() {
11-
if let Err(e) = try_main()
12-
{
13-
if let Some(ioerr) = e.root_cause().downcast_ref::<io::Error>()
14-
{
15-
if ioerr.kind() == io::ErrorKind::BrokenPipe
16-
{
11+
if let Err(e) = try_main() {
12+
if let Some(ioerr) = e.root_cause().downcast_ref::<io::Error>() {
13+
if ioerr.kind() == io::ErrorKind::BrokenPipe {
1714
std::process::exit(0);
1815
}
1916
}
@@ -24,28 +21,18 @@ fn main() {
2421

2522
fn try_main() -> anyhow::Result<()> {
2623
let opt: Opt = Opt::from_args();
27-
match &opt.subcommand
28-
{
29-
Some(Subcommand::Completion(CompletionOpt {
30-
shell,
31-
})) =>
32-
{
33-
Opt::clap().gen_completions_to(
34-
env!("CARGO_PKG_NAME"),
35-
*shell,
36-
&mut std::io::stdout(),
37-
);
38-
},
39-
None =>
40-
{
24+
match &opt.subcommand {
25+
Some(Subcommand::Completion(CompletionOpt { shell })) => {
26+
Opt::clap().gen_completions_to(env!("CARGO_PKG_NAME"), *shell, &mut std::io::stdout());
27+
}
28+
None => {
4129
let stdin = io::stdin();
42-
let reader: Box<dyn BufRead> = match &opt.file
43-
{
30+
let reader: Box<dyn BufRead> = match &opt.file {
4431
Some(path) => Box::new(LossyReader::open(path)?),
4532
None => Box::new(stdin.lock()),
4633
};
4734
code_minimap::print(reader, opt.hscale, opt.vscale, opt.padding)?;
48-
},
35+
}
4936
}
5037
Ok(())
5138
}

src/core.rs

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ impl LossyReader {
1616
let file = File::open(path)?;
1717
let reader = BufReader::new(file);
1818

19-
Ok(Self {
20-
reader,
21-
})
19+
Ok(Self { reader })
2220
}
2321
}
2422

@@ -40,8 +38,7 @@ impl BufRead for LossyReader {
4038
fn read_line(&mut self, buf: &mut String) -> std::io::Result<usize> {
4139
let mut append_buf = Vec::new();
4240
let res = self.read_until(0x0a, &mut append_buf);
43-
if let Err(err) = res
44-
{
41+
if let Err(err) = res {
4542
return Err(err);
4643
}
4744
buf.push_str(&String::from_utf8_lossy(&append_buf));
@@ -62,9 +59,7 @@ pub fn write(
6259
.lines()
6360
.map(|line| {
6461
line.map(|line| {
65-
let beg = line
66-
.find(|c: char| !c.is_whitespace())
67-
.unwrap_or(usize::max_value());
62+
let beg = line.find(|c: char| !c.is_whitespace()).unwrap_or(usize::max_value());
6863
let end = line.rfind(|c: char| !c.is_whitespace()).unwrap_or(0);
6964
(beg, end)
7065
})
@@ -77,21 +72,16 @@ pub fn write(
7772
.into_iter()
7873
.try_for_each(|chunk| {
7974
let mut chunk_size = 0;
80-
for (i, (_, group)) in chunk.enumerate()
81-
{
82-
let (beg, end) = group.into_iter().try_fold(
83-
(usize::max_value(), 0),
84-
|(beg, end), (_, line)| {
75+
for (i, (_, group)) in chunk.enumerate() {
76+
let (beg, end) = group
77+
.into_iter()
78+
.try_fold((usize::max_value(), 0), |(beg, end), (_, line)| {
8579
line.map(|(b, e)| (beg.min(b), end.max(e)))
86-
},
87-
)?;
80+
})?;
8881
frame[i] = beg..(end + 1);
8982
chunk_size += 1;
9083
}
91-
frame
92-
.iter_mut()
93-
.skip(chunk_size)
94-
.for_each(|row| *row = 0..0);
84+
frame.iter_mut().skip(chunk_size).for_each(|row| *row = 0..0);
9585
scale_frame(&mut frame, hscale);
9686
write_frame(&mut writer, &frame, padding)
9787
})
@@ -109,12 +99,7 @@ pub fn write(
10999
/// let stdin = io::stdin();
110100
/// code_minimap::print(stdin.lock(), 1.0, 1.0, None).unwrap();
111101
/// ```
112-
pub fn print(
113-
reader: impl BufRead,
114-
hscale: f64,
115-
vscale: f64,
116-
padding: Option<usize>,
117-
) -> io::Result<()> {
102+
pub fn print(reader: impl BufRead, hscale: f64, vscale: f64, padding: Option<usize>) -> io::Result<()> {
118103
write(io::stdout(), reader, hscale, vscale, padding)
119104
}
120105

@@ -132,49 +117,32 @@ pub fn print(
132117
/// code_minimap::write_to_string(stdin.lock(), 1.0, 1.0, None).unwrap();
133118
/// print!("{}", s);
134119
/// ```
135-
pub fn write_to_string(
136-
reader: impl BufRead,
137-
hscale: f64,
138-
vscale: f64,
139-
padding: Option<usize>,
140-
) -> io::Result<String> {
120+
pub fn write_to_string(reader: impl BufRead, hscale: f64, vscale: f64, padding: Option<usize>) -> io::Result<String> {
141121
let mut buf = Vec::new();
142122
write(&mut buf, reader, hscale, vscale, padding)?;
143123
Ok(String::from_utf8(buf).unwrap())
144124
}
145125

146-
fn write_frame(
147-
mut writer: impl Write,
148-
frame: &[Range<usize>],
149-
padding: Option<usize>,
150-
) -> std::io::Result<()> {
126+
fn write_frame(mut writer: impl Write, frame: &[Range<usize>], padding: Option<usize>) -> std::io::Result<()> {
151127
let idx = |pos| {
152-
frame.iter().enumerate().fold(0, |acc, (i, x)| {
153-
if x.contains(&pos)
154-
{
155-
acc + (1 << i)
156-
}
157-
else
158-
{
159-
acc
160-
}
161-
})
128+
frame
129+
.iter()
130+
.enumerate()
131+
.fold(0, |acc, (i, x)| if x.contains(&pos) { acc + (1 << i) } else { acc })
162132
};
163133
let end = frame.iter().max_by_key(|range| range.end).unwrap().end;
164134
let line: String = (0..end)
165135
.step_by(2)
166136
.map(|i| BRAILLE_MATRIX[(idx(i)) + (idx(i + 1) << 4)])
167137
.collect();
168-
match padding
169-
{
138+
match padding {
170139
Some(padding) => writeln!(writer, "{0:<1$}", line, padding),
171140
None => writeln!(writer, "{}", line),
172141
}
173142
}
174143

175144
fn scale_frame(frame: &mut [Range<usize>], factor: f64) {
176-
for x in frame
177-
{
145+
for x in frame {
178146
*x = scale(x.start, factor)..scale(x.end, factor);
179147
}
180148
}

0 commit comments

Comments
 (0)