Skip to content

Commit

Permalink
chore(error): remove basically all .unwrap() calls except for those…
Browse files Browse the repository at this point in the history
… in the `plugins` module
  • Loading branch information
lthoerner committed Aug 31, 2023
1 parent 0490418 commit 8c02210
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 83 deletions.
10 changes: 10 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ pub enum StateError {
NoNextDirectory,
FailedToOpenConfigFile(PathBuf),
FailedToReadConfigFile(PathBuf),
UnsupportedTerminal,
}

/// Error type for errors which occur during path operations.
pub enum PathError {
FailedToConvertStringToPath(String),
FailedToConvertPathToString(PathBuf),
FailedToCanonicalize(PathBuf),
FailedToGetParent(PathBuf),
UnknownDirectory(PathBuf),
}

Expand Down Expand Up @@ -255,6 +257,7 @@ impl Display for StateError {
path.display()
)
}
UnsupportedTerminal => write!(f, "Terminal is not supported"),
}
}
}
Expand All @@ -273,6 +276,13 @@ impl Display for PathError {
FailedToCanonicalize(path) => {
write!(f, "Path '{}' could not be canonicalized", path.display())
}
FailedToGetParent(path) => {
write!(
f,
"Parent directory of path '{}' could not be determined",
path.display()
)
}
UnknownDirectory(path) => {
write!(
f,
Expand Down
1 change: 0 additions & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod dispatcher;
mod parser;
pub mod readline;
mod symbols;
mod tokenizer;
Expand Down
75 changes: 0 additions & 75 deletions src/eval/parser.rs

This file was deleted.

8 changes: 5 additions & 3 deletions src/eval/readline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustyline::{
Completer, CompletionType, Config, Editor, Helper, Highlighter, Hinter, Validator,
};

use crate::errors::{Handle, Result};
use crate::state::shell::ShellState;

#[derive(Helper, Completer, Hinter, Validator, Highlighter)]
Expand Down Expand Up @@ -39,15 +40,16 @@ pub struct LineEditor {

impl LineEditor {
// Creates a LineEditor with the default configuration and history file
pub fn new(history_file: &str) -> Self {
pub fn new(history_file: &str) -> Result<Self> {
let config = Config::builder()
.history_ignore_space(true)
.completion_type(CompletionType::Fuzzy)
.build();

let helper = LineEditorHelper::new();

let mut editor = Editor::with_config(config).unwrap();
let mut editor =
Editor::with_config(config).replace_err(state_err!(UnsupportedTerminal))?;
editor.set_helper(Some(helper));
if editor.load_history(history_file).is_err() {
println!("No existing history file found, attempting to create one...");
Expand All @@ -61,7 +63,7 @@ impl LineEditor {
}
}

Self { editor }
Ok(Self { editor })
}

pub fn prompt_and_read_line(&mut self, shell: &ShellState) -> Option<String> {
Expand Down
1 change: 0 additions & 1 deletion src/exec/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crossterm::cursor::MoveTo;
use crossterm::execute;
use crossterm::style::Stylize;
use crossterm::terminal::{self, Clear, ClearType};
use fs_err;

use super::builtin_arguments::ListDirectoryArguments;
use super::commands::{Executable, Runnable};
Expand Down
16 changes: 14 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod exec;
mod plugins;
mod state;

use errors::Result;
use errors::{Result, RushError};
use eval::{Dispatcher, LineEditor};
use plugins::host::PluginHost;
use state::ShellState;
Expand All @@ -18,9 +18,14 @@ fn main() {
};

let plugins = PluginHost::new(shell.clone());

// The LineEditor type is responsible for reading lines of input from the user, storing history,
// providing tab completion and other line-editing features
let mut line_editor = LineEditor::new("./config/history.rush");
let mut line_editor = match LineEditor::new("./config/history.rush") {
Ok(editor) => editor,
Err(err) => crash_with_error(err),
};

// The Dispatcher type is responsible for resolving command names to actual function calls,
// or executables if needed, and then invoking them with the given arguments
let dispatcher = Dispatcher::default();
Expand All @@ -41,3 +46,10 @@ fn handle_error(potential_error: Result<()>, shell: &mut ShellState) {
eprintln!("{}", error);
}
}

// Handles errors from which the shell cannot recover, mainly errors arising from shell setup
fn crash_with_error(error: RushError) -> ! {
eprintln!("{}", error);
// TODO: Maybe return status code for each error type?
std::process::exit(1);
}
4 changes: 3 additions & 1 deletion src/state/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ impl Configuration {
// Scans a configuration file for settings and updates the configuration accordingly
pub fn from_file(filename: &str) -> Result<Self> {
let filename = PathBuf::from(filename);
let dirname = filename.parent().unwrap();
let dirname = filename
.parent()
.replace_err(path_err!(FailedToGetParent(filename)))?;

let mut config = Self::default();
let file = File::open(filename.clone())
Expand Down

0 comments on commit 8c02210

Please sign in to comment.