reproto_core/
rp_channel.rs

1//! Data model for request or responses for endpoints
2
3use errors::Result;
4use std::fmt;
5use {Diagnostics, Flavor, Translate, Translator};
6
7#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
8#[serde(bound = "F::Type: ::serde::Serialize")]
9pub enum RpChannel<F: 'static>
10where
11    F: Flavor,
12{
13    /// Single send.
14    Unary { ty: F::Type },
15    /// Multiple sends.
16    Streaming { ty: F::Type },
17}
18
19impl<F: 'static> RpChannel<F>
20where
21    F: Flavor,
22{
23    /// Get the type of the channel.
24    pub fn ty(&self) -> &F::Type {
25        use self::RpChannel::*;
26
27        match *self {
28            Unary { ref ty, .. } | Streaming { ref ty, .. } => ty,
29        }
30    }
31
32    /// Check if channel is streaming.
33    pub fn is_streaming(&self) -> bool {
34        use self::RpChannel::*;
35
36        match *self {
37            Unary { .. } => false,
38            Streaming { .. } => true,
39        }
40    }
41}
42
43impl<F: 'static> fmt::Display for RpChannel<F>
44where
45    F: Flavor,
46{
47    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
48        if self.is_streaming() {
49            write!(fmt, "stream {:?}", self.ty())
50        } else {
51            write!(fmt, "{:?}", self.ty())
52        }
53    }
54}
55
56impl<F: 'static, T> Translate<T> for RpChannel<F>
57where
58    F: Flavor,
59    T: Translator<Source = F>,
60{
61    type Source = F;
62    type Out = RpChannel<T::Target>;
63
64    /// Translate into different flavor.
65    fn translate(self, diag: &mut Diagnostics, translator: &T) -> Result<RpChannel<T::Target>> {
66        use self::RpChannel::*;
67
68        let out = match self {
69            Unary { ty } => Unary {
70                ty: translator.translate_type(diag, ty)?,
71            },
72            Streaming { ty } => Streaming {
73                ty: translator.translate_type(diag, ty)?,
74            },
75        };
76
77        Ok(out)
78    }
79}