pub mod reconnect;
use serde::{Deserialize, Serialize};
use std::future::Future;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum StreamEvent {
TextChunk(String),
Done,
}
pub trait Stream {
type Item;
fn next(&mut self) -> impl Future<Output = Option<Self::Item>> + Send;
}
#[cfg(test)]
mod tests {
use super::*;
struct MockStream {
count: u32,
}
impl Stream for MockStream {
type Item = StreamEvent;
fn next(&mut self) -> impl Future<Output = Option<Self::Item>> + Send {
let res = match self.count {
0..=2 => {
self.count += 1;
Some(StreamEvent::TextChunk(format!("chunk {}", self.count)))
}
3 => {
self.count += 1;
Some(StreamEvent::Done)
}
_ => None,
};
async move { res }
}
}
#[tokio::test]
async fn test_mock_stream() {
let mut stream = MockStream { count: 0 };
assert_eq!(
stream.next().await,
Some(StreamEvent::TextChunk("chunk 1".to_string()))
);
assert_eq!(
stream.next().await,
Some(StreamEvent::TextChunk("chunk 2".to_string()))
);
assert_eq!(
stream.next().await,
Some(StreamEvent::TextChunk("chunk 3".to_string()))
);
assert_eq!(stream.next().await, Some(StreamEvent::Done));
assert_eq!(stream.next().await, None);
}
}