Use error! in place of expect() calls

This commit is contained in:
Christopher Lübbemeier 2018-03-17 16:30:49 +01:00
parent a971d5f56e
commit d02b54a521
1 changed files with 33 additions and 25 deletions

View File

@ -109,9 +109,10 @@ impl FileBrowserWidget {
// Initialize values. // Initialize values.
let nvim = shell_state.nvim_clone(); let nvim = shell_state.nvim_clone();
self.nvim = Some(nvim); self.nvim = Some(nvim);
let dir = get_current_dir(&mut self.nvim().unwrap()); if let Some(dir) = get_current_dir(&mut self.nvim().unwrap()) {
update_dir_list(&dir, &self.comps.dir_list_model, &self.comps.dir_list); update_dir_list(&dir, &self.comps.dir_list_model, &self.comps.dir_list);
self.state.borrow_mut().current_dir = dir; self.state.borrow_mut().current_dir = dir;
}
// Populate tree. // Populate tree.
tree_reload(&self.store, &self.state.borrow()); tree_reload(&self.store, &self.state.borrow());
@ -255,18 +256,19 @@ impl FileBrowserWidget {
} }
} else { } else {
// FileType::File // FileType::File
let dir = get_current_dir(&mut nvim); if let Some(dir) = get_current_dir(&mut nvim) {
let dir = Path::new(&dir); let dir = Path::new(&dir);
let file_path = if let Some(rel_path) = Path::new(&file_path) let file_path = if let Some(rel_path) = Path::new(&file_path)
.strip_prefix(&dir) .strip_prefix(&dir)
.ok() .ok()
.and_then(|p| p.to_str()) .and_then(|p| p.to_str())
{ {
rel_path rel_path
} else { } else {
&file_path &file_path
}; };
nvim.command(&format!(":e {}", file_path)).report_err(); nvim.command(&format!(":e {}", file_path)).report_err();
}
} }
})); }));
@ -441,9 +443,14 @@ fn populate_tree_nodes(
parent: Option<&gtk::TreeIter>, parent: Option<&gtk::TreeIter>,
) { ) {
let path = Path::new(dir); let path = Path::new(dir);
let iter = path.read_dir() let read_dir = match path.read_dir() {
.expect("read dir failed") Ok(read_dir) => read_dir,
.filter_map(Result::ok); Err(err) => {
error!("Couldn't populate tree: {}", err);
return;
}
};
let iter = read_dir.filter_map(Result::ok);
let mut entries: Vec<DirEntry> = if state.show_hidden { let mut entries: Vec<DirEntry> = if state.show_hidden {
iter.collect() iter.collect()
} else { } else {
@ -501,13 +508,14 @@ fn populate_tree_nodes(
} }
} }
fn get_current_dir(nvim: &mut NeovimRef) -> String { fn get_current_dir(nvim: &mut NeovimRef) -> Option<String> {
nvim.eval("getcwd()") match nvim.eval("getcwd()") {
.as_ref() Ok(cwd) => cwd.as_str().map(|s| s.to_owned()),
.ok() Err(err) => {
.and_then(|s| s.as_str()) error!("Couldn't get cwd: {}", err);
.expect("Couldn't get working directory") None
.to_owned() }
}
} }
/// Reveals and selects the given file in the file browser. /// Reveals and selects the given file in the file browser.