Add some details on panics

This commit is contained in:
Peltoche 2018-11-24 12:02:39 +01:00
parent 4b9568510c
commit 5b9d87a74f
No known key found for this signature in database
GPG key ID: CED68D0487156952
3 changed files with 58 additions and 13 deletions

View file

@ -42,8 +42,16 @@ impl<'a> Core<'a> {
if path.is_dir() {
dirs.push(path);
} else if path.is_file() {
files.push(
self.path_to_meta(&path)
.expect("failed to convert path to meta"),
);
} else {
files.push(self.path_to_meta(&path).unwrap());
match path.metadata() {
Ok(_) => panic!("shouldn't failed"),
Err(err) => println!("cannot access '{}': {}", path.display(), err),
};
}
}
@ -56,6 +64,10 @@ impl<'a> Core<'a> {
for dir in dirs {
let folder_metas = self.list_folder_content(dir);
if folder_metas.len() == 0 {
continue;
}
if print_folder_name {
println!("\n{}:", dir.display())
}
@ -66,7 +78,15 @@ impl<'a> Core<'a> {
pub fn list_folder_content(&self, folder: &Path) -> Vec<Meta> {
let mut content: Vec<Meta> = Vec::new();
for entry in folder.read_dir().expect("read_dir call failed") {
let dir = match folder.read_dir() {
Ok(dir) => dir,
Err(err) => {
println!("cannot open directory'{}': {}", folder.display(), err);
return content;
}
};
for entry in dir {
if let Ok(entry) = entry {
if let Some(meta) = self.path_to_meta(entry.path().as_path()) {
content.push(meta);
@ -100,24 +120,36 @@ impl<'a> Core<'a> {
let meta;
let mut symlink = None;
if let Ok(res) = read_link(path) {
meta = path.symlink_metadata().unwrap();
symlink = Some(res.to_str().unwrap().to_string());
meta = path
.symlink_metadata()
.expect("failed to retrieve symlink metadata");
symlink = Some(
res.to_str()
.expect("failed to convert symlink to str")
.to_string(),
);
} else {
meta = path.metadata().unwrap();
meta = match path.metadata() {
Ok(meta) => meta,
Err(err) => {
println!("err: {}", err);
return None;
}
}
}
let user = get_user_by_uid(meta.uid())
.unwrap()
.expect("failed to get user name")
.name()
.to_str()
.unwrap()
.expect("failed to convert user name to str")
.to_string();
let group = get_group_by_gid(meta.gid())
.unwrap()
.expect("failed to get the group name")
.name()
.to_str()
.unwrap()
.expect("failed to convert group name to str")
.to_string();
let size = Size::Bytes(meta.len()).to_string(Base::Base10, Style::Abbreviated);

View file

@ -91,7 +91,12 @@ impl Formatter {
false => Colors[&Elem::UnrecognizedFile],
};
let file_name = meta.path.file_name().unwrap().to_str().unwrap();
let file_name = meta
.path
.file_name()
.expect("failed to retrieve path filename")
.to_str()
.expect("failed to convert path name to str");
content = content + &color.paint(file_name).to_string();
let color = Colors[&Elem::Link];
@ -104,7 +109,10 @@ impl Formatter {
}
pub fn format_date(&self, meta: &Meta) -> String {
let modified_time = meta.metadata.modified().unwrap();
let modified_time = meta
.metadata
.modified()
.expect("failed to retrieve modified date");
let now = SystemTime::now();
@ -117,7 +125,9 @@ impl Formatter {
color = Colors[&Elem::Older];
}
let modified_time_since_epoch = modified_time.duration_since(UNIX_EPOCH).unwrap();
let modified_time_since_epoch = modified_time
.duration_since(UNIX_EPOCH)
.expect("failed to convert modified time to timestamp");
let time = time::at(Timespec::new(
modified_time_since_epoch.as_secs() as i64,
modified_time_since_epoch.subsec_nanos() as i32,

View file

@ -27,7 +27,10 @@ fn main() {
display_all: matches.is_present("all"),
};
let inputs: Vec<&str> = matches.values_of("FILE").unwrap().collect();
let inputs: Vec<&str> = matches
.values_of("FILE")
.expect("failed to retrieve cli value")
.collect();
let core = Core::new(&options);