1pub mod core {
4 use super::{
5 prelude::{vec, Box, Cow, Named, Vec},
6 BlockConnections, BlockInstantiation, InputPortName, OutputPortName, System,
7 };
8 use crate::{
9 prelude::{Duration, Range, String, ToString},
10 types::DelayType,
11 };
12 use protoflow_core::{Block, Message};
13
14 pub trait CoreBlocks {
15 fn buffer<T: Message + Into<T> + 'static>(&mut self) -> Buffer<T>;
16
17 fn const_string(&mut self, value: impl ToString) -> Const<String>;
18
19 fn count<T: Message + 'static>(&mut self) -> Count<T>;
20
21 fn delay<T: Message + 'static>(&mut self) -> Delay<T>;
22
23 fn delay_by<T: Message + 'static>(&mut self, delay: DelayType) -> Delay<T>;
24
25 fn delay_by_fixed<T: Message + 'static>(&mut self, delay: Duration) -> Delay<T> {
26 self.delay_by(DelayType::Fixed(delay))
27 }
28
29 fn delay_by_random<T: Message + 'static>(&mut self, delay: Range<Duration>) -> Delay<T> {
30 self.delay_by(DelayType::Random(delay))
31 }
32
33 fn drop<T: Message + 'static>(&mut self) -> Drop<T>;
34
35 fn random<T: Message + 'static>(&mut self) -> Random<T>;
36
37 fn random_seeded<T: Message + 'static>(&mut self, seed: Option<u64>) -> Random<T>;
38 }
39
40 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
41 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
42 pub enum CoreBlockTag {
43 Buffer,
44 Const,
45 Count,
46 Delay,
47 Drop,
48 Random,
49 }
50
51 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
52 #[derive(Clone, Debug)]
53 pub enum CoreBlockConfig {
54 Buffer {
55 input: InputPortName,
56 },
57
58 Const {
59 output: OutputPortName,
60 value: String,
61 },
62
63 Count {
64 input: InputPortName,
65 output: Option<OutputPortName>,
66 count: OutputPortName,
67 },
68
69 Delay {
70 input: InputPortName,
71 output: OutputPortName,
72 delay: Option<DelayType>,
73 },
74
75 Drop {
76 input: InputPortName,
77 },
78
79 Random {
80 output: OutputPortName,
81 seed: Option<u64>,
82 },
83 }
84
85 impl Named for CoreBlockConfig {
86 fn name(&self) -> Cow<str> {
87 use CoreBlockConfig::*;
88 Cow::Borrowed(match self {
89 Buffer { .. } => "Buffer",
90 Const { .. } => "Const",
91 Count { .. } => "Count",
92 Delay { .. } => "Delay",
93 Drop { .. } => "Drop",
94 Random { .. } => "Random",
95 })
96 }
97 }
98
99 impl BlockConnections for CoreBlockConfig {
100 fn output_connections(&self) -> Vec<(&'static str, Option<OutputPortName>)> {
101 use CoreBlockConfig::*;
102 match self {
103 Buffer { .. } => vec![],
104 Const { output, .. } => vec![("output", Some(output.clone()))],
105 Count { output, count, .. } => {
106 vec![("output", output.clone()), ("count", Some(count.clone()))]
107 }
108 Delay { output, .. } => vec![("output", Some(output.clone()))],
109 Drop { .. } => vec![],
110 Random { output, .. } => vec![("output", Some(output.clone()))],
111 }
112 }
113 }
114
115 impl BlockInstantiation for CoreBlockConfig {
116 fn instantiate(&self, system: &mut System) -> Box<dyn Block> {
117 use super::SystemBuilding;
118 use CoreBlockConfig::*;
119 match self {
120 Buffer { .. } => Box::new(super::Buffer::new(system.input_any())), Const { value, .. } => Box::new(super::Const::with_system(system, value.clone())),
122 Count { .. } => Box::new(super::Count::new(
123 system.input_any(),
124 system.output(),
125 system.output(),
126 )), Delay { delay, .. } => {
128 Box::new(super::Delay::with_params(
129 system.input_any(),
130 system.output(),
131 delay.clone(),
132 ))
133 }
135 Drop { .. } => Box::new(super::Drop::new(system.input_any())), Random { seed, .. } => {
137 Box::new(super::Random::with_params(system.output::<u64>(), *seed))
138 }
140 }
141 }
142 }
143
144 mod buffer;
145 pub use buffer::*;
146
147 mod r#const;
148 pub use r#const::*;
149
150 mod count;
151 pub use count::*;
152
153 mod delay;
154 pub use delay::*;
155
156 mod drop;
157 pub use drop::*;
158
159 mod random;
160 pub use random::*;
161}
162
163pub use core::*;