protoflow_blocks/blocks/
core.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// This is free and unencumbered software released into the public domain.

pub mod core {
    use super::{
        prelude::{vec, Box, Cow, Named, Vec},
        BlockConnections, BlockInstantiation, InputPortName, OutputPortName, System,
    };
    use crate::{
        prelude::{Duration, Range, String, ToString},
        types::DelayType,
    };
    use protoflow_core::{Block, Message};

    pub trait CoreBlocks {
        fn buffer<T: Message + Into<T> + 'static>(&mut self) -> Buffer<T>;

        fn const_string(&mut self, value: impl ToString) -> Const<String>;

        fn count<T: Message + 'static>(&mut self) -> Count<T>;

        fn delay<T: Message + 'static>(&mut self) -> Delay<T>;

        fn delay_by<T: Message + 'static>(&mut self, delay: DelayType) -> Delay<T>;

        fn delay_by_fixed<T: Message + 'static>(&mut self, delay: Duration) -> Delay<T> {
            self.delay_by(DelayType::Fixed(delay))
        }

        fn delay_by_random<T: Message + 'static>(&mut self, delay: Range<Duration>) -> Delay<T> {
            self.delay_by(DelayType::Random(delay))
        }

        fn drop<T: Message + 'static>(&mut self) -> Drop<T>;

        fn random<T: Message + 'static>(&mut self) -> Random<T>;

        fn random_seeded<T: Message + 'static>(&mut self, seed: Option<u64>) -> Random<T>;
    }

    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
    pub enum CoreBlockTag {
        Buffer,
        Const,
        Count,
        Delay,
        Drop,
        Random,
    }

    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[derive(Clone, Debug)]
    pub enum CoreBlockConfig {
        Buffer {
            input: InputPortName,
        },

        Const {
            output: OutputPortName,
            value: String,
        },

        Count {
            input: InputPortName,
            output: Option<OutputPortName>,
            count: OutputPortName,
        },

        Delay {
            input: InputPortName,
            output: OutputPortName,
            delay: Option<DelayType>,
        },

        Drop {
            input: InputPortName,
        },

        Random {
            output: OutputPortName,
            seed: Option<u64>,
        },
    }

    impl Named for CoreBlockConfig {
        fn name(&self) -> Cow<str> {
            use CoreBlockConfig::*;
            Cow::Borrowed(match self {
                Buffer { .. } => "Buffer",
                Const { .. } => "Const",
                Count { .. } => "Count",
                Delay { .. } => "Delay",
                Drop { .. } => "Drop",
                Random { .. } => "Random",
            })
        }
    }

    impl BlockConnections for CoreBlockConfig {
        fn output_connections(&self) -> Vec<(&'static str, Option<OutputPortName>)> {
            use CoreBlockConfig::*;
            match self {
                Buffer { .. } => vec![],
                Const { output, .. } => vec![("output", Some(output.clone()))],
                Count { output, count, .. } => {
                    vec![("output", output.clone()), ("count", Some(count.clone()))]
                }
                Delay { output, .. } => vec![("output", Some(output.clone()))],
                Drop { .. } => vec![],
                Random { output, .. } => vec![("output", Some(output.clone()))],
            }
        }
    }

    impl BlockInstantiation for CoreBlockConfig {
        fn instantiate(&self, system: &mut System) -> Box<dyn Block> {
            use super::SystemBuilding;
            use CoreBlockConfig::*;
            match self {
                Buffer { .. } => Box::new(super::Buffer::new(system.input_any())), // TODO: Buffer::with_system(system)
                Const { value, .. } => Box::new(super::Const::with_system(system, value.clone())),
                Count { .. } => Box::new(super::Count::new(
                    system.input_any(),
                    system.output(),
                    system.output(),
                )), // TODO: Count::with_system(system)
                Delay { delay, .. } => {
                    Box::new(super::Delay::with_params(
                        system.input_any(),
                        system.output(),
                        delay.clone(),
                    ))
                    // TODO: Delay::with_system(system, Some(delay.clone())))
                }
                Drop { .. } => Box::new(super::Drop::new(system.input_any())), // TODO: Drop::with_system(system)
                Random { seed, .. } => {
                    Box::new(super::Random::with_params(system.output::<u64>(), *seed))
                    // TODO: Random::with_system(system, *seed))
                }
            }
        }
    }

    mod buffer;
    pub use buffer::*;

    mod r#const;
    pub use r#const::*;

    mod count;
    pub use count::*;

    mod delay;
    pub use delay::*;

    mod drop;
    pub use drop::*;

    mod random;
    pub use random::*;
}

pub use core::*;