tower_web/response/
serializer.rs1use response::{ContentType, SerializerContext};
2use util::tuple::Either2;
3
4use bytes::Bytes;
5use serde::Serialize;
6use void::Void;
7
8pub trait Serializer: Clone + Send + Sync + 'static + ::util::Sealed {
18 type Format: Clone + Send + Sync + 'static;
21
22 fn lookup(&self, name: &str) -> Option<ContentType<Self::Format>>;
25
26 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}