Skip to content

Commit 1397df7

Browse files
committed
removed some immediate reference dereferences and added general bug fixes along with some instructions on contributing
1 parent 134defd commit 1397df7

File tree

2 files changed

+106
-102
lines changed

2 files changed

+106
-102
lines changed

contributing.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```bash
2+
cargo fmt
3+
```
4+
5+
```bash
6+
rustup component add clippy
7+
8+
cargo clippy
9+
```

src/main.rs

Lines changed: 97 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(clippy::collapsible_if)]
12
extern crate libc;
23
extern crate pretty_bytes;
34

@@ -66,10 +67,9 @@ pub struct Cli {
6667
time_format: String,
6768
}
6869

69-
fn main() -> Result<(), Box<dyn std::error::Error>> {
70+
fn output() -> Result<(), Box<dyn std::error::Error>> {
7071
let args = Cli::from_args();
7172
let directory = &args.path;
72-
let headline_on = &args.headline_on;
7373
let hidden_files = &args.hidden_files;
7474
let wide_mode = &args.wide_mode;
7575
let time_on = &args.time_on;
@@ -81,114 +81,109 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8181
let time_format = &args.time_format;
8282
let colors_on = &args.colors_on;
8383

84-
draw_headlines(
85-
*headline_on,
86-
*perms_on,
87-
*size_on,
88-
*time_on,
89-
*group_on,
90-
*user_on,
91-
);
92-
9384
let mut singly_found = false;
9485
if !std::path::Path::new(directory).exists() {
9586
let entries = fs::read_dir(".")?
9687
.map(|res| res.map(|e| e.path()))
9788
.collect::<Result<Vec<_>, io::Error>>()?;
98-
99-
let mut size_count = 4;
100-
for s in &entries {
101-
if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count {
102-
size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len();
103-
};
89+
90+
let mut size_count = 4;
91+
for s in &entries {
92+
if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count {
93+
size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len();
94+
};
95+
}
96+
for e in &entries {
97+
if e
98+
.file_name()
99+
.unwrap()
100+
.to_str()
101+
.unwrap()
102+
.to_lowercase()
103+
.contains(&args.path.display().to_string().to_lowercase())
104+
{
105+
let _ = single(e, size_count, *wide_mode, time_format);
106+
singly_found = true;
104107
}
105-
for e in &entries {
106-
if e
107-
.file_name()
108-
.unwrap()
109-
.to_str()
110-
.unwrap()
111-
.to_lowercase()
112-
.contains(&args.path.display().to_string().to_lowercase())
113-
{
114-
let _ = single(e, size_count, *wide_mode, time_format);
115-
singly_found = true;
116-
}
117-
}
118-
if !singly_found {
119-
if !*colors_on {
120-
print!("{}", color::Fg(color::Red));
121-
}
122-
println!(
123-
"{}",
124-
Style::new()
125-
.bold()
126-
.paint(format!("{} could not be found", &args.path.display().to_string()))
127-
);
128-
}
129-
std::process::exit(1);
108+
}
109+
if !singly_found {
110+
if !*colors_on {
111+
print!("{}", color::Fg(color::Red));
112+
}
113+
println!(
114+
"{}",
115+
Style::new().bold().paint(format!(
116+
"{} could not be found",
117+
&args.path.display().to_string()
118+
))
119+
);
120+
}
121+
std::process::exit(1);
130122
}
131123

132124
if !directory.symlink_metadata()?.is_dir() {
133125
let _ = single(&directory, 4 as usize, *wide_mode, time_format);
134126
std::process::exit(0);
135127
}
136128

137-
138129
let entries = fs::read_dir(directory)?
139130
.map(|res| res.map(|e| e.path()))
140131
.collect::<Result<Vec<_>, io::Error>>()?;
141132

142-
let mut size_count = 4;
143-
let mut group_size = 8;
144-
for s in &entries {
145-
if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count {
146-
size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len();
147-
};
148-
149-
let metadata_uid = fs::symlink_metadata(&s)?.uid();
150-
let user_name_len = get_user_name(metadata_uid).len();
151-
if user_name_len > group_size {
152-
group_size = user_name_len;
153-
}
133+
let mut size_count = 4;
134+
let mut group_size = 8;
135+
for s in &entries {
136+
if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count {
137+
size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len();
138+
};
139+
140+
let metadata_uid = fs::symlink_metadata(&s)?.uid();
141+
let user_name_len = get_user_name(metadata_uid).len();
142+
if user_name_len > group_size {
143+
group_size = user_name_len;
154144
}
145+
}
155146

156-
let mut dirs: Vec<&std::path::PathBuf> = vec![];
157-
158-
for e in &entries {
159-
if !&e.file_name().unwrap().to_str().unwrap().starts_with(".") || *hidden_files {
160-
if *&e.is_file() && !*is_sorted {
161-
dirs.push(*&e);
162-
} else {
163-
if !perms_on {
164-
let _ = file_perms(&e);
165-
}
147+
let mut dirs: Vec<&std::path::PathBuf> = vec![];
166148

167-
if !size_on {
168-
let _ = file_size(size_count, &e);
169-
}
149+
for e in &entries {
150+
if !&e.file_name().unwrap().to_str().unwrap().starts_with('.') || *hidden_files {
151+
if e.is_file() && !*is_sorted {
152+
dirs.push(e);
153+
} else {
154+
if !perms_on {
155+
let _ = file_perms(&e);
156+
}
170157

171-
if !time_on {
172-
let _ = time_mod(e, time_format);
173-
}
158+
if !size_on {
159+
let _ = file_size(size_count, &e);
160+
}
174161

175-
if !group_on {
176-
let _ = show_group_name(e);
177-
}
162+
if !time_on {
163+
let _ = time_mod(e, time_format);
164+
}
178165

179-
if !user_on {
180-
let _ = show_user_name(e);
181-
}
166+
if !group_on {
167+
let _ = show_group_name(e);
168+
}
182169

183-
let _ = show_file_name(&e, *wide_mode);
170+
if !user_on {
171+
let _ = show_user_name(e);
184172
}
173+
174+
let _ = show_file_name(&e, *wide_mode);
185175
}
186176
}
187-
for e in &dirs {
188-
let _ = single(e, size_count, *wide_mode, time_format);
189-
}
177+
}
178+
for e in &dirs {
179+
let _ = single(e, size_count, *wide_mode, time_format);
180+
}
190181

191-
Ok(())
182+
Ok(())
183+
}
184+
185+
fn main() {
186+
let _ = output();
192187
}
193188

194189
pub fn draw_headlines(
@@ -216,7 +211,7 @@ pub fn draw_headlines(
216211
draw_headline("user", 0, true);
217212
}
218213
draw_headline("name", 0, true);
219-
print!("\n");
214+
println!();
220215
}
221216
}
222217

@@ -244,8 +239,8 @@ pub fn file_size(
244239
print!(
245240
"{} ",
246241
Style::new()
247-
.bold()
248-
.paint(convert(fs::symlink_metadata(&e)?.size() as f64))
242+
.bold()
243+
.paint(convert(fs::symlink_metadata(&e)?.size() as f64))
249244
);
250245
Ok(())
251246
}
@@ -254,7 +249,7 @@ pub fn time_mod(
254249
e: &std::path::PathBuf,
255250
time_format: &str,
256251
) -> Result<(), Box<dyn std::error::Error>> {
257-
if let Ok(_) = e.symlink_metadata()?.modified() {
252+
if e.symlink_metadata()?.modified().is_ok() {
258253
let mtime = FileTime::from_last_modification_time(&e.symlink_metadata().unwrap());
259254
let d = NaiveDateTime::from_timestamp(mtime.seconds() as i64, 0);
260255
if !Cli::from_args().colors_on {
@@ -277,7 +272,7 @@ pub fn show_group_name(e: &std::path::PathBuf) -> Result<(), Box<dyn std::error:
277272
match get_group_by_gid(fs::symlink_metadata(e)?.gid()).as_ref() {
278273
Some(n) => n.name().to_str().unwrap(),
279274
None => "",
280-
}
275+
}
281276
)
282277
);
283278
Ok(())
@@ -293,7 +288,7 @@ pub fn show_user_name(e: &std::path::PathBuf) -> Result<(), Box<dyn std::error::
293288
match get_user_by_uid(fs::symlink_metadata(e)?.uid()).as_ref() {
294289
Some(n) => n.name().to_str().unwrap(),
295290
None => "",
296-
}
291+
}
297292
)
298293
);
299294
Ok(())
@@ -313,7 +308,7 @@ pub fn show_file_name(
313308
}
314309
print!("{}/", &e.file_name().unwrap().to_str().unwrap());
315310
if !wide_mode {
316-
print!("\n");
311+
println!();
317312
} else {
318313
print!(" ");
319314
}
@@ -324,8 +319,8 @@ pub fn show_file_name(
324319
print!(
325320
"{} -> ",
326321
Style::new()
327-
.bold()
328-
.paint(e.file_name().unwrap().to_str().unwrap())
322+
.bold()
323+
.paint(e.file_name().unwrap().to_str().unwrap())
329324
);
330325
match fs::canonicalize(fs::read_link(e)?) {
331326
Ok(_n) => {
@@ -335,9 +330,9 @@ pub fn show_file_name(
335330
print!(
336331
"{}/",
337332
fs::canonicalize(fs::read_link(e)?)
338-
.unwrap()
339-
.to_str()
340-
.unwrap()
333+
.unwrap()
334+
.to_str()
335+
.unwrap()
341336
)
342337
}
343338
} else {
@@ -346,9 +341,9 @@ pub fn show_file_name(
346341
print!(
347342
"{}",
348343
fs::canonicalize(fs::read_link(e)?)
349-
.unwrap()
350-
.to_str()
351-
.unwrap()
344+
.unwrap()
345+
.to_str()
346+
.unwrap()
352347
)
353348
}
354349
}
@@ -374,7 +369,7 @@ pub fn show_file_name(
374369
}
375370
}
376371
if !wide_mode {
377-
print!("\n");
372+
println!();
378373
} else {
379374
print!(" ");
380375
}
@@ -385,11 +380,11 @@ pub fn show_file_name(
385380
print!(
386381
"{}",
387382
Style::new()
388-
.bold()
389-
.paint(e.file_name().unwrap().to_str().unwrap())
383+
.bold()
384+
.paint(e.file_name().unwrap().to_str().unwrap())
390385
);
391386
if !wide_mode {
392-
print!("\n");
387+
println!();
393388
} else {
394389
print!(" ");
395390
}
@@ -461,7 +456,7 @@ pub fn perms(mode: u16) -> String {
461456
format!("{}{}", color::Fg(color::LightRed), group),
462457
format!("{}{}", color::Fg(color::Yellow), other),
463458
]
464-
.join("")
459+
.join("")
465460
} else {
466461
[user, group, other].join("")
467462
}

0 commit comments

Comments
 (0)