conjure_http/server/
encoding.rs

1// Copyright 2024 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
15use conjure_serde::{json, smile};
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}
49
50/// An [`Encoding`] using [`conjure_serde::json`](module@conjure_serde::json).
51pub struct JsonEncoding;
52
53impl Encoding for JsonEncoding {
54    fn content_type(&self) -> HeaderValue {
55        HeaderValue::from_static("application/json")
56    }
57
58    fn serializer<'a>(&self, w: &'a mut Vec<u8>) -> Box<dyn SerializerState<'a> + 'a> {
59        Box::new(JsonSerializerState {
60            serializer: json::Serializer::new(w),
61        })
62    }
63
64    fn deserializer<'a>(&self, buf: &'a [u8]) -> Box<dyn DeserializerState<'a> + 'a> {
65        Box::new(JsonDeserializerState {
66            deserializer: json::ServerDeserializer::from_slice(buf),
67        })
68    }
69}
70
71struct JsonSerializerState<'a> {
72    serializer: json::Serializer<&'a mut Vec<u8>>,
73}
74
75impl<'a> SerializerState<'a> for JsonSerializerState<'a> {
76    fn serializer<'b, 'c>(&'b mut self) -> Box<dyn Serializer + 'c>
77    where
78        'a: 'c,
79        'b: 'c,
80    {
81        Box::new(<dyn Serializer>::erase(&mut self.serializer))
82    }
83}
84
85struct JsonDeserializerState<'de> {
86    deserializer: json::ServerDeserializer<json::SliceRead<'de>>,
87}
88
89impl<'de> DeserializerState<'de> for JsonDeserializerState<'de> {
90    fn deserializer<'a>(&'a mut self) -> Box<dyn Deserializer<'de> + 'a> {
91        Box::new(<dyn Deserializer>::erase(&mut self.deserializer))
92    }
93}
94
95/// An [`Encoding`] using [`conjure_serde::smile`](module@conjure_serde::smile).
96pub struct SmileEncoding;
97
98impl Encoding for SmileEncoding {
99    fn content_type(&self) -> HeaderValue {
100        HeaderValue::from_static("application/x-jackson-smile")
101    }
102
103    fn serializer<'a>(&self, w: &'a mut Vec<u8>) -> Box<dyn SerializerState<'a> + 'a> {
104        Box::new(SmileSerializerState {
105            serializer: smile::Serializer::new(w),
106        })
107    }
108
109    fn deserializer<'a>(&self, buf: &'a [u8]) -> Box<dyn DeserializerState<'a> + 'a> {
110        Box::new(SmileDeserializerState {
111            deserializer: smile::ServerDeserializer::from_slice(buf),
112        })
113    }
114}
115
116struct SmileSerializerState<'a> {
117    serializer: smile::Serializer<&'a mut Vec<u8>>,
118}
119
120impl<'a> SerializerState<'a> for SmileSerializerState<'a> {
121    fn serializer<'b, 'c>(&'b mut self) -> Box<dyn Serializer + 'c>
122    where
123        'a: 'c,
124        'b: 'c,
125    {
126        Box::new(<dyn Serializer>::erase(&mut self.serializer))
127    }
128}
129
130struct SmileDeserializerState<'de> {
131    deserializer: smile::ServerDeserializer<'de, smile::SliceRead<'de>>,
132}
133
134impl<'de> DeserializerState<'de> for SmileDeserializerState<'de> {
135    fn deserializer<'a>(&'a mut self) -> Box<dyn Deserializer<'de> + 'a> {
136        Box::new(<dyn Deserializer>::erase(&mut self.deserializer))
137    }
138}