use eventsource_stream::Eventsource;
use futures_util::StreamExt;
use crate::error::{Error, Result, StreamError};
pub(crate) fn into_typed_stream<T>(
response: reqwest::Response,
) -> impl futures_util::Stream<Item = Result<T>> + Send + 'static
where
T: serde::de::DeserializeOwned + Send + 'static,
{
response
.bytes_stream()
.eventsource()
.map(|item| match item {
Ok(event) => serde_json::from_str::<T>(&event.data)
.map_err(|e| Error::Stream(StreamError::Parse(e.to_string()))),
Err(e) => Err(Error::Stream(StreamError::Connection(e.to_string()))),
})
}