use crate::envelope::StreamItem;
use crate::error::TransportError;
use futures_core::Stream;
use serde_json::Value;
use std::fmt;
use std::pin::Pin;
#[cfg(not(target_arch = "wasm32"))]
pub type ItemStream<'a> =
Pin<Box<dyn Stream<Item = Result<StreamItem, TransportError>> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type ItemStream<'a> = Pin<Box<dyn Stream<Item = Result<StreamItem, TransportError>> + 'a>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MethodRef {
namespace: &'static str,
method: &'static str,
}
impl MethodRef {
pub const fn new(namespace: &'static str, method: &'static str) -> Self {
Self { namespace, method }
}
pub const fn namespace(&self) -> &'static str {
self.namespace
}
pub const fn method(&self) -> &'static str {
self.method
}
pub fn qualified(&self) -> String {
format!("{}.{}", self.namespace, self.method)
}
}
impl fmt::Display for MethodRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.namespace, self.method)
}
}
pub trait Transport {
fn call(&self, method: MethodRef, params: Value) -> ItemStream<'_>;
}
impl<T: Transport + ?Sized> Transport for &T {
fn call(&self, method: MethodRef, params: Value) -> ItemStream<'_> {
(**self).call(method, params)
}
}
impl<T: Transport + ?Sized> Transport for Box<T> {
fn call(&self, method: MethodRef, params: Value) -> ItemStream<'_> {
(**self).call(method, params)
}
}
impl<T: Transport + ?Sized> Transport for std::rc::Rc<T> {
fn call(&self, method: MethodRef, params: Value) -> ItemStream<'_> {
(**self).call(method, params)
}
}
impl<T: Transport + ?Sized> Transport for std::sync::Arc<T> {
fn call(&self, method: MethodRef, params: Value) -> ItemStream<'_> {
(**self).call(method, params)
}
}