webdav_headers/
destination.rs

1// SPDX-FileCopyrightText: d-k-bo <d-k-bo@mailbox.org>
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use crate::{utils::HeaderIteratorExt, DESTINATION};
6
7/// The `Destination` header as defined in [RFC 4918](http://webdav.org/specs/rfc4918.html#HEADER_Destination).
8#[derive(Clone, Debug, PartialEq)]
9pub struct Destination(pub http::Uri);
10
11impl headers::Header for Destination {
12    fn name() -> &'static http::HeaderName {
13        &DESTINATION
14    }
15    fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error>
16    where
17        Self: Sized,
18        I: Iterator<Item = &'i http::HeaderValue>,
19    {
20        Ok(Self(
21            values
22                .extract_str()?
23                .parse()
24                .map_err(|_| headers::Error::invalid())?,
25        ))
26    }
27    fn encode<E: Extend<http::HeaderValue>>(&self, values: &mut E) {
28        values.extend(std::iter::once(self.0.to_string().parse().unwrap()))
29    }
30}
31
32#[cfg(test)]
33#[test]
34fn test() {
35    use crate::test::test_all;
36
37    test_all([
38        (
39            "https://example.com/foo",
40            Destination("https://example.com/foo".parse().unwrap()),
41        ),
42        ("/foo/bar/123", Destination("/foo/bar/123".parse().unwrap())),
43    ])
44}