use core::{
fmt::{Debug, Formatter},
net::SocketAddr,
};
use bytes::Bytes;
use smoltcp::iface::SocketHandle;
use crate::command;
#[derive(thiserror::Error, Debug, Copy, Clone, PartialEq, Eq)]
pub enum Error {
#[error("connection reset or closed")]
Reset,
}
#[cfg(feature = "std")]
impl From<Error> for std::io::Error {
fn from(value: Error) -> Self {
match value {
Error::Reset => std::io::Error::new(std::io::ErrorKind::ConnectionReset, value),
}
}
}
pub enum Command {
Connect {
local_endpoint: SocketAddr,
remote_endpoint: SocketAddr,
},
Recv {
max_len: Option<usize>,
},
Send {
buf: Bytes,
},
Close,
}
impl Debug for Command {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Self::Connect {
local_endpoint,
remote_endpoint,
} => f
.debug_struct("Connect")
.field("local_endpoint", local_endpoint)
.field("remote_endpoint", remote_endpoint)
.finish(),
Self::Recv { max_len } => f.debug_struct("Recv").field("max_len", max_len).finish(),
Self::Send { buf } => f.debug_struct("Send").field("buf_len", &buf.len()).finish(),
Self::Close => f.write_str("Close"),
}
}
}
impl From<Command> for command::Command {
fn from(value: Command) -> Self {
command::Command::TcpStream(value)
}
}
pub enum Response {
Connected {
handle: SocketHandle,
},
Sent {
n: usize,
},
Recv {
buf: Bytes,
},
Finished,
}
impl From<Response> for command::Response {
fn from(value: Response) -> Self {
Self::TcpStream(value)
}
}
impl Debug for Response {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Self::Connected { handle } => {
f.debug_struct("Connected").field("handle", handle).finish()
}
Self::Sent { n } => f.debug_struct("Sent").field("n", n).finish(),
Self::Recv { buf } => f.debug_struct("Recv").field("buf_len", &buf.len()).finish(),
Self::Finished => f.write_str("Finished"),
}
}
}