mqtt_format/v3/
identifier.rs

1//
2//   This Source Code Form is subject to the terms of the Mozilla Public
3//   License, v. 2.0. If a copy of the MPL was not distributed with this
4//   file, You can obtain one at http://mozilla.org/MPL/2.0/.
5//
6
7use futures::{AsyncWrite, AsyncWriteExt};
8use nom::{number::complete::be_u16, Parser};
9
10use super::{errors::MPacketWriteError, MSResult};
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub struct MPacketIdentifier(pub u16);
14
15pub fn mpacketidentifier(input: &[u8]) -> MSResult<'_, MPacketIdentifier> {
16    be_u16.map(MPacketIdentifier).parse(input)
17}
18impl MPacketIdentifier {
19    pub(crate) async fn write_to<W: AsyncWrite>(
20        &self,
21        writer: &mut std::pin::Pin<&mut W>,
22    ) -> Result<(), MPacketWriteError> {
23        writer.write_all(&self.0.to_be_bytes()).await?;
24        Ok(())
25    }
26
27    pub(crate) fn get_len(&self) -> usize {
28        2
29    }
30}