orchestra_toolkit/session/api/
raw_msg.rs

1/* Copyright 2024-2025 LEDR Technologies Inc.
2* This file is part of the Orchestra library, which helps developer use our Orchestra technology which is based on AvesTerra, owned and developped by Georgetown University, under license agreement with LEDR Technologies Inc.
3*
4* The Orchestra library is a free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
5*
6* The Orchestra library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
7*
8* You should have received a copy of the GNU Lesser General Public License along with the Orchestra library. If not, see <https://www.gnu.org/licenses/>.
9*
10* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
11*/
12
13use std::{
14    future::{Future, IntoFuture},
15    pin::Pin,
16};
17
18use crate::{hgtp::*, CallError, Session, SessionAsync, SessionTrait};
19
20use super::send_recv;
21
22pub struct RawMessage<T: SessionTrait> {
23    pub(crate) session: T,
24    pub(crate) msg: HGTPMessage,
25}
26impl SessionAsync {
27    #[must_use]
28    pub fn raw_message(&self, msg: HGTPMessage) -> RawMessage<Self> {
29        RawMessage {
30            session: self.clone(),
31            msg,
32        }
33    }
34}
35impl Session {
36    #[must_use]
37    pub fn raw_message(&self, msg: HGTPMessage) -> RawMessage<Self> {
38        RawMessage {
39            session: self.clone(),
40            msg,
41        }
42    }
43}
44impl IntoFuture for RawMessage<SessionAsync> {
45    type IntoFuture = Pin<Box<dyn Future<Output = Result<HGTPMessage, CallError>> + Send>>;
46    type Output = <Self::IntoFuture as Future>::Output;
47    fn into_future(self) -> Self::IntoFuture {
48        Box::pin(async move { self.call_async().await })
49    }
50}
51impl RawMessage<Session> {
52    pub fn call(self) -> Result<HGTPMessage, CallError> {
53        self.session.clone().run_async(|_| self.call_async())
54    }
55}
56impl<T> RawMessage<T> where T: SessionTrait {}
57
58impl<T: SessionTrait> RawMessage<T> {
59    async fn call_async(self) -> Result<HGTPMessage, CallError> {
60        let msg = send_recv(self.msg, self.session.get_socket_pool()).await?;
61        Ok(msg)
62    }
63}