use std::io;
use thiserror::Error;
use winapi::shared::winerror::ERROR_PARTIAL_COPY;
#[derive(Debug, Error)]
pub enum ProcessError {
#[error("inaccessible target process")]
ProcessInaccessible,
#[error("io error: {}", _0)]
Io(io::Error),
}
impl From<io::Error> for ProcessError {
fn from(err: io::Error) -> Self {
if err.raw_os_error() == Some(ERROR_PARTIAL_COPY as _)
|| err.kind() == io::ErrorKind::PermissionDenied
{
Self::ProcessInaccessible
} else {
Self::Io(err)
}
}
}
#[derive(Debug, Error)]
pub enum ProcessOrPathError {
#[error("process error: {}", _0)]
Process(#[from] ProcessError),
#[error("path contains illegal interior nul")]
IllegalPath,
}
impl From<io::Error> for ProcessOrPathError {
fn from(err: io::Error) -> Self {
Self::Process(err.into())
}
}
impl From<widestring::error::ContainsNul<u16>> for ProcessOrPathError {
fn from(_err: widestring::error::ContainsNul<u16>) -> Self {
Self::IllegalPath
}
}
impl From<std::ffi::FromBytesWithNulError> for ProcessOrPathError {
fn from(_err: std::ffi::FromBytesWithNulError) -> Self {
Self::IllegalPath
}
}
impl From<std::ffi::NulError> for ProcessOrPathError {
fn from(_err: std::ffi::NulError) -> Self {
Self::IllegalPath
}
}
#[derive(Debug, Error)]
pub enum IoOrNulError {
#[error("interior nul found")]
Nul(#[from] widestring::error::ContainsNul<u16>),
#[error("io error: {}", _0)]
Io(#[from] io::Error),
}
#[derive(Debug, Error)]
pub enum GetLocalProcedureAddressError {
#[error("inaccessible target process")]
ProcessInaccessible,
#[error("path contains illegal interior nul")]
IllegalPath,
#[error("io error: {}", _0)]
Io(io::Error),
#[error("unsupported remote target process")]
UnsupportedRemoteTarget,
}
impl From<io::Error> for GetLocalProcedureAddressError {
fn from(err: io::Error) -> Self {
ProcessError::from(err).into()
}
}
impl From<ProcessError> for GetLocalProcedureAddressError {
fn from(err: ProcessError) -> Self {
match err {
ProcessError::ProcessInaccessible => Self::ProcessInaccessible,
ProcessError::Io(e) => Self::Io(e),
}
}
}
impl From<ProcessOrPathError> for GetLocalProcedureAddressError {
fn from(err: ProcessOrPathError) -> Self {
match err {
ProcessOrPathError::Process(e) => e.into(),
ProcessOrPathError::IllegalPath => Self::IllegalPath,
}
}
}
impl From<widestring::error::ContainsNul<u16>> for GetLocalProcedureAddressError {
fn from(_err: widestring::error::ContainsNul<u16>) -> Self {
Self::IllegalPath
}
}
impl From<std::ffi::FromBytesWithNulError> for GetLocalProcedureAddressError {
fn from(_err: std::ffi::FromBytesWithNulError) -> Self {
Self::IllegalPath
}
}
impl From<std::ffi::NulError> for GetLocalProcedureAddressError {
fn from(_err: std::ffi::NulError) -> Self {
Self::IllegalPath
}
}