summaryrefslogtreecommitdiffstats
path: root/mqtt-format/src/v5/packets/unsuback.rs
blob: d372da0f15b9dc3fb8e13a3fe49b0f9555c4cbe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
//   This Source Code Form is subject to the terms of the Mozilla Public
//   License, v. 2.0. If a copy of the MPL was not distributed with this
//   file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

use winnow::Bytes;
use winnow::Parser;

use crate::v5::variable_header::PacketIdentifier;
use crate::v5::variable_header::ReasonString;
use crate::v5::variable_header::UserProperties;
use crate::v5::MResult;

crate::v5::reason_code::make_combined_reason_code! {
    pub enum UnsubackReasonCode {
        ImplementationSpecificError = crate::v5::reason_code::ImplementationSpecificError,
        NoSubscriptionExisted = crate::v5::reason_code::NoSubscriptionExisted,
        NotAuthorized = crate::v5::reason_code::NotAuthorized,
        PacketIdentifierInUse = crate::v5::reason_code::PacketIdentifierInUse,
        Success = crate::v5::reason_code::Success,
        TopicFilterInvalid = crate::v5::reason_code::TopicFilterInvalid,
        UnspecifiedError = crate::v5::reason_code::UnspecifiedError,
    }
}

crate::v5::properties::define_properties! {
    pub struct UnsubackProperties<'i> {
        reason_string: ReasonString<'i>,
        user_properties: UserProperties<'i>,
    }
}

#[derive(Debug)]
#[doc = crate::v5::util::md_speclink!("_Toc3901187")]
pub struct MUnsuback<'i> {
    pub packet_identifier: PacketIdentifier,
    pub properties: UnsubackProperties<'i>,
    pub reasons: &'i [UnsubackReasonCode],
}

impl<'i> MUnsuback<'i> {
    pub fn parse(input: &mut &'i Bytes) -> MResult<Self> {
        winnow::combinator::trace("MUnsuback", |input: &mut &'i Bytes| {
            let packet_identifier = PacketIdentifier::parse(input)?;
            let properties = UnsubackProperties::parse(input)?;

            // Verify that the payload only contains valid reason codes
            let payload: &[u8] = winnow::combinator::repeat_till::<_, _, (), _, _, _, _>(
                0..,
                UnsubackReasonCode::parse,
                winnow::combinator::eof,
            )
            .recognize()
            .parse_next(input)?;

            // SAFETY: We verified above that the payload slice only contains valid UnsubackReasonCode
            // bytes
            let reasons: &[UnsubackReasonCode] = unsafe { core::mem::transmute(payload) };

            Ok(Self {
                packet_identifier,
                properties,
                reasons,
            })
        })
        .parse_next(input)
    }
}