use alloc::vec::Vec;
use bytes::{BufMut, Bytes, BytesMut};
use crate::telnet::op_command::{IAC, SB, SE};
use crate::Parser;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct TelnetIAC {
pub command: u8,
}
impl TelnetIAC {
#[must_use]
pub fn new(command: u8) -> Self {
Self { command }
}
#[must_use]
pub fn to_bytes(self) -> Bytes {
Bytes::copy_from_slice(&[IAC, self.command])
}
#[must_use]
#[deprecated(since = "0.2.1", note = "Use `to_bytes` instead.")]
pub fn into_bytes(self) -> Bytes {
self.to_bytes()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct TelnetNegotiation {
pub command: u8,
pub option: u8,
}
impl TelnetNegotiation {
#[must_use]
pub fn new(command: u8, option: u8) -> Self {
Self { command, option }
}
#[must_use]
pub fn to_bytes(self) -> Bytes {
Bytes::copy_from_slice(&[IAC, self.command, self.option])
}
#[must_use]
#[deprecated(since = "0.2.1", note = "Use `to_bytes` instead.")]
pub fn into_bytes(self) -> Bytes {
self.to_bytes()
}
}
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct TelnetSubnegotiation {
pub option: u8,
pub buffer: Bytes,
}
#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for TelnetSubnegotiation {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let option = u.arbitrary()?;
let buffer: Vec<u8> = u.arbitrary()?;
Ok(Self {
option,
buffer: Bytes::from(buffer),
})
}
}
impl TelnetSubnegotiation {
pub fn new(option: u8, buffer: Bytes) -> Self {
Self { option, buffer }
}
#[must_use]
pub fn to_bytes(self) -> Bytes {
let head = [IAC, SB, self.option];
let parsed = &Parser::escape_iac(self.buffer)[..];
let tail = [IAC, SE];
let mut buf = BytesMut::with_capacity(head.len() + parsed.len() + tail.len());
buf.put(&head[..]);
buf.put(parsed);
buf.put(&tail[..]);
buf.freeze()
}
#[must_use]
#[deprecated(since = "0.2.1", note = "Use `to_bytes` instead.")]
pub fn into_bytes(self) -> Bytes {
self.to_bytes()
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TelnetEvents {
IAC(TelnetIAC),
Negotiation(TelnetNegotiation),
Subnegotiation(TelnetSubnegotiation),
DataReceive(Bytes),
DataSend(Bytes),
DecompressImmediate(Bytes),
}
impl From<TelnetIAC> for TelnetEvents {
fn from(iac: TelnetIAC) -> Self {
TelnetEvents::IAC(iac)
}
}
impl From<TelnetNegotiation> for TelnetEvents {
fn from(neg: TelnetNegotiation) -> Self {
TelnetEvents::Negotiation(neg)
}
}
impl From<TelnetSubnegotiation> for TelnetEvents {
fn from(sub: TelnetSubnegotiation) -> Self {
TelnetEvents::Subnegotiation(sub)
}
}
impl TelnetEvents {
#[deprecated(since = "0.2.1", note = "Construct enum variant directly or use into.")]
pub fn build_send(buffer: Bytes) -> Self {
TelnetEvents::DataSend(buffer)
}
#[deprecated(since = "0.2.1", note = "Construct enum variant directly or use into.")]
pub fn build_receive(buffer: Bytes) -> Self {
TelnetEvents::DataReceive(buffer)
}
#[must_use]
#[deprecated(since = "0.2.1", note = "Construct enum variant directly or use into.")]
pub fn build_iac(command: u8) -> TelnetEvents {
TelnetEvents::IAC(TelnetIAC::new(command))
}
#[must_use]
#[deprecated(since = "0.2.1", note = "Construct enum variant directly or use into.")]
pub fn build_negotiation(command: u8, option: u8) -> Self {
TelnetEvents::Negotiation(TelnetNegotiation::new(command, option))
}
#[deprecated(since = "0.2.1", note = "Construct enum variant directly or use into.")]
pub fn build_subnegotiation(option: u8, buffer: Bytes) -> Self {
TelnetEvents::Subnegotiation(TelnetSubnegotiation::new(option, buffer))
}
#[must_use]
pub fn to_bytes(self) -> Bytes {
match self {
TelnetEvents::IAC(iac) => iac.to_bytes(),
TelnetEvents::Negotiation(neg) => neg.to_bytes(),
TelnetEvents::Subnegotiation(sub) => sub.to_bytes(),
TelnetEvents::DataReceive(data)
| TelnetEvents::DataSend(data)
| TelnetEvents::DecompressImmediate(data) => data,
}
}
}
#[allow(clippy::from_over_into)]
impl Into<Bytes> for TelnetIAC {
fn into(self) -> Bytes {
self.to_bytes()
}
}
#[allow(clippy::from_over_into)]
impl Into<Vec<u8>> for TelnetIAC {
fn into(self) -> Vec<u8> {
self.to_bytes().into()
}
}
#[allow(clippy::from_over_into)]
impl Into<Bytes> for TelnetNegotiation {
fn into(self) -> Bytes {
self.to_bytes()
}
}
#[allow(clippy::from_over_into)]
impl Into<Vec<u8>> for TelnetNegotiation {
fn into(self) -> Vec<u8> {
self.to_bytes().into()
}
}
#[allow(clippy::from_over_into)]
impl Into<Bytes> for TelnetSubnegotiation {
fn into(self) -> Bytes {
self.to_bytes()
}
}
#[allow(clippy::from_over_into)]
impl Into<Vec<u8>> for TelnetSubnegotiation {
fn into(self) -> Vec<u8> {
self.to_bytes().into()
}
}