Skip to main content

any_dialog/
error.rs

1//! The crate's error type.
2
3use std::fmt;
4use std::path::PathBuf;
5use std::process::ExitStatus;
6
7/// Everything that can go wrong while showing a dialog.
8#[derive(Debug)]
9pub enum Error {
10    /// None of the backends (`kdialog`, `zenity`, `osascript`,
11    /// `powershell`) was found on this system.
12    NoBackend,
13    /// The helper could not be started.
14    Spawn {
15        program: PathBuf,
16        source: std::io::Error,
17    },
18    /// The helper exited with a status that does not mean success or cancel.
19    Exit {
20        program: PathBuf,
21        status: ExitStatus,
22    },
23}
24
25impl fmt::Display for Error {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Error::NoBackend => {
29                write!(
30                    f,
31                    "no dialog backend available (none of kdialog, zenity, osascript, or powershell was found)"
32                )
33            }
34            Error::Spawn { program, source } => {
35                write!(f, "could not run {}: {source}", program.display())
36            }
37            Error::Exit { program, status } => {
38                write!(f, "{} exited with {status}", program.display())
39            }
40        }
41    }
42}
43
44impl std::error::Error for Error {
45    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46        match self {
47            Error::Spawn { source, .. } => Some(source),
48            _ => None,
49        }
50    }
51}