Radish alpha
r
rad:z39mP9rQAaGmERfUMPULfPUi473tY
Radicle terminal user interface
Radicle
Git
Replace optional error variant in return types
Merged did:key:z6MkwcUR...q1kL opened 8 months ago

This patch replaces the Result<_, Optionanyhow::Error> with a proper error.

This simplifies the setup code a bit. The only changed behaviour is that when rad does not exit successfully, an error is returned instead of Some(error), which does not alter the behaviour of this binary, because it would exit the process anyways, but now “rad binary failed” is printed as well.

2 files changed +11 -17 a2de36dd 7a1ba859
modified bin/main.rs
@@ -54,12 +54,10 @@ enum Command {
}

fn main() {
-
    match parse_args().map_err(Some).and_then(run) {
+
    match parse_args().and_then(run) {
        Ok(_) => process::exit(0),
        Err(err) => {
-
            if let Some(err) = err {
-
                radicle_term::error(format!("rad-tui: {err}"));
-
            }
+
            radicle_term::error(format!("rad-tui: {err}"));
            process::exit(1);
        }
    }
@@ -150,15 +148,13 @@ fn print_help() -> anyhow::Result<()> {
    tui_help::run(Default::default(), cli_term::DefaultContext)
}

-
fn run(command: Command) -> Result<(), Option<anyhow::Error>> {
+
fn run(command: Command) -> Result<(), anyhow::Error> {
    match command {
        Command::Version { json } => {
            let mut stdout = io::stdout();
            if json {
-
                VERSION
-
                    .write_json(&mut stdout)
-
                    .map_err(|e| Some(e.into()))?;
-
                writeln!(&mut stdout).ok();
+
                VERSION.write_json(&mut stdout)?;
+
                writeln!(&mut stdout)?;
            } else {
                println!("rad-tui {} ({})", VERSION.version, VERSION.commit);
            }
@@ -182,7 +178,7 @@ fn run(command: Command) -> Result<(), Option<anyhow::Error>> {
    Ok(())
}

-
fn run_other(command: Option<&str>, args: &[OsString]) -> Result<(), Option<anyhow::Error>> {
+
fn run_other(command: Option<&str>, args: &[OsString]) -> Result<(), anyhow::Error> {
    match command {
        Some("issue") => {
            term::run_command_args::<tui_issue::Options, _>(
modified bin/terminal.rs
@@ -2,27 +2,25 @@ use std::ffi::OsString;
use std::io::ErrorKind;
use std::process;

-
use anyhow::anyhow;
-

use radicle_cli::terminal;
use radicle_cli::terminal::args;
use radicle_cli::terminal::io;
use radicle_cli::terminal::{Args, Command, DefaultContext, Error, Help};

-
fn _run_rad(args: &[OsString]) -> Result<(), Option<anyhow::Error>> {
+
fn _run_rad(args: &[OsString]) -> Result<(), anyhow::Error> {
    let status = process::Command::new("rad").args(args).status();

    match status {
        Ok(status) => {
            if !status.success() {
-
                return Err(None);
+
                anyhow::bail!("rad binary failed")
            }
        }
        Err(err) => {
            if let ErrorKind::NotFound = err.kind() {
-
                return Err(Some(anyhow!("'rad' was not found.",)));
+
                anyhow::bail!("'rad' was not found.");
            } else {
-
                return Err(Some(err.into()));
+
                return Err(err.into());
            }
        }
    }
@@ -30,7 +28,7 @@ fn _run_rad(args: &[OsString]) -> Result<(), Option<anyhow::Error>> {
    Ok(())
}

-
pub fn run_rad(command: Option<&str>, args: &[OsString]) -> Result<(), Option<anyhow::Error>> {
+
pub fn run_rad(command: Option<&str>, args: &[OsString]) -> Result<(), anyhow::Error> {
    let args = if let Some(command) = command {
        [vec![command.into()], args.to_vec()].concat()
    } else {