coap_lite/
impl_coap_message_0_3.rs

1use alloc::vec::Vec;
2
3use coap_message_0_3 as coap_message;
4
5use coap_message_0_3::{
6    Code, MinimalWritableMessage, MutableWritableMessage, OptionNumber,
7    ReadableMessage, SeekWritableMessage, WithSortedOptions,
8};
9
10use crate::{CoapOption, MessageClass, Packet};
11
12impl Code for MessageClass {
13    type Error = core::convert::Infallible;
14
15    fn new(code: u8) -> Result<Self, core::convert::Infallible> {
16        Ok(code.into())
17    }
18}
19
20// pub only in name: We don't expose this whole module, so all users will know
21// is that this is a suitable iterator.
22pub struct MessageOptionAdapter<'a> {
23    head: Option<(u16, alloc::collections::linked_list::Iter<'a, Vec<u8>>)>,
24    // right from Packet::options -- fortunately that doesn't say that it
25    // returns an impl Iterator
26    raw_iter: alloc::collections::btree_map::Iter<
27        'a,
28        u16,
29        alloc::collections::linked_list::LinkedList<Vec<u8>>,
30    >,
31}
32
33// pub only in name: We don't expose this whole module, so all users will know
34// is that this implements coap_message::MessageOption
35pub struct MessageOption<'a> {
36    number: u16,
37    value: &'a [u8],
38}
39
40impl<'a> Iterator for MessageOptionAdapter<'a> {
41    type Item = MessageOption<'a>;
42
43    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
44        loop {
45            if let Some((number, values)) = self.head.as_mut() {
46                if let Some(value) = values.next() {
47                    return Some(MessageOption {
48                        number: *number,
49                        value,
50                    });
51                }
52            }
53            let (number, values) = self.raw_iter.next()?;
54            self.head = Some((*number, values.iter()));
55        }
56    }
57}
58
59impl coap_message::MessageOption for MessageOption<'_> {
60    fn number(&self) -> u16 {
61        self.number
62    }
63    fn value(&self) -> &[u8] {
64        self.value
65    }
66}
67
68impl ReadableMessage for Packet {
69    type Code = MessageClass;
70
71    type MessageOption<'a> = MessageOption<'a>;
72    type OptionsIter<'a> = MessageOptionAdapter<'a>;
73
74    fn code(&self) -> Self::Code {
75        self.header.code
76    }
77    fn payload(&self) -> &[u8] {
78        &self.payload
79    }
80    fn options(&self) -> Self::OptionsIter<'_> {
81        MessageOptionAdapter {
82            raw_iter: self.options.iter(),
83            head: None,
84        }
85    }
86}
87
88impl WithSortedOptions for Packet {}
89
90impl OptionNumber for CoapOption {
91    type Error = core::convert::Infallible;
92
93    fn new(option: u16) -> Result<Self, core::convert::Infallible> {
94        Ok(option.into())
95    }
96}
97
98impl MinimalWritableMessage for Packet {
99    type Code = MessageClass;
100    type OptionNumber = CoapOption;
101
102    type AddOptionError = core::convert::Infallible;
103    type SetPayloadError = core::convert::Infallible;
104    type UnionError = core::convert::Infallible;
105
106    fn set_code(&mut self, code: Self::Code) {
107        self.header.code = code;
108    }
109
110    fn add_option(
111        &mut self,
112        option: Self::OptionNumber,
113        data: &[u8],
114    ) -> Result<(), Self::AddOptionError> {
115        self.add_option(option, data.into());
116        Ok(())
117    }
118
119    fn set_payload(
120        &mut self,
121        payload: &[u8],
122    ) -> Result<(), Self::SetPayloadError> {
123        self.payload = payload.into();
124        Ok(())
125    }
126}
127
128impl MutableWritableMessage for Packet {
129    fn available_space(&self) -> usize {
130        usize::MAX
131    }
132    fn payload_mut_with_len(
133        &mut self,
134        len: usize,
135    ) -> Result<&mut [u8], Self::SetPayloadError> {
136        self.payload.resize(len, 0);
137        Ok(&mut self.payload)
138    }
139    fn truncate(
140        &mut self,
141        length: usize,
142    ) -> Result<(), Self::SetPayloadError> {
143        self.payload.truncate(length);
144        Ok(())
145    }
146    fn mutate_options<F>(&mut self, mut callback: F)
147    where
148        F: FnMut(Self::OptionNumber, &mut [u8]),
149    {
150        for (&number, ref mut values) in self.options.iter_mut() {
151            for v in values.iter_mut() {
152                callback(number.into(), v);
153            }
154        }
155    }
156}
157
158impl SeekWritableMessage for Packet {}