data_pipeline_rs/
pipeline_builder.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
use std::marker::PhantomData;

use crate::{
    data_handler::{DataDemuxer, SomeDataHandler},
    node::{Node, NodeRef},
};

pub trait PipelineState {}
pub struct Open;
pub struct Terminated;

impl PipelineState for Open {}
impl PipelineState for Terminated {}

#[derive(Default)]
pub struct PipelineBuilder<S: PipelineState, T> {
    nodes: Vec<NodeRef<T>>,
    _state: PhantomData<S>,
}

impl<T> Default for PipelineBuilder<Open, T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> PipelineBuilder<Open, T> {
    pub fn new() -> Self {
        PipelineBuilder::<Open, T> {
            nodes: Vec::new(),
            _state: PhantomData,
        }
    }
}

impl<S: PipelineState, T> PipelineBuilder<S, T> {
    pub fn build(mut self) -> NodeRef<T> {
        self.nodes.remove(0)
    }
}

impl<T> PipelineBuilder<Open, T> {
    pub fn attach<U: Into<NodeRef<T>>>(mut self, node: U) -> Self {
        let node_ref = node.into();
        if let Some(last) = self.nodes.last() {
            last.set_next(node_ref.clone());
            node_ref.set_prev(last.clone());
        }
        self.nodes.push(node_ref);
        self
    }

    pub fn attach_handler<N: Into<String>, U: Into<SomeDataHandler<T>>>(
        self,
        name: N,
        handler: U,
    ) -> Self {
        let node = Node::new(name, handler.into());
        self.attach(node)
    }

    pub fn demux<U: Into<String>>(
        mut self,
        name: U,
        demuxer: impl DataDemuxer<T> + Send + 'static,
    ) -> PipelineBuilder<Terminated, T> {
        let new_node = NodeRef::new(Node::new(name, SomeDataHandler::Demuxer(Box::new(demuxer))));
        if let Some(last) = self.nodes.last() {
            last.set_next(new_node.clone());
            new_node.set_prev(last.clone());
        }
        self.nodes.push(new_node);
        // Demuxers can't be attached to like normal nodes because they handle their own downstream
        // paths, so although a pipeline will almost always continue after a demuxer, from a
        // builder perspective the demuxer needs to be built with its own sub-pipelines for its
        // downstream paths.
        PipelineBuilder::<Terminated, T> {
            nodes: self.nodes,
            _state: PhantomData::<Terminated>,
        }
    }
}