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() { if path.is_dir() {
dirs.push(path); dirs.push(path);
} else if path.is_file() {
files.push(
self.path_to_meta(&path)
.expect("failed to convert path to meta"),
);
} else { } 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 { for dir in dirs {
let folder_metas = self.list_folder_content(dir); let folder_metas = self.list_folder_content(dir);
if folder_metas.len() == 0 {
continue;
}
if print_folder_name { if print_folder_name {
println!("\n{}:", dir.display()) println!("\n{}:", dir.display())
} }
@ -66,7 +78,15 @@ impl<'a> Core<'a> {
pub fn list_folder_content(&self, folder: &Path) -> Vec<Meta> { pub fn list_folder_content(&self, folder: &Path) -> Vec<Meta> {
let mut content: Vec<Meta> = Vec::new(); 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 Ok(entry) = entry {
if let Some(meta) = self.path_to_meta(entry.path().as_path()) { if let Some(meta) = self.path_to_meta(entry.path().as_path()) {
content.push(meta); content.push(meta);
@ -100,24 +120,36 @@ impl<'a> Core<'a> {
let meta; let meta;
let mut symlink = None; let mut symlink = None;
if let Ok(res) = read_link(path) { if let Ok(res) = read_link(path) {
meta = path.symlink_metadata().unwrap(); meta = path
symlink = Some(res.to_str().unwrap().to_string()); .symlink_metadata()
.expect("failed to retrieve symlink metadata");
symlink = Some(
res.to_str()
.expect("failed to convert symlink to str")
.to_string(),
);
} else { } 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()) let user = get_user_by_uid(meta.uid())
.unwrap() .expect("failed to get user name")
.name() .name()
.to_str() .to_str()
.unwrap() .expect("failed to convert user name to str")
.to_string(); .to_string();
let group = get_group_by_gid(meta.gid()) let group = get_group_by_gid(meta.gid())
.unwrap() .expect("failed to get the group name")
.name() .name()
.to_str() .to_str()
.unwrap() .expect("failed to convert group name to str")
.to_string(); .to_string();
let size = Size::Bytes(meta.len()).to_string(Base::Base10, Style::Abbreviated); let size = Size::Bytes(meta.len()).to_string(Base::Base10, Style::Abbreviated);

View file

@ -91,7 +91,12 @@ impl Formatter {
false => Colors[&Elem::UnrecognizedFile], 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(); content = content + &color.paint(file_name).to_string();
let color = Colors[&Elem::Link]; let color = Colors[&Elem::Link];
@ -104,7 +109,10 @@ impl Formatter {
} }
pub fn format_date(&self, meta: &Meta) -> String { 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(); let now = SystemTime::now();
@ -117,7 +125,9 @@ impl Formatter {
color = Colors[&Elem::Older]; 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( let time = time::at(Timespec::new(
modified_time_since_epoch.as_secs() as i64, modified_time_since_epoch.as_secs() as i64,
modified_time_since_epoch.subsec_nanos() as i32, modified_time_since_epoch.subsec_nanos() as i32,

View file

@ -27,7 +27,10 @@ fn main() {
display_all: matches.is_present("all"), 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); let core = Core::new(&options);