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
76
77
78
79
//! Data model for request or responses for endpoints

use errors::Result;
use std::fmt;
use {Flavor, Translate, Translator};

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(bound = "F::Type: ::serde::Serialize")]
pub enum RpChannel<F: 'static>
where
    F: Flavor,
{
    /// Single send.
    Unary { ty: F::Type },
    /// Multiple sends.
    Streaming { ty: F::Type },
}

impl<F: 'static> RpChannel<F>
where
    F: Flavor,
{
    /// Get the type of the channel.
    pub fn ty(&self) -> &F::Type {
        use self::RpChannel::*;

        match *self {
            Unary { ref ty, .. } | Streaming { ref ty, .. } => ty,
        }
    }

    /// Check if channel is streaming.
    pub fn is_streaming(&self) -> bool {
        use self::RpChannel::*;

        match *self {
            Unary { .. } => false,
            Streaming { .. } => true,
        }
    }
}

impl<F: 'static> fmt::Display for RpChannel<F>
where
    F: Flavor,
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        if self.is_streaming() {
            write!(fmt, "stream {:?}", self.ty())
        } else {
            write!(fmt, "{:?}", self.ty())
        }
    }
}

impl<F: 'static, T> Translate<T> for RpChannel<F>
where
    F: Flavor,
    T: Translator<Source = F>,
{
    type Source = F;
    type Out = RpChannel<T::Target>;

    /// Translate into different flavor.
    fn translate(self, translator: &T) -> Result<RpChannel<T::Target>> {
        use self::RpChannel::*;

        let out = match self {
            Unary { ty } => Unary {
                ty: translator.translate_type(ty)?,
            },
            Streaming { ty } => Streaming {
                ty: translator.translate_type(ty)?,
            },
        };

        Ok(out)
    }
}