1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use response::{ContentType, SerializerContext};
use util::tuple::Either2;

use bytes::Bytes;
use serde::Serialize;
use void::Void;

/// Serialize an HTTP response body
///
/// `Serializer` values use one or more [Serde serializers][serde] to perform
/// the actual serialization.
///
/// The `Serializer` values are also responsible for mapping content-type values
/// to Serde serializers.
///
/// [serde]: https://docs.rs/serde/1.0.71/serde/trait.Serializer.html
pub trait Serializer: Clone + Send + Sync + 'static + ::util::Sealed {
    /// A token used by `Serializer` implementations to identify the specific
    /// serialization format to use when encoding a value.
    type Format: Clone + Send + Sync + 'static;

    /// Lookup a serializer and `HeaderValue` for the given `Content-Type`
    /// string.
    fn lookup(&self, name: &str) -> Option<ContentType<Self::Format>>;

    /// Serialize the value using the specified format.
    fn serialize<T>(&self, value: &T, format: &Self::Format, context: &SerializerContext)
        -> Result<Bytes, ::Error>
    where
        T: Serialize;
}

impl Serializer for () {
    type Format = Void;

    fn lookup(&self, _: &str) -> Option<ContentType<Self::Format>> {
        None
    }

    fn serialize<T>(&self, _: &T, _: &Self::Format, _: &SerializerContext)
        -> Result<Bytes, ::Error>
    where
        T: Serialize
    {
        unreachable!();
    }
}

impl<T, U> Serializer for (T, U)
where
    T: Serializer,
    U: Serializer,
{
    type Format = Either2<T::Format, U::Format>;

    fn lookup(&self, name: &str) -> Option<ContentType<Self::Format>> {
        if let Some(content_type) = self.0.lookup(name) {
            return Some(content_type.map(Either2::A));
        }

        self.1.lookup(name)
            .map(|content_type| content_type.map(Either2::B))
    }

    fn serialize<V>(&self, value: &V, format: &Self::Format, context: &SerializerContext)
        -> Result<Bytes, ::Error>
    where
        V: Serialize
    {
        match *format {
            Either2::A(ref format) => self.0.serialize(value, format, context),
            Either2::B(ref format) => self.1.serialize(value, format, context),
        }
    }
}