#![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::enforce::{self, CheckedStream};
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<TransportStream>> + Send>>;
#[cfg(target_family = "wasm")]
pub type TransportFuture = Pin<Box<dyn Future<Output = Result<TransportStream>>>>;
pub struct TransportStream(Source);
enum Source {
Json(RawEventStream),
Typed(EventStream),
}
impl std::fmt::Debug for TransportStream {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self.0 {
Source::Json(_) => "TransportStream::json(..)",
Source::Typed(_) => "TransportStream::typed(..)",
})
}
}
impl TransportStream {
#[cfg(not(target_family = "wasm"))]
pub fn json(raw: impl Stream<Item = Result<Value>> + Send + 'static) -> Self {
Self(Source::Json(Box::pin(raw)))
}
#[cfg(target_family = "wasm")]
pub fn json(raw: impl Stream<Item = Result<Value>> + 'static) -> Self {
Self(Source::Json(Box::pin(raw)))
}
#[cfg_attr(feature = "server", doc = "```")]
#[cfg_attr(not(feature = "server"), doc = "```ignore")]
#[cfg(not(target_family = "wasm"))]
pub fn typed(events: impl Stream<Item = Result<Event>> + Send + 'static) -> Self {
Self(Source::Typed(Box::pin(events)))
}
#[cfg(target_family = "wasm")]
pub fn typed(events: impl Stream<Item = Result<Event>> + 'static) -> Self {
Self(Source::Typed(Box::pin(events)))
}
#[must_use]
pub fn enforced(self) -> Self {
Self(Source::Json(self.into_json()))
}
pub fn into_json(self) -> RawEventStream {
match self.0 {
Source::Json(raw) => raw,
Source::Typed(events) => Box::pin(events.map(|event| {
event.and_then(|event| {
serde_json::to_value(event).map_err(|error| Error::Protocol(error.to_string()))
})
})),
}
}
pub fn into_events<F>(self, on_warning: F) -> impl Stream<Item = Result<Event>>
where
F: FnMut(String),
{
enforce::unchecked(self.into_checked(), on_warning)
}
pub(crate) fn into_checked(self) -> CheckedStream {
match self.0 {
Source::Json(raw) => Box::pin(enforce::checked(raw)),
Source::Typed(events) => Box::pin(enforce::typed(events)),
}
}
}
pub trait Transport {
fn run(&self, input: RunAgentInput) -> TransportFuture;
}
impl<T: Transport + ?Sized> Transport for &T {
fn run(&self, input: RunAgentInput) -> TransportFuture {
(**self).run(input)
}
}
impl<T: Transport + ?Sized> Transport for Box<T> {
fn run(&self, input: RunAgentInput) -> TransportFuture {
(**self).run(input)
}
}
impl<T: Transport + ?Sized> Transport for Arc<T> {
fn run(&self, input: RunAgentInput) -> TransportFuture {
(**self).run(input)
}
}
pub fn enforce_events<S, F>(raw: S, on_warning: F) -> impl Stream<Item = Result<Event>>
where
S: Stream<Item = Result<Value>>,
F: FnMut(String),
{
enforce::events(raw, on_warning)
}