conjure_http/encoding.rs
1// Copyright 2025 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//! Encoding APIs for serializable bodies.
15
16use erased_serde::{Deserializer, Serializer};
17use http::HeaderValue;
18
19/// An encoding of HTTP bodies.
20pub trait Encoding {
21 /// The encoding's MIME type.
22 fn content_type(&self) -> HeaderValue;
23
24 /// Returns state which will serialize the response body into the provided buffer.
25 fn serializer<'a>(&self, w: &'a mut Vec<u8>) -> Box<dyn SerializerState<'a> + 'a>;
26
27 /// Returns state which will deserialize the request body from the provided buffer.
28 fn deserializer<'a>(&self, buf: &'a [u8]) -> Box<dyn DeserializerState<'a> + 'a>;
29}
30
31/// An intermediate state between an [`Encoding`] and [`Serializer`].
32///
33/// This only exists due to the specifics of [`erased_serde`]'s implementation.
34pub trait SerializerState<'a> {
35 /// Returns the state's internal serializer.
36 fn serializer<'b, 'c>(&'b mut self) -> Box<dyn Serializer + 'c>
37 where
38 'a: 'c,
39 'b: 'c;
40}
41
42/// An intermediate state between an [`Encoding`] and [`Deserializer`].
43///
44/// This only exists due to the specifics of [`erased_serde`]'s implementation.
45pub trait DeserializerState<'de> {
46 /// Returns the state's internal deserializer.
47 fn deserializer<'a>(&'a mut self) -> Box<dyn Deserializer<'de> + 'a>;
48}