Skip to main content

plyne/
lib.rs

1pub mod function;
2pub mod pipeline;
3
4pub use function::*;
5pub use pipeline::*;
6
7pub use magic_params;
8pub use paste;
9
10// Pipeline
11pub type Input<T> = tokio::sync::mpsc::UnboundedSender<T>;
12pub type Output<T> = tokio::sync::mpsc::UnboundedReceiver<T>;
13
14#[macro_export]
15macro_rules! define_tasks {
16    (
17        $tasks_name: ident
18        pipelines {
19            $($name: ident: $ty: ty),* $(,)?
20        }
21        vars {
22            $($var: ident: $vty: ty),* $(,)?
23        }
24        tasks {
25            $($task: ident),* $(,)?
26        }
27    ) => {
28        $crate::paste::paste! {
29            // Context
30            $crate::magic_params::define_context!([< $tasks_name Context >] {
31                $( $name: $crate::Pipeline<$ty>,)*
32                $( $var: $vty,)*
33            });
34
35            // Pipelines
36            $(
37                impl [< From $tasks_name Context >]<'_> for $crate::Input<$ty> {
38                    fn from_context(ctx: &[< $tasks_name Context >]) -> Self { ctx.$name.input().unwrap() }
39                }
40
41                impl [< From $tasks_name Context >]<'_> for $crate::Output<$ty> {
42                    fn from_context(ctx: &[< $tasks_name Context >]) -> Self { ctx.$name.output().unwrap() }
43                }
44            )*
45
46            $crate::magic_params::context_as_params!([< $tasks_name Context >], 12);
47
48            // System
49            struct $tasks_name([< $tasks_name Context >]);
50
51            impl $tasks_name {
52                #[must_use]
53                #[allow(dead_code)]
54                pub fn new($($var: $vty),*) -> Self {
55                    Self([< $tasks_name Context >] {
56                        $( $name: $crate::Pipeline::new(false), )*
57                        $( $var, )*
58                    })
59                }
60
61                #[allow(dead_code)]
62                pub async fn execute(self) -> [< $tasks_name Context >] {
63                    futures::join!( $( $task.call(&self.0), )* );
64                    self.0
65                }
66            }
67        }
68    };
69}