summaryrefslogtreecommitdiffstats
path: root/mqtt-format/src/v5/packets/unsubscribe.rs
blob: 399ec860204c0368d3c9cd1a0681f16636225999 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
//   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::combinator::repeat_till;
use winnow::Bytes;
use winnow::Parser;

use crate::v5::properties::define_properties;
use crate::v5::strings::parse_string;
use crate::v5::strings::write_string;
use crate::v5::variable_header::PacketIdentifier;
use crate::v5::variable_header::SubscriptionIdentifier;
use crate::v5::variable_header::UserProperties;
use crate::v5::write::WResult;
use crate::v5::write::WriteMqttPacket;
use crate::v5::MResult;

define_properties! {
    packet_type: MUnsubscribe,
    anker: "_Toc3901182",
    pub struct UnsubscribeProperties<'i> {
        (anker: "_Toc3901183")
        subscription_identifier: SubscriptionIdentifier,

        (anker: "_Toc3901183")
        user_properties: UserProperties<'i>,
    }
}

pub struct Unsubscriptions<'i> {
    start: &'i [u8],
}

impl<'i> core::fmt::Debug for Unsubscriptions<'i> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Unsubscriptions").finish()
    }
}

impl<'i> Unsubscriptions<'i> {
    fn parse(input: &mut &'i Bytes) -> MResult<Unsubscriptions<'i>> {
        winnow::combinator::trace("Unsubscriptions", |input: &mut &'i Bytes| {
            let start = repeat_till::<_, _, (), _, _, _, _>(
                1..,
                Unsubscription::parse,
                winnow::combinator::eof,
            )
            .recognize()
            .parse_next(input)?;

            Ok(Unsubscriptions { start })
        })
        .parse_next(input)
    }

    pub async fn write<W: WriteMqttPacket>(&self, buffer: &mut W) -> WResult<W> {
        for unsub in self.iter() {
            unsub.write(buffer).await?;
        }

        Ok(())
    }

    pub fn iter(&self) -> UnsubscriptionsIter<'i> {
        UnsubscriptionsIter {
            current: Bytes::new(self.start),
        }
    }
}

#[allow(missing_debug_implementations)]
pub struct UnsubscriptionsIter<'i> {
    current: &'i Bytes,
}

impl<'i> Iterator for UnsubscriptionsIter<'i> {
    type Item = Unsubscription<'i>;

    fn next(&mut self) -> Option<Self::Item> {
        if !self.current.is_empty() {
            let sub = Unsubscription::parse(&mut self.current)
                .expect("Already parsed subscriptions should be valid");

            return Some(sub);
        }

        None
    }
}

#[derive(Debug)]
pub struct Unsubscription<'i> {
    pub topic_filter: &'i str,
}

impl<'i> Unsubscription<'i> {
    fn parse(input: &mut &'i Bytes) -> MResult<Self> {
        winnow::combinator::trace("Unsubscription", |input: &mut &'i Bytes| {
            let topic_filter = parse_string(input)?;

            Ok(Unsubscription { topic_filter })
        })
        .parse_next(input)
    }

    pub async fn write<W: WriteMqttPacket>(&self, buffer: &mut W) -> WResult<W> {
        write_string(buffer, self.topic_filter).await
    }
}

#[derive(Debug)]
#[doc = crate::v5::util::md_speclink!("_Toc3901179")]
pub struct MUnsubscribe<'i> {
    pub packet_identifier: PacketIdentifier,
    pub properties: UnsubscribeProperties<'i>,
    pub unsubscriptions: Unsubscriptions<'i>,
}

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

            Ok(MUnsubscribe {
                packet_identifier,
                properties,
                unsubscriptions,
            })
        })
        .parse_next(input)
    }

    pub async fn write<W: WriteMqttPacket>(&self, buffer: &mut W) -> WResult<W> {
        self.packet_identifier.write(buffer).await?;
        self.properties.write(buffer).await?;
        self.unsubscriptions.write(buffer).await
    }
}