use std::{
future::{Future, IntoFuture},
pin::Pin,
};
use crate::{hgtp::*, CallError, Session, SessionAsync, SessionTrait};
use super::send_recv;
pub struct RawMessage<T: SessionTrait> {
pub(crate) session: T,
pub(crate) msg: HGTPMessage,
}
impl SessionAsync {
#[must_use]
pub fn raw_message(&self, msg: HGTPMessage) -> RawMessage<Self> {
RawMessage {
session: self.clone(),
msg,
}
}
}
impl Session {
#[must_use]
pub fn raw_message(&self, msg: HGTPMessage) -> RawMessage<Self> {
RawMessage {
session: self.clone(),
msg,
}
}
}
impl IntoFuture for RawMessage<SessionAsync> {
type IntoFuture = Pin<Box<dyn Future<Output = Result<HGTPMessage, CallError>> + Send>>;
type Output = <Self::IntoFuture as Future>::Output;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.call_async().await })
}
}
impl RawMessage<Session> {
pub fn call(self) -> Result<HGTPMessage, CallError> {
self.session.clone().run_async(|_| self.call_async())
}
}
impl<T> RawMessage<T> where T: SessionTrait {}
impl<T: SessionTrait> RawMessage<T> {
async fn call_async(self) -> Result<HGTPMessage, CallError> {
let msg = send_recv(self.msg, self.session.get_socket_pool()).await?;
Ok(msg)
}
}