#[allow(unused_imports)]
use anyhow::*;
use runnel::*;
#[macro_export]
macro_rules! pipe_line {
(($next_in:expr,$handles:expr) ; $($rest:tt)*) => {{
$crate::pipe_line!(($next_in,$handles,$crate::_pipe_sz!()) ; $($rest)*)
}};
(($next_in:expr,$handles:expr,$psz:expr) ; $($rest:tt)*) => {{
let next_in = { $next_in };
let mut handles = { $handles };
let next_in = $crate::pipe_line!((0,handles,next_in,$psz) $($rest)*);
(next_in, handles)
}};
(($n:expr,$f:ident,$next_in:ident,$psz:expr) $cmd:ident $cmd_nm:literal $($x:expr)* ; $($rest:tt)*) => {{
let (a_out, a_in) = runnel::medium::pipeio::pipe($psz);
let handle = $crate::_txtpc!(($n, $next_in, a_out) $cmd $cmd_nm $crate::_txtpc_args!($($x)*));
$f.push(handle);
let next_in = pipe_line!(($n+1,$f,a_in,$psz) $($rest)*);
next_in
}};
(($n:expr,$f:ident,$next_in:ident,$psz:expr) $cmd:ident $cmd_nm:literal $($x:expr)* ;) => {{
let (a_out, a_in) = runnel::medium::pipeio::pipe($psz);
let handle = $crate::_txtpc!(($n, $next_in, a_out) $cmd $cmd_nm $crate::_txtpc_args!($($x)*));
$f.push(handle);
let next_in = a_in;
next_in
}};
(($n:expr,$f:ident,$next_in:ident,$psz:expr)) => {{ $next_in }};
}
#[doc(hidden)]
#[macro_export]
macro_rules! _txtpc {
(($n:expr, $pin:ident, $pout:ident) $cmd:ident $cmd_nm:literal $args:expr) => {
std::thread::spawn(move || {
let _n = $n;
let prog_name = $cmd_nm;
let sioe = runnel::RunnelIoeBuilder::new()
.pin($pin)
.pout($pout)
.build();
let args = $args;
if let Err(err) = $cmd::execute(&sioe, prog_name, args) {
let _ = write_error(&sioe, prog_name, err);
}
})
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! _txtpc_args {
() => (
&[]
);
($($x:expr)+) => (
&[$($x),+]
);
}
#[doc(hidden)]
#[macro_export]
macro_rules! _pipe_sz {
() => {
16
};
}
pub fn write_error(sioe: &RunnelIoe, prog_name: &str, err: anyhow::Error) -> std::io::Result<()> {
let mut p_err = sioe.perr().lock();
p_err.write_fmt(format_args!("{}: {}\n", prog_name, err))?;
p_err.flush()?;
Ok(())
}