pub use kproc_pmacros::{InputStreams, OutputStreams};
use std::future::Future;
use crate::{utils, Result};
pub trait Inputs
{
type Streams: InputStreams<Inputs = Self>;
}
pub trait Outputs
{
type Streams: OutputStreams<Outputs = Self>;
}
pub trait InputStreams
{
type Inputs: Inputs<Streams = Self>;
fn next(&mut self) -> impl Future<Output = Result<Self::Inputs>>;
}
pub trait OutputStreams
{
type Outputs: Outputs<Streams = Self>;
fn is_full(&self) -> bool;
fn next(&mut self, outputs: Self::Outputs) -> impl Future<Output = crate::Result<()>>;
}
pub trait ProcessorStreams
{
type Inputs;
type Outputs;
type InputStreams: InputStreams<Inputs = Self::Inputs>;
type OutputStreams: OutputStreams<Outputs = Self::Outputs>;
}
pub trait ValueProcessor: ProcessorStreams
{
fn process_one(&self, inputs: Self::Inputs)
-> impl Future<Output = Result<Self::Outputs>> + Send;
}
pub trait Processor: ProcessorStreams
where
Self: Sized,
{
fn start(
self,
inputs: Self::InputStreams,
outputs: Self::OutputStreams,
) -> impl Future<Output = ()>;
}
impl<T> Processor for T
where
T: ValueProcessor,
{
#[allow(clippy::manual_async_fn)] fn start(
self,
mut inputs: Self::InputStreams,
mut outputs: Self::OutputStreams,
) -> impl Future<Output = ()>
{
async move {
loop
{
let r = inputs.next().await;
match r
{
Ok(i) =>
{
let output_values = self.process_one(i).await;
match output_values
{
Ok(ov) =>
{
utils::log_error(outputs.next(ov).await);
}
Err(_) =>
{
utils::log_error(output_values);
}
}
}
Err(_) =>
{
return;
}
}
}
}
}
}