conjure_http/
path_params.rs

1// Copyright 2019 Palantir Technologies, Inc.
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// http://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//! Path parameters.
16
17use std::collections::hash_map::{self, HashMap};
18use std::ops::Index;
19
20/// A data structure storing the raw, encoded, path parameters of the request.
21#[derive(Clone, Debug, Default, PartialEq, Eq)]
22pub struct PathParams(HashMap<String, String>);
23
24impl PathParams {
25    /// Creates a new, empty `PathParams`.
26    #[inline]
27    pub fn new() -> PathParams {
28        PathParams::default()
29    }
30
31    /// Inserts a parameter.
32    #[inline]
33    pub fn insert<T, U>(&mut self, key: T, value: U)
34    where
35        T: Into<String>,
36        U: Into<String>,
37    {
38        self.0.insert(key.into(), value.into());
39    }
40
41    /// Returns an iterator over the parameters.
42    #[inline]
43    pub fn iter(&self) -> Iter<'_> {
44        Iter(self.0.iter())
45    }
46}
47
48impl<'a> IntoIterator for &'a PathParams {
49    type IntoIter = Iter<'a>;
50    type Item = (&'a str, &'a str);
51
52    #[inline]
53    fn into_iter(self) -> Iter<'a> {
54        self.iter()
55    }
56}
57
58impl<'a> Index<&'a str> for PathParams {
59    type Output = str;
60
61    #[inline]
62    fn index(&self, key: &'a str) -> &str {
63        &self.0[key]
64    }
65}
66
67/// An iterator over path parameters.
68pub struct Iter<'a>(hash_map::Iter<'a, String, String>);
69
70impl<'a> Iterator for Iter<'a> {
71    type Item = (&'a str, &'a str);
72
73    #[inline]
74    fn next(&mut self) -> Option<(&'a str, &'a str)> {
75        self.0.next().map(|v| (&**v.0, &**v.1))
76    }
77
78    #[inline]
79    fn size_hint(&self) -> (usize, Option<usize>) {
80        self.0.size_hint()
81    }
82}
83
84impl ExactSizeIterator for Iter<'_> {
85    #[inline]
86    fn len(&self) -> usize {
87        self.0.len()
88    }
89}