tower_web/response/
serializer.rs

1use response::{ContentType, SerializerContext};
2use util::tuple::Either2;
3
4use bytes::Bytes;
5use serde::Serialize;
6use void::Void;
7
8/// Serialize an HTTP response body
9///
10/// `Serializer` values use one or more [Serde serializers][serde] to perform
11/// the actual serialization.
12///
13/// The `Serializer` values are also responsible for mapping content-type values
14/// to Serde serializers.
15///
16/// [serde]: https://docs.rs/serde/1.0.71/serde/trait.Serializer.html
17pub trait Serializer: Clone + Send + Sync + 'static + ::util::Sealed {
18    /// A token used by `Serializer` implementations to identify the specific
19    /// serialization format to use when encoding a value.
20    type Format: Clone + Send + Sync + 'static;
21
22    /// Lookup a serializer and `HeaderValue` for the given `Content-Type`
23    /// string.
24    fn lookup(&self, name: &str) -> Option<ContentType<Self::Format>>;
25
26    /// Serialize the value using the specified format.
27    fn serialize<T>(&self, value: &T, format: &Self::Format, context: &SerializerContext)
28        -> Result<Bytes, ::Error>
29    where
30        T: Serialize;
31}
32
33impl Serializer for () {
34    type Format = Void;
35
36    fn lookup(&self, _: &str) -> Option<ContentType<Self::Format>> {
37        None
38    }
39
40    fn serialize<T>(&self, _: &T, _: &Self::Format, _: &SerializerContext)
41        -> Result<Bytes, ::Error>
42    where
43        T: Serialize
44    {
45        unreachable!();
46    }
47}
48
49impl<T, U> Serializer for (T, U)
50where
51    T: Serializer,
52    U: Serializer,
53{
54    type Format = Either2<T::Format, U::Format>;
55
56    fn lookup(&self, name: &str) -> Option<ContentType<Self::Format>> {
57        if let Some(content_type) = self.0.lookup(name) {
58            return Some(content_type.map(Either2::A));
59        }
60
61        self.1.lookup(name)
62            .map(|content_type| content_type.map(Either2::B))
63    }
64
65    fn serialize<V>(&self, value: &V, format: &Self::Format, context: &SerializerContext)
66        -> Result<Bytes, ::Error>
67    where
68        V: Serialize
69    {
70        match *format {
71            Either2::A(ref format) => self.0.serialize(value, format, context),
72            Either2::B(ref format) => self.1.serialize(value, format, context),
73        }
74    }
75}