357 lines
16 KiB
Rust
357 lines
16 KiB
Rust
// ── Macros ────────────────────────────────────────────────────────────────────
|
|||
|
|
|
||
|
|
/// Creates a `SourceFn` from an iterator of plain values.
|
||
|
|
#[macro_export]
|
||
|
|
macro_rules! make_source {
|
||
|
|
($enum:ident, $iterator:expr, $output:ident) => {{
|
||
|
|
let mut iter = $iterator.into_iter();
|
||
|
|
Box::new(
|
||
|
|
move || -> ::std::result::Result<$enum, $crate::PipelineError> {
|
||
|
|
match iter.next() {
|
||
|
|
Some(x) => Ok($enum::$output(x)),
|
||
|
|
None => Err($crate::PipelineError::EndOfStream),
|
||
|
|
}
|
||
|
|
},
|
||
|
|
) as Box<dyn FnMut() -> ::std::result::Result<$enum, $crate::PipelineError> + Send>
|
||
|
|
}};
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Creates a `SourceFn` from an iterator of `Result<T, E>`.
|
||
|
|
#[macro_export]
|
||
|
|
macro_rules! make_source_fallible {
|
||
|
|
($enum:ident, $iterator:expr, $output:ident) => {{
|
||
|
|
let mut iter = $iterator.into_iter();
|
||
|
|
Box::new(
|
||
|
|
move || -> ::std::result::Result<$enum, $crate::PipelineError> {
|
||
|
|
match iter.next() {
|
||
|
|
Some(Ok(x)) => Ok($enum::$output(x)),
|
||
|
|
Some(Err(e)) => Err($crate::PipelineError::StepError(Box::new(e))),
|
||
|
|
None => Err($crate::PipelineError::EndOfStream),
|
||
|
|
}
|
||
|
|
},
|
||
|
|
) as Box<dyn FnMut() -> ::std::result::Result<$enum, $crate::PipelineError> + Send>
|
||
|
|
}};
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Creates a `Stage::Transform` from a pure (non-fallible) function `Fn(T) -> U`.
|
||
|
|
#[macro_export]
|
||
|
|
macro_rules! make_transform {
|
||
|
|
($enum:ident, $func:tt, $input:ident, $output:ident) => {{
|
||
|
|
let __f = $func;
|
||
|
|
$crate::Stage::Transform(::std::sync::Arc::from(Box::new(
|
||
|
|
move |data: $enum| -> ::std::result::Result<$enum, $crate::PipelineError> {
|
||
|
|
match data {
|
||
|
|
$enum::$input(x) => Ok($enum::$output(__f(x))),
|
||
|
|
_ => Err($crate::PipelineError::TypeMismatch),
|
||
|
|
}
|
||
|
|
},
|
||
|
|
)
|
||
|
|
as Box<
|
||
|
|
dyn Fn($enum) -> ::std::result::Result<$enum, $crate::PipelineError> + Send + Sync,
|
||
|
|
>))
|
||
|
|
}};
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Creates a `Stage::Transform` from a fallible function `Fn(T) -> Result<U, E>`.
|
||
|
|
#[macro_export]
|
||
|
|
macro_rules! make_transform_fallible {
|
||
|
|
($enum:ident, $func:tt, $input:ident, $output:ident) => {{
|
||
|
|
let __f = $func;
|
||
|
|
$crate::Stage::Transform(::std::sync::Arc::from(Box::new(
|
||
|
|
move |data: $enum| -> ::std::result::Result<$enum, $crate::PipelineError> {
|
||
|
|
match data {
|
||
|
|
$enum::$input(inner) => {
|
||
|
|
let result = __f(inner)
|
||
|
|
.map_err(|e| $crate::PipelineError::StepError(Box::new(e)))?;
|
||
|
|
Ok($enum::$output(result))
|
||
|
|
}
|
||
|
|
_ => Err($crate::PipelineError::TypeMismatch),
|
||
|
|
}
|
||
|
|
},
|
||
|
|
)
|
||
|
|
as Box<
|
||
|
|
dyn Fn($enum) -> ::std::result::Result<$enum, $crate::PipelineError> + Send + Sync,
|
||
|
|
>))
|
||
|
|
}};
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Creates a `Stage::Flat` from a function `Fn(T) -> impl IntoIterator<Item = U>`.
|
||
|
|
///
|
||
|
|
/// Each item produced by the iterator is pushed individually to
|
||
|
|
/// the output channel, so that the scheduler can dispatch the items in parallel
|
||
|
|
/// as soon as a worker is available.
|
||
|
|
#[macro_export]
|
||
|
|
macro_rules! make_flat_transform {
|
||
|
|
($enum:ident, $func:tt, $input:ident, $output:ident) => {{
|
||
|
|
let __f = $func;
|
||
|
|
$crate::Stage::Flat(::std::sync::Arc::new(
|
||
|
|
move |data: $enum,
|
||
|
|
push: &$crate::PipelineSender<
|
||
|
|
::std::result::Result<$enum, $crate::PipelineError>,
|
||
|
|
>,
|
||
|
|
delta: &$crate::PipelineSender<isize>| {
|
||
|
|
match data {
|
||
|
|
$enum::$input(inner) => {
|
||
|
|
let mut count: isize = 0;
|
||
|
|
for item in __f(inner) {
|
||
|
|
push.send(Ok($enum::$output(item))).ok();
|
||
|
|
count += 1;
|
||
|
|
}
|
||
|
|
delta.send(count - 1).ok();
|
||
|
|
}
|
||
|
|
_ => {
|
||
|
|
push.send(Err($crate::PipelineError::TypeMismatch)).ok();
|
||
|
|
delta.send(0).ok();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
) as $crate::SharedFlatFn<$enum>)
|
||
|
|
}};
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Creates a `Stage::Flat` from a fallible function
|
||
|
|
/// `Fn(T) -> Result<impl IntoIterator<Item = U>, E>`.
|
||
|
|
///
|
||
|
|
/// If the function returns `Err`, an error is pushed to the channel and no
|
||
|
|
/// regular item is produced.
|
||
|
|
#[macro_export]
|
||
|
|
macro_rules! make_flat_transform_fallible {
|
||
|
|
($enum:ident, $func:tt, $input:ident, $output:ident) => {{
|
||
|
|
let __f = $func;
|
||
|
|
$crate::Stage::Flat(::std::sync::Arc::new(
|
||
|
|
move |data: $enum,
|
||
|
|
push: &$crate::PipelineSender<
|
||
|
|
::std::result::Result<$enum, $crate::PipelineError>,
|
||
|
|
>,
|
||
|
|
delta: &$crate::PipelineSender<isize>| {
|
||
|
|
match data {
|
||
|
|
$enum::$input(inner) => match __f(inner) {
|
||
|
|
Ok(iter) => {
|
||
|
|
let mut count: isize = 0;
|
||
|
|
for item in iter {
|
||
|
|
push.send(Ok($enum::$output(item))).ok();
|
||
|
|
count += 1;
|
||
|
|
}
|
||
|
|
delta.send(count - 1).ok();
|
||
|
|
}
|
||
|
|
Err(e) => {
|
||
|
|
push.send(Err($crate::PipelineError::StepError(Box::new(e))))
|
||
|
|
.ok();
|
||
|
|
delta.send(0).ok();
|
||
|
|
}
|
||
|
|
},
|
||
|
|
_ => {
|
||
|
|
push.send(Err($crate::PipelineError::TypeMismatch)).ok();
|
||
|
|
delta.send(0).ok();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
) as $crate::SharedFlatFn<$enum>)
|
||
|
|
}};
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Creates a `SinkFn` from a function that consumes a concrete value and returns `()`.
|
||
|
|
#[macro_export]
|
||
|
|
macro_rules! make_sink {
|
||
|
|
($enum:ident, $func:tt, $input:ident) => {{
|
||
|
|
let __f = $func;
|
||
|
|
Box::new(
|
||
|
|
move |data: $enum| -> ::std::result::Result<(), $crate::PipelineError> {
|
||
|
|
match data {
|
||
|
|
$enum::$input(x) => {
|
||
|
|
__f(x);
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
_ => Err($crate::PipelineError::TypeMismatch),
|
||
|
|
}
|
||
|
|
},
|
||
|
|
) as Box<dyn Fn($enum) -> ::std::result::Result<(), $crate::PipelineError> + Send>
|
||
|
|
}};
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Creates a `SinkFn` from a fallible function that returns `Result<(), E>`.
|
||
|
|
#[macro_export]
|
||
|
|
macro_rules! make_sink_fallible {
|
||
|
|
($enum:ident, $func:tt, $input:ident) => {{
|
||
|
|
let __f = $func;
|
||
|
|
Box::new(
|
||
|
|
move |data: $enum| -> ::std::result::Result<(), $crate::PipelineError> {
|
||
|
|
match data {
|
||
|
|
$enum::$input(inner) => {
|
||
|
|
__f(inner).map_err(|e| $crate::PipelineError::StepError(Box::new(e)))
|
||
|
|
}
|
||
|
|
_ => Err($crate::PipelineError::TypeMismatch),
|
||
|
|
}
|
||
|
|
},
|
||
|
|
) as Box<dyn Fn($enum) -> ::std::result::Result<(), $crate::PipelineError> + Send>
|
||
|
|
}};
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Builds a `Pipeline` from a source, a list of stages and a sink.
|
||
|
|
///
|
||
|
|
/// Syntax:
|
||
|
|
/// ```ignore
|
||
|
|
/// make_pipeline! {
|
||
|
|
/// MyData,
|
||
|
|
/// source my_iter => Variant, // infallible source
|
||
|
|
/// source? my_iter => Variant, // fallible source (Result<T, E>)
|
||
|
|
/// | func: In => Out, // 1→1 transform, infallible
|
||
|
|
/// |? func: In => Out, // 1→1 transform, fallible
|
||
|
|
/// || func: In => Out, // 1→N flat transform, infallible
|
||
|
|
/// ||? func: In => Out, // 1→N flat transform, fallible
|
||
|
|
/// sink my_func @ Variant, // infallible sink
|
||
|
|
/// sink? my_func @ Variant, // fallible sink
|
||
|
|
/// }
|
||
|
|
/// ```
|
||
|
|
#[macro_export]
|
||
|
|
macro_rules! make_pipeline {
|
||
|
|
// ── Entry points ─────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
($enum:ident, source $src:expr => $src_out:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipeline!(@build $enum,
|
||
|
|
{ $crate::make_source!($enum, $src, $src_out) },
|
||
|
|
[],
|
||
|
|
$($rest)*)
|
||
|
|
};
|
||
|
|
($enum:ident, source? $src:expr => $src_out:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipeline!(@build $enum,
|
||
|
|
{ $crate::make_source_fallible!($enum, $src, $src_out) },
|
||
|
|
[],
|
||
|
|
$($rest)*)
|
||
|
|
};
|
||
|
|
|
||
|
|
// ── Stage accumulation ───────────────────────────────────────────────
|
||
|
|
|
||
|
|
// 1→1 transform, infallible
|
||
|
|
(@build $enum:ident, $source:tt, [$($acc:tt)*],
|
||
|
|
| $tf:tt : $t_in:ident => $t_out:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipeline!(@build $enum, $source,
|
||
|
|
[$($acc)* $crate::make_transform!($enum, $tf, $t_in, $t_out),],
|
||
|
|
$($rest)*)
|
||
|
|
};
|
||
|
|
|
||
|
|
// 1→1 transform, fallible
|
||
|
|
(@build $enum:ident, $source:tt, [$($acc:tt)*],
|
||
|
|
|? $tf:tt : $t_in:ident => $t_out:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipeline!(@build $enum, $source,
|
||
|
|
[$($acc)* $crate::make_transform_fallible!($enum, $tf, $t_in, $t_out),],
|
||
|
|
$($rest)*)
|
||
|
|
};
|
||
|
|
|
||
|
|
// 1→N flat transform, infallible
|
||
|
|
(@build $enum:ident, $source:tt, [$($acc:tt)*],
|
||
|
|
|| $tf:tt : $t_in:ident => $t_out:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipeline!(@build $enum, $source,
|
||
|
|
[$($acc)* $crate::make_flat_transform!($enum, $tf, $t_in, $t_out),],
|
||
|
|
$($rest)*)
|
||
|
|
};
|
||
|
|
|
||
|
|
// 1→N flat transform, fallible
|
||
|
|
(@build $enum:ident, $source:tt, [$($acc:tt)*],
|
||
|
|
||? $tf:tt : $t_in:ident => $t_out:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipeline!(@build $enum, $source,
|
||
|
|
[$($acc)* $crate::make_flat_transform_fallible!($enum, $tf, $t_in, $t_out),],
|
||
|
|
$($rest)*)
|
||
|
|
};
|
||
|
|
|
||
|
|
// ── Termination: sink ────────────────────────────────────────────────
|
||
|
|
|
||
|
|
(@build $enum:ident, $source:tt, [$($acc:tt)*],
|
||
|
|
sink $sink_fn:tt @ $sink_in:ident $(,)?) => {
|
||
|
|
$crate::Pipeline::new(
|
||
|
|
$source,
|
||
|
|
vec![$($acc)*],
|
||
|
|
$crate::make_sink!($enum, $sink_fn, $sink_in),
|
||
|
|
)
|
||
|
|
};
|
||
|
|
(@build $enum:ident, $source:tt, [$($acc:tt)*],
|
||
|
|
sink? $sink_fn:tt @ $sink_in:ident $(,)?) => {
|
||
|
|
$crate::Pipeline::new(
|
||
|
|
$source,
|
||
|
|
vec![$($acc)*],
|
||
|
|
$crate::make_sink_fallible!($enum, $sink_fn, $sink_in),
|
||
|
|
)
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Builds a typed `Pipe<D, In, Out>` — sourceless and sinkless.
|
||
|
|
///
|
||
|
|
/// Syntax:
|
||
|
|
/// ```ignore
|
||
|
|
/// make_pipe! {
|
||
|
|
/// MyData : InType => OutType,
|
||
|
|
/// | func : InVariant => OutVariant, // transform 1→1
|
||
|
|
/// |? func : InVariant => OutVariant, // transform 1→1 fallible
|
||
|
|
/// || func : InVariant => OutVariant, // flat transform 1→N
|
||
|
|
/// ||? func : InVariant => OutVariant, // flat transform 1→N fallible
|
||
|
|
/// }
|
||
|
|
/// ```
|
||
|
|
#[macro_export]
|
||
|
|
macro_rules! make_pipe {
|
||
|
|
// ── Entry: first stage | ─────────────────────────────────────────────
|
||
|
|
($enum:ident : $in_ty:ty => $out_ty:ty,
|
||
|
|
| $tf:tt : $fi:ident => $fo:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipe!(@build $enum : $in_ty => $out_ty, $fi,
|
||
|
|
[$crate::make_transform!($enum, $tf, $fi, $fo),], $fo, $($rest)*)
|
||
|
|
};
|
||
|
|
// ── Entry: first stage |? ────────────────────────────────────────────
|
||
|
|
($enum:ident : $in_ty:ty => $out_ty:ty,
|
||
|
|
|? $tf:tt : $fi:ident => $fo:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipe!(@build $enum : $in_ty => $out_ty, $fi,
|
||
|
|
[$crate::make_transform_fallible!($enum, $tf, $fi, $fo),], $fo, $($rest)*)
|
||
|
|
};
|
||
|
|
// ── Entry: first stage || ────────────────────────────────────────────
|
||
|
|
($enum:ident : $in_ty:ty => $out_ty:ty,
|
||
|
|
|| $tf:tt : $fi:ident => $fo:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipe!(@build $enum : $in_ty => $out_ty, $fi,
|
||
|
|
[$crate::make_flat_transform!($enum, $tf, $fi, $fo),], $fo, $($rest)*)
|
||
|
|
};
|
||
|
|
// ── Entry: first stage ||? ───────────────────────────────────────────
|
||
|
|
($enum:ident : $in_ty:ty => $out_ty:ty,
|
||
|
|
||? $tf:tt : $fi:ident => $fo:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipe!(@build $enum : $in_ty => $out_ty, $fi,
|
||
|
|
[$crate::make_flat_transform_fallible!($enum, $tf, $fi, $fo),], $fo, $($rest)*)
|
||
|
|
};
|
||
|
|
|
||
|
|
// ── Accumulation: | ──────────────────────────────────────────────────
|
||
|
|
(@build $enum:ident : $in_ty:ty => $out_ty:ty, $fi:ident,
|
||
|
|
[$($acc:tt)*], $lo:ident,
|
||
|
|
| $tf:tt : $ti:ident => $to:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipe!(@build $enum : $in_ty => $out_ty, $fi,
|
||
|
|
[$($acc)* $crate::make_transform!($enum, $tf, $ti, $to),], $to, $($rest)*)
|
||
|
|
};
|
||
|
|
// ── Accumulation: |? ─────────────────────────────────────────────────
|
||
|
|
(@build $enum:ident : $in_ty:ty => $out_ty:ty, $fi:ident,
|
||
|
|
[$($acc:tt)*], $lo:ident,
|
||
|
|
|? $tf:tt : $ti:ident => $to:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipe!(@build $enum : $in_ty => $out_ty, $fi,
|
||
|
|
[$($acc)* $crate::make_transform_fallible!($enum, $tf, $ti, $to),], $to, $($rest)*)
|
||
|
|
};
|
||
|
|
// ── Accumulation: || ─────────────────────────────────────────────────
|
||
|
|
(@build $enum:ident : $in_ty:ty => $out_ty:ty, $fi:ident,
|
||
|
|
[$($acc:tt)*], $lo:ident,
|
||
|
|
|| $tf:tt : $ti:ident => $to:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipe!(@build $enum : $in_ty => $out_ty, $fi,
|
||
|
|
[$($acc)* $crate::make_flat_transform!($enum, $tf, $ti, $to),], $to, $($rest)*)
|
||
|
|
};
|
||
|
|
// ── Accumulation: ||? ────────────────────────────────────────────────
|
||
|
|
(@build $enum:ident : $in_ty:ty => $out_ty:ty, $fi:ident,
|
||
|
|
[$($acc:tt)*], $lo:ident,
|
||
|
|
||? $tf:tt : $ti:ident => $to:ident, $($rest:tt)*) => {
|
||
|
|
$crate::make_pipe!(@build $enum : $in_ty => $out_ty, $fi,
|
||
|
|
[$($acc)* $crate::make_flat_transform_fallible!($enum, $tf, $ti, $to),], $to, $($rest)*)
|
||
|
|
};
|
||
|
|
|
||
|
|
// ── Termination ───────────────────────────────────────────────────────
|
||
|
|
(@build $enum:ident : $in_ty:ty => $out_ty:ty, $fi:ident,
|
||
|
|
[$($acc:tt)*], $lo:ident $(,)?) => {
|
||
|
|
$crate::Pipe::new(
|
||
|
|
vec![$($acc)*],
|
||
|
|
::std::sync::Arc::new(|x: $in_ty| $enum::$fi(x)),
|
||
|
|
::std::sync::Arc::new(|d: $enum| -> $out_ty {
|
||
|
|
if let $enum::$lo(x) = d { x }
|
||
|
|
else { ::std::unreachable!("unexpected pipeline data variant in make_pipe!") }
|
||
|
|
}),
|
||
|
|
)
|
||
|
|
};
|
||
|
|
}
|