use super::super::imports::*;
use std::{convert::TryFrom, mem};
#[repr(u32)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum PipeDirection {
ClientToServer = PIPE_ACCESS_INBOUND,
ServerToClient = PIPE_ACCESS_OUTBOUND,
Duplex = PIPE_ACCESS_DUPLEX,
}
impl PipeDirection {
pub const fn client_role(self) -> PipeStreamRole {
match self {
Self::ClientToServer => PipeStreamRole::Writer,
Self::ServerToClient => PipeStreamRole::Reader,
Self::Duplex => PipeStreamRole::ReaderAndWriter,
}
}
pub const fn server_role(self) -> PipeStreamRole {
match self {
Self::ClientToServer => PipeStreamRole::Reader,
Self::ServerToClient => PipeStreamRole::Writer,
Self::Duplex => PipeStreamRole::ReaderAndWriter,
}
}
}
impl TryFrom<DWORD> for PipeDirection {
type Error = ();
fn try_from(op: DWORD) -> Result<Self, ()> {
assert!((1..=3).contains(&op));
unsafe { mem::transmute(op) }
}
}
impl From<PipeDirection> for DWORD {
fn from(op: PipeDirection) -> Self {
unsafe { mem::transmute(op) }
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum PipeStreamRole {
Reader,
Writer,
ReaderAndWriter,
}
impl PipeStreamRole {
pub const fn direction_as_server(self) -> PipeDirection {
match self {
Self::Reader => PipeDirection::ClientToServer,
Self::Writer => PipeDirection::ServerToClient,
Self::ReaderAndWriter => PipeDirection::Duplex,
}
}
pub const fn direction_as_client(self) -> PipeDirection {
match self {
Self::Reader => PipeDirection::ServerToClient,
Self::Writer => PipeDirection::ClientToServer,
Self::ReaderAndWriter => PipeDirection::Duplex,
}
}
}
#[repr(u32)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum PipeMode {
Bytes = PIPE_TYPE_BYTE,
Messages = PIPE_TYPE_MESSAGE,
}
impl PipeMode {
pub const fn to_pipe_type(self) -> DWORD {
self as _
}
pub const fn to_readmode(self) -> DWORD {
match self {
Self::Bytes => PIPE_READMODE_BYTE,
Self::Messages => PIPE_READMODE_MESSAGE,
}
}
}
impl TryFrom<DWORD> for PipeMode {
type Error = ();
fn try_from(op: DWORD) -> Result<Self, ()> {
#[allow(unreachable_patterns)] match op {
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE => Ok(Self::Bytes),
PIPE_READMODE_MESSAGE | PIPE_TYPE_MESSAGE => Ok(Self::Messages),
_ => Err(()),
}
}
}