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
//
//   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 nom::{multi::many1_count, Parser};

use super::{
    strings::{mstring, MString},
    MSResult,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MUnsubscriptionRequests<'message> {
    count: usize,
    data: &'message [u8],
}

impl<'message> IntoIterator for MUnsubscriptionRequests<'message> {
    type Item = MUnsubscriptionRequest<'message>;

    type IntoIter = MUnsubscriptionIter<'message>;

    fn into_iter(self) -> Self::IntoIter {
        MUnsubscriptionIter {
            count: self.count,
            data: self.data,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MUnsubscriptionIter<'message> {
    count: usize,
    data: &'message [u8],
}

impl<'message> Iterator for MUnsubscriptionIter<'message> {
    type Item = MUnsubscriptionRequest<'message>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.count == 0 {
            return None;
        }

        self.count -= 1;
        let (rest, request) = munsubscriptionrequest(self.data)
            .expect("Could not parse already validated sub request");
        self.data = rest;

        Some(request)
    }
}

pub fn munsubscriptionrequests(input: &[u8]) -> MSResult<'_, MUnsubscriptionRequests> {
    let data = input;
    let (input, count) = many1_count(munsubscriptionrequest)(input)?;

    Ok((input, MUnsubscriptionRequests { count, data }))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MUnsubscriptionRequest<'message> {
    pub topic: MString<'message>,
}

fn munsubscriptionrequest(input: &[u8]) -> MSResult<'_, MUnsubscriptionRequest> {
    mstring
        .map(|topic| MUnsubscriptionRequest { topic })
        .parse(input)
}