1#[macro_use]
86mod macros;
87
88#[derive(serde::Serialize)]
89struct BorrowedNameWrapper<'a>(#[serde(with = "crate::header_name")] &'a http::HeaderName);
90
91#[derive(serde::Deserialize)]
92struct NameWrapper(#[serde(with = "crate::header_name")] http::HeaderName);
93
94#[derive(serde::Deserialize)]
95#[serde(untagged)]
96enum Either<T> {
97    One(T),
98    Many(Vec<T>),
99}
100
101#[inline]
102fn insert_header_values<'a, M, T>(
103    map: &mut http::HeaderMap<T>,
104    key: http::HeaderName,
105    mut values: impl Iterator<Item = T>,
106) -> Result<(), M::Error>
107where
108    M: serde::de::MapAccess<'a>,
109{
110    if let http::header::Entry::Vacant(e) = map.entry(key) {
111        if let Some(first) = values.next() {
112            let mut e = e.insert_entry(first);
113
114            for val in values {
115                e.append(val);
116            }
117        } else {
118            return Err(serde::de::Error::custom(format!(
119                "no value for header {}",
120                e.key()
121            )));
122        }
123    }
124
125    Ok(())
126}
127
128macro_rules! doc_mod {
129    { $ty:ty, $path:ident$(, $generic:ident)? } => {
130        #[doc = concat!(" [`Serialize`](serde::Serialize)/[`Deserialize`](serde::Deserialize) for [`http::", stringify!($ty), "`]")]
131        #[doc = concat!("use http::", stringify!($ty), ";")]
136        #[doc = concat!("struct MyStruct<T", $(", ", stringify!($generic), )?">")]
140        $(#[doc = concat!("    ", stringify!($generic), ": Serialize + for<'a> Deserialize<'a>,") ])?
143        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "\")]")]
145        #[doc = concat!("    base: ", stringify!($ty), $("<", stringify!($generic), ">",)? ",")]
146        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::option\", default)]")]
148        #[doc = concat!("    option: Option<", stringify!($ty), $("<", stringify!($generic), ">",)? ">,")]
149        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::result\")]")]
151        #[doc = concat!("    result: Result<", stringify!($ty), $("<", stringify!($generic), ">",)? ", T>,")]
152        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::vec\")]")]
154        #[doc = concat!("    vec: Vec<", stringify!($ty), $("<", stringify!($generic), ">",)? ">,")]
155        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::vec_deque\")]")]
157        #[doc = concat!("    vec_deque: VecDeque<", stringify!($ty), $("<", stringify!($generic), ">",)? ">,")]
158        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::linked_list\")]")]
160        #[doc = concat!("    linked_list: LinkedList<", stringify!($ty), $("<", stringify!($generic), ">",)? ">,")]
161        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::hash_map\")]")]
163        #[doc = concat!("    hash_map: HashMap<T, ", stringify!($ty), $("<", stringify!($generic), ">",)? ">,")]
164        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::btree_map\")]")]
166        #[doc = concat!("    btree_map: BTreeMap<T, ", stringify!($ty), $("<", stringify!($generic), ">",)? ">,")]
167        pub mod $path;
170    }
171}
172
173macro_rules! doc_mod_hash {
174    ($ty:ty, $path:ident$(, $extra:expr)?) => {
175        #[doc = concat!(" [`Serialize`](serde::Serialize)/[`Deserialize`](serde::Deserialize) for [`http::"$(, $extra)?, stringify!($ty), "`]")]
176        #[doc = concat!("use http::", $($extra,)? stringify!($ty), ";")]
181        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "\")]")]
190        #[doc = concat!("    base: ", stringify!($ty), ",")]
191        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::option\", default)]")]
193        #[doc = concat!("    option: Option<", stringify!($ty), ">,")]
194        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::result\")]")]
196        #[doc = concat!("    result: Result<", stringify!($ty), ", U>,")]
197        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::vec\")]")]
199        #[doc = concat!("    vec: Vec<", stringify!($ty), ">,")]
200        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::vec_deque\")]")]
202        #[doc = concat!("    vec_deque: VecDeque<", stringify!($ty), ">,")]
203        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::linked_list\")]")]
205        #[doc = concat!("    linked_list: LinkedList<", stringify!($ty), ">,")]
206        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::hash_map\")]")]
208        #[doc = concat!("    hash_map: HashMap<T, ", stringify!($ty), ">,")]
209        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::hash_map_key\")]")]
211        #[doc = concat!("    hash_map_key: HashMap<", stringify!($ty), ", U>,")]
212        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::btree_map\")]")]
214        #[doc = concat!("    btree_map: BTreeMap<T, ", stringify!($ty), ">,")]
215        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::hash_set\")]")]
217        #[doc = concat!("    hash_set: HashSet<", stringify!($ty), ">,")]
218        pub mod $path;
221    };
222}
223
224macro_rules! doc_mod_ord_and_hash {
225    ($ty:ty, $path:ident) => {
226        #[doc = concat!(" [`Serialize`](serde::Serialize)/[`Deserialize`](serde::Deserialize) for [`http::", stringify!($ty), "`]")]
227        #[doc = concat!("use http::", stringify!($ty), ";")]
232        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "\")]")]
241        #[doc = concat!("    base: ", stringify!($ty), ",")]
242        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::option\", default)]")]
244        #[doc = concat!("    option: Option<", stringify!($ty), ">,")]
245        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::result\")]")]
247        #[doc = concat!("    result: Result<", stringify!($ty), ", U>,")]
248        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::vec\")]")]
250        #[doc = concat!("    vec: Vec<", stringify!($ty), ">,")]
251        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::vec_deque\")]")]
253        #[doc = concat!("    vec_deque: VecDeque<", stringify!($ty), ">,")]
254        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::linked_list\")]")]
256        #[doc = concat!("    linked_list: LinkedList<", stringify!($ty), ">,")]
257        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::hash_map\")]")]
259        #[doc = concat!("    hash_map: HashMap<T, ", stringify!($ty), ">,")]
260        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::hash_map_key\")]")]
262        #[doc = concat!("    hash_map_key: HashMap<", stringify!($ty), ", U>,")]
263        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::btree_map\")]")]
265        #[doc = concat!("    btree_map: BTreeMap<T, ", stringify!($ty), ">,")]
266        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::btree_map_key\")]")]
268        #[doc = concat!("    btree_map_key: BTreeMap<", stringify!($ty), ", U>,")]
269        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::hash_set\")]")]
271        #[doc = concat!("    hash_set: HashSet<", stringify!($ty), ">,")]
272        #[doc = concat!("    #[serde(with = \"http_serde_ext::", stringify!($path), "::btree_set\")]")]
274        #[doc = concat!("    btree_set: BTreeSet<", stringify!($ty), ">,")]
275        pub mod $path;
278    };
279}
280
281doc_mod_hash!(Authority, authority, "uri::");
282doc_mod!(HeaderMap, header_map);
283doc_mod!(HeaderMap, header_map_generic, U);
284doc_mod_hash!(HeaderName, header_name);
285doc_mod_ord_and_hash!(HeaderValue, header_value);
286doc_mod_hash!(Method, method);
287doc_mod_hash!(PathAndQuery, path_and_query, "uri::");
288doc_mod!(Request, request, U);
289doc_mod!(Response, response, U);
290doc_mod_hash!(Scheme, scheme, "uri::");
291doc_mod_ord_and_hash!(StatusCode, status_code);
292doc_mod_hash!(Uri, uri);
293doc_mod_ord_and_hash!(Version, version);