async_coap/send_desc/
uri_host_path.rs

1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use super::*;
17use std::marker::PhantomData;
18
19impl<SD: SendDescUnicast, IC> SendDescUnicast for UriHostPath<SD, IC> {}
20impl<SD: SendDescMulticast, IC> SendDescMulticast for UriHostPath<SD, IC> {}
21
22/// Combinator for Send Descriptors created by [`SendDescExt::uri_host_path`].
23#[derive(Debug)]
24pub struct UriHostPath<SD, IC> {
25    pub(super) inner: SD,
26    pub(super) host: Option<String>,
27    pub(super) path_and_query: RelRefBuf,
28    pub(super) phantom: PhantomData<IC>,
29}
30
31impl<SD, IC, R> SendDesc<IC, R> for UriHostPath<SD, IC>
32where
33    SD: SendDesc<IC, R>,
34    IC: InboundContext,
35    R: Send,
36{
37    send_desc_passthru_timing!(inner);
38    send_desc_passthru_handler!(inner, R);
39    send_desc_passthru_payload!(inner);
40
41    fn write_options(
42        &self,
43        msg: &mut dyn OptionInsert,
44        socket_addr: &IC::SocketAddr,
45        start: Bound<OptionNumber>,
46        end: Bound<OptionNumber>,
47    ) -> Result<(), Error> {
48        // We define this up here at the top of the method so that it doesn't
49        // go out of scope.
50        let mut unescape_buf;
51
52        write_options!((msg, socket_addr, start, end, self.inner) {
53            URI_HOST => self.host.iter(),
54            URI_PATH => {
55                unescape_buf = self.path_and_query.clone().into_unescape_buf();
56                unescape_buf.path_segments()
57            },
58            URI_QUERY => {
59                unescape_buf = self.path_and_query.clone().into_unescape_buf();
60                unescape_buf.query_items()
61            },
62        })
63    }
64}