use core::result::Result as CoreResult;
use std::time::Duration;
use thiserror::Error as ThisError;
#[derive(ThisError, Debug, Clone)]
pub enum CommandError {
#[error("Failed to spawn command '{cmd}': {message}")]
SpawnFailed {
cmd: String,
message: String,
},
#[error("Command execution failed for '{cmd}': {message}")]
ExecutionFailed {
cmd: String,
message: String,
},
#[error("Command '{cmd}' failed with exit code {code:?}. Stderr: {stderr}")]
NonZeroExitCode {
cmd: String,
code: Option<i32>,
stderr: String,
},
#[error("Command timed out after {duration:?}")]
Timeout {
duration: Duration,
},
#[error("Command was killed: {reason}")]
Killed {
reason: String,
},
#[error("Invalid command configuration: {description}")]
Configuration {
description: String,
},
#[error("Failed to capture {stream} stream")]
CaptureFailed {
stream: String,
},
#[error("Error reading {stream} stream: {message}")]
StreamReadError {
stream: String,
message: String,
},
#[error("Command processing error: {0}")]
Generic(String),
}
pub type CommandResult<T> = CoreResult<T, CommandError>;
impl AsRef<str> for CommandError {
fn as_ref(&self) -> &str {
match self {
CommandError::SpawnFailed { .. } => "CommandError::SpawnFailed",
CommandError::ExecutionFailed { .. } => "CommandError::ExecutionFailed",
CommandError::NonZeroExitCode { .. } => "CommandError::NonZeroExitCode",
CommandError::Timeout { .. } => "CommandError::Timeout",
CommandError::Killed { .. } => "CommandError::Killed",
CommandError::Configuration { .. } => "CommandError::Configuration",
CommandError::CaptureFailed { .. } => "CommandError::CaptureFailed",
CommandError::StreamReadError { .. } => "CommandError::StreamReadError",
CommandError::Generic(_) => "CommandError::Generic",
}
}
}