Skip to content

Commit 44008f1

Browse files
committed
Removed all instances of .clone in favor of to_owned, also removed most instances where args where being parsed again
1 parent 570884a commit 44008f1

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/main.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
mod input;
44
mod text_effects;
55
mod utils;
6-
use std::os::unix::fs::{FileTypeExt, MetadataExt};
7-
use structopt::StructOpt;
6+
use std::os::unix::fs::{FileTypeExt, MetadataExt}; use structopt::StructOpt;
87
use std::cmp::Ordering;
98

109
struct Directory {
1110
paths: Vec<File>,
11+
args: input::Cli,
1212
}
1313

1414
#[derive(Clone)]
@@ -104,12 +104,12 @@ impl PathType {
104104
}
105105

106106
impl File {
107-
fn new(file: std::path::PathBuf) -> Self {
107+
fn new(file: std::path::PathBuf, time_format: String) -> Self {
108108
Self {
109109
group: utils::group(file.to_path_buf()),
110110
user: utils::user(file.to_path_buf()),
111-
modified: utils::file_times::modified(file.to_path_buf(), input::Cli::from_args().time_format),
112-
created: utils::file_times::created(file.to_path_buf(), input::Cli::from_args().time_format),
111+
modified: utils::file_times::modified(file.to_path_buf(), time_format.to_owned()),
112+
created: utils::file_times::created(file.to_path_buf(), time_format),
113113
size: utils::size::size(file.to_path_buf()),
114114
perms: utils::perms::perms(file.to_path_buf()),
115115
file_type: PathType::new(&file).unwrap(),
@@ -134,7 +134,9 @@ fn get_sort_type(sort_t: [bool; 4]) -> DirSortType {
134134
}
135135

136136
impl Directory {
137-
fn new(dir: std::path::PathBuf) -> Result<Self, Box<dyn std::error::Error>> {
137+
fn new(args: input::Cli) -> Result<Self, Box<dyn std::error::Error>> {
138+
let dir = &args.dir;
139+
138140
if !std::path::Path::new(&dir).exists() {
139141
return Err(
140142
Box::new(
@@ -144,18 +146,22 @@ impl Directory {
144146
}
145147

146148
if !std::path::Path::new(&dir).is_dir() {
147-
let f = File::new(dir.clone());
148-
match input::Cli::from_args().long {
149+
let f = File::new(dir.to_owned(), args.time_format);
150+
match args.long {
149151
true => print!("{:?}", f),
150152
_ => print!("{}", f)
151153
}
152154
std::process::exit(0)
153155
}
154156

155157
let paths = std::fs::read_dir(dir)?
156-
.map(|res| res.map(|e| File::new(e.path())))
158+
.map(|res| res.map(|e| File::new(
159+
e.path(), args.time_format.to_owned()
160+
)
161+
)
162+
)
157163
.collect::<Result<Vec<File>, std::io::Error>>()?;
158-
Ok(Self { paths })
164+
Ok(Self { paths, args })
159165
}
160166

161167

@@ -165,17 +171,17 @@ impl Directory {
165171
let mut directories = Vec::new();
166172
for (i, f) in new.iter().enumerate() {
167173
if f.path.symlink_metadata().unwrap().is_dir() {
168-
directories.push(new[i].clone());
174+
directories.push(new[i].to_owned());
169175
} else {
170-
newer.push(new[i].clone())
176+
newer.push(new[i].to_owned())
171177
}
172178
}
173179

174180
match get_sort_type([
175-
input::Cli::from_args().name,
176-
input::Cli::from_args().created,
177-
input::Cli::from_args().modified,
178-
input::Cli::from_args().size,
181+
self.args.name,
182+
self.args.created,
183+
self.args.modified,
184+
self.args.size,
179185
]) {
180186
DirSortType::Name => {
181187
name_sort(&mut directories);
@@ -202,10 +208,10 @@ impl Directory {
202208

203209
fn sort_paths(&mut self) {
204210
match get_sort_type([
205-
input::Cli::from_args().name,
206-
input::Cli::from_args().created,
207-
input::Cli::from_args().modified,
208-
input::Cli::from_args().size,
211+
self.args.name,
212+
self.args.created,
213+
self.args.modified,
214+
self.args.size,
209215
]) {
210216
DirSortType::Name => sort_as(&mut self.paths, |a, b| {
211217
a.path
@@ -265,7 +271,7 @@ impl Directory {
265271

266272

267273
fn sort(&mut self) {
268-
match input::Cli::from_args().gdf {
274+
match self.args.gdf {
269275
true => self.sort_directory_then_path(),
270276
false => self.sort_paths(),
271277
}
@@ -288,9 +294,9 @@ impl Directory {
288294
}
289295

290296
for p in 0..self.paths.iter().len() {
291-
let ghold = self.paths[p].group.clone();
292-
let uhold = self.paths[p].user.clone();
293-
let shold = self.paths[p].size.clone();
297+
let ghold = self.paths[p].group.to_owned();
298+
let uhold = self.paths[p].user.to_owned();
299+
let shold = self.paths[p].size.to_owned();
294300
let mut gwidth = String::new();
295301
for _ in 0..(gs - ghold.len()) {
296302
gwidth.push(' ')
@@ -433,7 +439,7 @@ impl std::fmt::Debug for File {
433439
impl std::fmt::Display for Directory {
434440
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
435441
Ok(for i in self.paths.iter() {
436-
match input::Cli::from_args().long {
442+
match self.args.long {
437443
true => write!(f, "{:?}", i)?,
438444
_ => write!(f, "{} ", i)?,
439445
}
@@ -443,7 +449,7 @@ impl std::fmt::Display for Directory {
443449

444450
fn main() {
445451
println!("{}",
446-
match Directory::new(input::Cli::from_args().dir) {
452+
match Directory::new(input::Cli::from_args()) {
447453
Ok(mut res) => format!("{}", res.setup()),
448454
Err(err) => format!("{}{}", termion::color::Fg(termion::color::Red), err)
449455
}

0 commit comments

Comments
 (0)