use errors::Result;
use std::fmt;
use {Diagnostics, Flavor, Translate, Translator};
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(bound = "F::Type: ::serde::Serialize")]
pub enum RpChannel<F: 'static>
where
F: Flavor,
{
Unary { ty: F::Type },
Streaming { ty: F::Type },
}
impl<F: 'static> RpChannel<F>
where
F: Flavor,
{
pub fn ty(&self) -> &F::Type {
use self::RpChannel::*;
match *self {
Unary { ref ty, .. } | Streaming { ref ty, .. } => ty,
}
}
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>;
fn translate(self, diag: &mut Diagnostics, translator: &T) -> Result<RpChannel<T::Target>> {
use self::RpChannel::*;
let out = match self {
Unary { ty } => Unary {
ty: translator.translate_type(diag, ty)?,
},
Streaming { ty } => Streaming {
ty: translator.translate_type(diag, ty)?,
},
};
Ok(out)
}
}