//! Generating the strings use super::*; use tokio::sync::mpsc::error::SendError; use futures::StreamExt; #[derive(Debug, Default)] pub struct GenBodyError(Option); impl error::Error for GenBodyError{} impl fmt::Display for GenBodyError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if let Some(z) = &self.0 { write!(f, "failed to write read string {:?} to body", z) } else { write!(f, "failed to read string from chain. it might be empty.") } } } pub async fn body(state: State, num: Option, mut output: mpsc::Sender) -> Result<(), GenBodyError> { let mut chain = state.chain_read(); let filter = state.outbound_filter(); match num { Some(num) if num < state.config().max_gen_size => { let mut chain = chain.take(num); while let Some(string) = chain.next().await { output.send(filter.filter_owned(string)).await?; } }, _ => output.send(filter.filter_owned(chain.next().await.ok_or_else(GenBodyError::default)?)).await?, } Ok(()) } impl From> for GenBodyError { #[inline] fn from(from: SendError) -> Self { Self(Some(from.0)) } }