use bytes::Bytes;
use crate::{DecodeError, varint};
pub const PROP_ID_LEN: usize = 1;
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum PropertyId {
PayloadFormatIndicator = 0x01,
MessageExpiryInterval = 0x02,
ContentType = 0x03,
ResponseTopic = 0x08,
CorrelationData = 0x09,
SubscriptionIdentifier = 0x0B,
SessionExpiryInterval = 0x11,
AssignedClientIdentifier = 0x12,
ServerKeepAlive = 0x13,
AuthenticationMethod = 0x15,
AuthenticationData = 0x16,
RequestProblemInformation = 0x17,
WillDelayInterval = 0x18,
RequestResponseInformation = 0x19,
ResponseInformation = 0x1A,
ServerReference = 0x1C,
ReasonString = 0x1F,
ReceiveMaximum = 0x21,
TopicAliasMaximum = 0x22,
TopicAlias = 0x23,
MaximumQoS = 0x24,
RetainAvailable = 0x25,
UserProperty = 0x26,
MaximumPacketSize = 0x27,
WildcardSubscriptionAvailable = 0x28,
SubscriptionIdentifierAvailable = 0x29,
SharedSubscriptionAvailable = 0x2A,
}
impl TryFrom<u8> for PropertyId {
type Error = DecodeError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0x01 => Ok(Self::PayloadFormatIndicator),
0x02 => Ok(Self::MessageExpiryInterval),
0x03 => Ok(Self::ContentType),
0x08 => Ok(Self::ResponseTopic),
0x09 => Ok(Self::CorrelationData),
0x0B => Ok(Self::SubscriptionIdentifier),
0x11 => Ok(Self::SessionExpiryInterval),
0x12 => Ok(Self::AssignedClientIdentifier),
0x13 => Ok(Self::ServerKeepAlive),
0x15 => Ok(Self::AuthenticationMethod),
0x16 => Ok(Self::AuthenticationData),
0x17 => Ok(Self::RequestProblemInformation),
0x18 => Ok(Self::WillDelayInterval),
0x19 => Ok(Self::RequestResponseInformation),
0x1A => Ok(Self::ResponseInformation),
0x1C => Ok(Self::ServerReference),
0x1F => Ok(Self::ReasonString),
0x21 => Ok(Self::ReceiveMaximum),
0x22 => Ok(Self::TopicAliasMaximum),
0x23 => Ok(Self::TopicAlias),
0x24 => Ok(Self::MaximumQoS),
0x25 => Ok(Self::RetainAvailable),
0x26 => Ok(Self::UserProperty),
0x27 => Ok(Self::MaximumPacketSize),
0x28 => Ok(Self::WildcardSubscriptionAvailable),
0x29 => Ok(Self::SubscriptionIdentifierAvailable),
0x2A => Ok(Self::SharedSubscriptionAvailable),
_ => Err(DecodeError::ProtocolError {
reason: "invalid property identifier",
}),
}
}
}
impl PropertyId {
pub const PAYLOAD_FORMAT_INDICATOR_LEN: usize = 1;
pub const MESSAGE_EXPIRY_INTERVAL_LEN: usize = 4;
pub const SESSION_EXPIRY_INTERVAL_LEN: usize = 4;
pub const RECEIVE_MAXIMUM_LEN: usize = 2;
pub const MAXIMUM_PACKET_SIZE_LEN: usize = 4;
pub const TOPIC_ALIAS_MAXIMUM_LEN: usize = 2;
pub const TOPIC_ALIAS_LEN: usize = 2;
pub const REQUEST_RESPONSE_INFORMATION_LEN: usize = 1;
pub const REQUEST_PROBLEM_INFORMATION_LEN: usize = 1;
pub const MAXIMUM_QOS_LEN: usize = 1;
pub const RETAIN_AVAILABLE_LEN: usize = 1;
pub const WILDCARD_SUBSCRIPTION_AVAILABLE_LEN: usize = 1;
pub const SUBSCRIPTION_IDENTIFIER_AVAILABLE_LEN: usize = 1;
pub const SHARED_SUBSCRIPTION_AVAILABLE_LEN: usize = 1;
pub const SERVER_KEEP_ALIVE_LEN: usize = 2;
pub const WILL_DELAY_INTERVAL_LEN: usize = 4;
}
pub(crate) fn read_properties_bytes(buf: &mut Bytes) -> Result<Bytes, DecodeError> {
let len = varint::decode(buf)? as usize;
if buf.len() < len {
return Err(DecodeError::InsufficientBytes {
needed: len - buf.len(),
});
}
Ok(buf.split_to(len))
}
pub(crate) fn properties_encoded_len(body_len: usize) -> usize {
varint::len(body_len as u32) + body_len
}
pub(crate) fn write_properties_len(body_len: usize, buf: &mut bytes::BytesMut) {
let mut tmp = [0u8; 4];
let n = varint::encode_to(body_len as u32, &mut tmp);
buf.extend_from_slice(&tmp[..n]);
}