use std::{
pin::Pin,
task::{Context, Poll},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Block {
pub text: String,
pub short_text: Option<String>,
pub color: Option<String>,
}
impl From<String> for Block {
fn from(text: String) -> Self {
Self {
text,
short_text: None,
color: None,
}
}
}
impl From<&str> for Block {
fn from(text: &str) -> Self {
text.to_owned().into()
}
}
pub struct Stream {
id: usize,
stream: Pin<Box<dyn futures_util::Stream<Item = Option<Block>>>>,
}
impl Stream {
pub fn new<S>(id: usize, stream: S) -> Self
where
S: futures_util::Stream<Item = Option<Block>> + 'static,
{
Self {
id,
stream: Box::pin(stream),
}
}
}
impl futures_util::Stream for Stream {
type Item = (usize, Option<Block>);
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.stream)
.poll_next(cx)
.map(|option| option.map(|block| (self.id, block)))
}
}