mqtt_format/v3/
unsubscription_request.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 nom::{multi::many1_count, Parser};
8
9use super::{
10    strings::{mstring, MString},
11    MSResult,
12};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub struct MUnsubscriptionRequests<'message> {
16    count: usize,
17    data: &'message [u8],
18}
19
20impl<'message> IntoIterator for MUnsubscriptionRequests<'message> {
21    type Item = MUnsubscriptionRequest<'message>;
22
23    type IntoIter = MUnsubscriptionIter<'message>;
24
25    fn into_iter(self) -> Self::IntoIter {
26        MUnsubscriptionIter {
27            count: self.count,
28            data: self.data,
29        }
30    }
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub struct MUnsubscriptionIter<'message> {
35    count: usize,
36    data: &'message [u8],
37}
38
39impl<'message> Iterator for MUnsubscriptionIter<'message> {
40    type Item = MUnsubscriptionRequest<'message>;
41
42    fn next(&mut self) -> Option<Self::Item> {
43        if self.count == 0 {
44            return None;
45        }
46
47        self.count -= 1;
48        match munsubscriptionrequest(self.data) {
49            Ok((rest, request)) => {
50                self.data = rest;
51                Some(request)
52            }
53            Err(e) => {
54                unreachable!("Could not parse already validated sub request: {}", e)
55            }
56        }
57    }
58}
59
60pub fn munsubscriptionrequests(input: &[u8]) -> MSResult<'_, MUnsubscriptionRequests> {
61    let data = input;
62    let (input, count) = many1_count(munsubscriptionrequest)(input)?;
63
64    Ok((input, MUnsubscriptionRequests { count, data }))
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub struct MUnsubscriptionRequest<'message> {
69    pub topic: MString<'message>,
70}
71
72fn munsubscriptionrequest(input: &[u8]) -> MSResult<'_, MUnsubscriptionRequest> {
73    mstring
74        .map(|topic| MUnsubscriptionRequest { topic })
75        .parse(input)
76}