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
147
148
149
150
151
152
153
154
155
156
157
158
use alloc::vec::Vec;

use coap_message_0_3 as coap_message;

use coap_message_0_3::{
    Code, MinimalWritableMessage, MutableWritableMessage, OptionNumber,
    ReadableMessage, SeekWritableMessage, WithSortedOptions,
};

use crate::{CoapOption, MessageClass, Packet};

impl Code for MessageClass {
    type Error = core::convert::Infallible;

    fn new(code: u8) -> Result<Self, core::convert::Infallible> {
        Ok(code.into())
    }
}

// pub only in name: We don't expose this whole module, so all users will know
// is that this is a suitable iterator.
pub struct MessageOptionAdapter<'a> {
    head: Option<(u16, alloc::collections::linked_list::Iter<'a, Vec<u8>>)>,
    // right from Packet::options -- fortunately that doesn't say that it
    // returns an impl Iterator
    raw_iter: alloc::collections::btree_map::Iter<
        'a,
        u16,
        alloc::collections::linked_list::LinkedList<Vec<u8>>,
    >,
}

// pub only in name: We don't expose this whole module, so all users will know
// is that this implements coap_message::MessageOption
pub struct MessageOption<'a> {
    number: u16,
    value: &'a [u8],
}

impl<'a> Iterator for MessageOptionAdapter<'a> {
    type Item = MessageOption<'a>;

    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        loop {
            if let Some((number, values)) = self.head.as_mut() {
                if let Some(value) = values.next() {
                    return Some(MessageOption {
                        number: *number,
                        value,
                    });
                }
            }
            let (number, values) = self.raw_iter.next()?;
            self.head = Some((*number, values.iter()));
        }
    }
}

impl<'a> coap_message::MessageOption for MessageOption<'a> {
    fn number(&self) -> u16 {
        self.number
    }
    fn value(&self) -> &[u8] {
        self.value
    }
}

impl ReadableMessage for Packet {
    type Code = MessageClass;

    type MessageOption<'a> = MessageOption<'a>;
    type OptionsIter<'a> = MessageOptionAdapter<'a>;

    fn code(&self) -> Self::Code {
        self.header.code
    }
    fn payload(&self) -> &[u8] {
        &self.payload
    }
    fn options(&self) -> Self::OptionsIter<'_> {
        MessageOptionAdapter {
            raw_iter: self.options.iter(),
            head: None,
        }
    }
}

impl WithSortedOptions for Packet {}

impl OptionNumber for CoapOption {
    type Error = core::convert::Infallible;

    fn new(option: u16) -> Result<Self, core::convert::Infallible> {
        Ok(option.into())
    }
}

impl MinimalWritableMessage for Packet {
    type Code = MessageClass;
    type OptionNumber = CoapOption;

    type AddOptionError = core::convert::Infallible;
    type SetPayloadError = core::convert::Infallible;
    type UnionError = core::convert::Infallible;

    fn set_code(&mut self, code: Self::Code) {
        self.header.code = code;
    }

    fn add_option(
        &mut self,
        option: Self::OptionNumber,
        data: &[u8],
    ) -> Result<(), Self::AddOptionError> {
        self.add_option(option, data.into());
        Ok(())
    }

    fn set_payload(
        &mut self,
        payload: &[u8],
    ) -> Result<(), Self::SetPayloadError> {
        self.payload = payload.into();
        Ok(())
    }
}

impl MutableWritableMessage for Packet {
    fn available_space(&self) -> usize {
        usize::MAX
    }
    fn payload_mut_with_len(
        &mut self,
        len: usize,
    ) -> Result<&mut [u8], Self::SetPayloadError> {
        self.payload.resize(len, 0);
        Ok(&mut self.payload)
    }
    fn truncate(
        &mut self,
        length: usize,
    ) -> Result<(), Self::SetPayloadError> {
        self.payload.truncate(length);
        Ok(())
    }
    fn mutate_options<F>(&mut self, mut callback: F)
    where
        F: FnMut(Self::OptionNumber, &mut [u8]),
    {
        for (&number, ref mut values) in self.options.iter_mut() {
            for v in values.iter_mut() {
                callback(number.into(), v);
            }
        }
    }
}

impl SeekWritableMessage for Packet {}