use std::io;
pub use crate::v3::control::{Closed, ControlAck, Disconnect, Error, PeerGone, ProtocolError};
use crate::v3::{codec, control::ControlAckKind, error};
#[non_exhaustive]
#[derive(Debug)]
pub enum Control<E> {
Publish(Publish),
Closed(Closed),
Error(Error<E>),
ProtocolError(ProtocolError),
PeerGone(PeerGone),
}
impl<E> Control<E> {
pub(super) fn publish(pkt: codec::Publish) -> Self {
Control::Publish(Publish(pkt))
}
pub(super) fn closed() -> Self {
Control::Closed(Closed)
}
pub(super) fn error(err: E) -> Self {
Control::Error(Error::new(err))
}
pub(super) fn proto_error(err: error::ProtocolError) -> Self {
Control::ProtocolError(ProtocolError::new(err))
}
pub(super) fn peer_gone(err: Option<io::Error>) -> Self {
Control::PeerGone(PeerGone(err))
}
pub fn disconnect(&self) -> ControlAck {
ControlAck { result: ControlAckKind::Disconnect }
}
pub fn ack(self) -> ControlAck {
match self {
Control::Publish(msg) => msg.ack(),
Control::Closed(msg) => msg.ack(),
Control::Error(msg) => msg.ack(),
Control::ProtocolError(msg) => msg.ack(),
Control::PeerGone(msg) => msg.ack(),
}
}
}
#[derive(Debug)]
pub struct Publish(codec::Publish);
impl Publish {
pub fn packet(&self) -> &codec::Publish {
&self.0
}
pub fn packet_mut(&mut self) -> &mut codec::Publish {
&mut self.0
}
pub fn ack(self) -> ControlAck {
if let Some(id) = self.0.packet_id {
ControlAck { result: ControlAckKind::PublishAck(id) }
} else {
ControlAck { result: ControlAckKind::Nothing }
}
}
pub fn into_inner(self) -> (ControlAck, codec::Publish) {
if let Some(id) = self.0.packet_id {
(ControlAck { result: ControlAckKind::PublishAck(id) }, self.0)
} else {
(ControlAck { result: ControlAckKind::Nothing }, self.0)
}
}
}