0

I have a UI application written in iced, which performs various operation. To launch the UI I'm using run() method: UpdateDriver::run(Settings::default()) and currently this returns the exit code of type iced::Result and type of result is pub type Result = Result<(), Error>;, but I want the application to return custom exit code of type u8 based on the operation performed.

Ex: 0(success), 1(failure), 3(reboot) etc.

I need help on how to do that.

Current WorkFlow:

fn launch_ui_mode() -> iced::Result {
    UpdateDriver::run(Settings::default())
}

Expectation:

fn launch_ui_mode() -> u8 {
    UpdateDriver::run(Settings::default())
}

Main Function:

fn main() -> ExitCode {

    let retcode: u8;
    let exec_type = get_execution_mode();

    if exec_mode == UI{ 
        retcode = launch_ui_mode(); //This is iced::result type, I need u8 type
    }
    else{
        retcode = launch_cli_mode();
    }

    return ExitCode::from(retcode);
}

My application runs in both CLI and UI mode, for CLI mode I'm directly returning the u8 type exit code, need to implement that for UI mode too.

So if something like this is not possible, what is the other way to return the exit-code from the UI. Should I add the global variable to store the exit code in UI part of code? Will it be safe?

  • At the risk of stating the obvious, did you try `process::exit`? Or you could create your own error enum and use `map_err` on the iced result and map that to an exit code at a higher level depending on your setup. Speaking of which, where are you expecting to process these exit codes? Or are you just trying to capture them in e.g. systemd? – Jared Smith May 19 '23 at 11:59
  • Are you asking how to [match on an enum](https://doc.rust-lang.org/book/ch06-00-enums.html) to convert a `iced::Result` into an `u8`? – cafce25 May 19 '23 at 12:23
  • So, iced::Result is of type `pub type Result = Result<(), Error>;`, it returns empty() when success, else it returns Error kind in case of err. I want it to return u8 type value or `pub type Result = Result;` – Mayank Joshi May 19 '23 at 12:48

1 Answers1

0

I don't have experience with iced, but since Rust 1.61 you can return from main any type that implements Termination trait. So you could create some wrapper (new-type pattern) for iced::Result and implement Termination for it. And then depending on your implementation you can exit with custom exit codes.

cafce25
  • 15,907
  • 4
  • 25
  • 31
Aleksander Krauze
  • 3,115
  • 7
  • 18
  • Thanks for the suggestion, I'm already using the std::process::ExitCode in the main, as shown above. I need to get the exit code of type u8, from the iced library – Mayank Joshi May 19 '23 at 12:06