use std::{path::PathBuf, process::ExitStatus, str::Utf8Error};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("The environment variable CARGO_LLVM_COV_TARGET_DIR is not set")]
CargoLlvmCovTargetDirNotSet,
#[error("Unable to attach to stdin of command \"{command}\"")]
CommandAttachToStdin {
command: String,
},
#[error("The command \"{command}\" could not be started in the background:\n{source}")]
CommandBackground {
command: String,
source: std::io::Error,
},
#[error("The command \"{command}\" could not be executed:\n{source}")]
CommandExec {
command: String,
source: std::io::Error,
},
#[error(
"The command \"{command}\" exited with non-zero status code \"{exit_status}\":\nstderr:\n{stderr}"
)]
CommandNonZero {
command: String,
exit_status: ExitStatus,
stderr: String,
},
#[error("Unable to write to stdin of command \"{command}\"")]
CommandWriteToStdin {
command: String,
source: std::io::Error,
},
#[error("Unable to to find executable \"{command}\"")]
ExecutableNotFound {
command: String,
source: which::Error,
},
#[error("I/O error while {context}:\n{source}")]
Io {
context: String,
source: std::io::Error,
},
#[error("I/O error at {path} while {context}:\n{source}")]
IoPath {
path: PathBuf,
context: String,
source: std::io::Error,
},
#[error("Password hash error while {context}: {source}")]
PasswordHash {
context: String,
source: yescrypt::password_hash::Error,
},
#[error("Converting a byte vector to a string failed while {context}:\n{source}")]
Utf8String {
context: String,
source: Utf8Error,
},
}
pub trait IoError<T> {
fn map_io_err(self, context: impl ToString) -> Result<T, Error>;
fn map_io_path_err(self, path: impl Into<PathBuf>, context: impl ToString) -> Result<T, Error>;
}
impl<T> IoError<T> for std::io::Result<T> {
fn map_io_path_err(self, path: impl Into<PathBuf>, context: impl ToString) -> Result<T, Error> {
self.map_err(|source| Error::IoPath {
path: path.into(),
context: context.to_string(),
source,
})
}
fn map_io_err(self, context: impl ToString) -> Result<T, Error> {
self.map_err(|source| Error::Io {
context: context.to_string(),
source,
})
}
}