#![cfg_attr(
feature = "http",
doc = "- [`http`] *(feature `http`)* — [`HttpTransport`], backed by `reqwest`."
)]
#![cfg_attr(
not(feature = "http"),
doc = "- `http` *(feature `http`, off in this build)* — `HttpTransport`, backed by `reqwest`."
)]
pub mod replay;
pub mod sse;
#[cfg(feature = "http")]
pub mod http;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use crate::{Event, RunAgentInput};
use futures_core::Stream;
use futures_util::StreamExt;
use serde_json::Value;
use crate::client::error::{Error, Result};
pub use replay::ReplayTransport;
pub use sse::{SseDecoder, SseFrame, decode_events, decode_raw_events};
#[cfg(feature = "http")]
pub use http::{HttpTransport, HttpTransportBuilder};
#[cfg(not(target_family = "wasm"))]
pub type EventStream = Pin<Box<dyn Stream<Item = Result<Event>> + Send>>;
#[cfg(target_family = "wasm")]
pub type EventStream = Pin<Box<dyn Stream<Item = Result<Event>>>>;
#[cfg(not(target_family = "wasm"))]
pub type RawEventStream = Pin<Box<dyn Stream<Item = Result<Value>> + Send>>;
#[cfg(target_family = "wasm")]
pub type RawEventStream = Pin<Box<dyn Stream<Item = Result<Value>>>>;
#[cfg(not(target_family = "wasm"))]
pub type TransportFuture = Pin<Box<dyn Future<Output = Result<EventStream>> + Send>>;
#[cfg(target_family = "wasm")]
pub type TransportFuture = Pin<Box<dyn Future<Output = Result<EventStream>>>>;
#[cfg(not(target_family = "wasm"))]
pub type RawTransportFuture = Pin<Box<dyn Future<Output = Result<RawEventStream>> + Send>>;
#[cfg(target_family = "wasm")]
pub type RawTransportFuture = Pin<Box<dyn Future<Output = Result<RawEventStream>>>>;
pub trait Transport {
fn run(&self, input: RunAgentInput) -> TransportFuture;
fn run_raw(&self, input: RunAgentInput) -> RawTransportFuture {
let connecting = self.run(input);
Box::pin(async move {
let events = connecting.await?;
Ok(boxed_raw_stream(events.map(|event| {
event.and_then(|event| serde_json::to_value(event).map_err(Error::from))
})))
})
}
}
impl<T: Transport + ?Sized> Transport for &T {
fn run(&self, input: RunAgentInput) -> TransportFuture {
(**self).run(input)
}
fn run_raw(&self, input: RunAgentInput) -> RawTransportFuture {
(**self).run_raw(input)
}
}
impl<T: Transport + ?Sized> Transport for Box<T> {
fn run(&self, input: RunAgentInput) -> TransportFuture {
(**self).run(input)
}
fn run_raw(&self, input: RunAgentInput) -> RawTransportFuture {
(**self).run_raw(input)
}
}
impl<T: Transport + ?Sized> Transport for Arc<T> {
fn run(&self, input: RunAgentInput) -> TransportFuture {
(**self).run(input)
}
fn run_raw(&self, input: RunAgentInput) -> RawTransportFuture {
(**self).run_raw(input)
}
}
#[cfg(not(target_family = "wasm"))]
pub fn boxed_stream(stream: impl Stream<Item = Result<Event>> + Send + 'static) -> EventStream {
Box::pin(stream)
}
#[cfg(target_family = "wasm")]
pub fn boxed_stream(stream: impl Stream<Item = Result<Event>> + 'static) -> EventStream {
Box::pin(stream)
}
#[cfg(not(target_family = "wasm"))]
pub fn boxed_raw_stream(
stream: impl Stream<Item = Result<Value>> + Send + 'static,
) -> RawEventStream {
Box::pin(stream)
}
#[cfg(target_family = "wasm")]
pub fn boxed_raw_stream(stream: impl Stream<Item = Result<Value>> + 'static) -> RawEventStream {
Box::pin(stream)
}