use std::{mem, num::NonZeroU16};
use ntex::router::Path;
use ntex::util::{ByteString, Bytes};
use serde::de::DeserializeOwned;
use serde_json::Error as JsonError;
use crate::v3::codec;
#[derive(Clone)]
pub struct Publish {
pkt: codec::Publish,
pkt_size: u32,
topic: Path<ByteString>,
}
impl Publish {
#[doc(hidden)]
pub fn new(pkt: codec::Publish, pkt_size: u32) -> Self {
Self { topic: Path::new(pkt.topic.clone()), pkt, pkt_size }
}
#[inline]
pub fn dup(&self) -> bool {
self.pkt.dup
}
#[inline]
pub fn retain(&self) -> bool {
self.pkt.retain
}
#[inline]
pub fn qos(&self) -> codec::QoS {
self.pkt.qos
}
#[inline]
pub fn publish_topic(&self) -> &str {
&self.pkt.topic
}
#[inline]
pub fn id(&self) -> Option<NonZeroU16> {
self.pkt.packet_id
}
#[inline]
pub fn topic(&self) -> &Path<ByteString> {
&self.topic
}
#[inline]
pub fn topic_mut(&mut self) -> &mut Path<ByteString> {
&mut self.topic
}
#[inline]
pub fn packet(&self) -> &codec::Publish {
&self.pkt
}
#[inline]
pub fn packet_mut(&mut self) -> &mut codec::Publish {
&mut self.pkt
}
#[inline]
pub fn packet_size(&self) -> u32 {
self.pkt_size
}
#[inline]
pub fn payload(&self) -> &Bytes {
&self.pkt.payload
}
pub fn take_payload(&mut self) -> Bytes {
mem::take(&mut self.pkt.payload)
}
pub fn json<T: DeserializeOwned>(&mut self) -> Result<T, JsonError> {
serde_json::from_slice(&self.pkt.payload)
}
pub(super) fn into_inner(self) -> codec::Publish {
self.pkt
}
}
impl std::fmt::Debug for Publish {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.pkt.fmt(f)
}
}