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
use std::collections::HashMap;

use dfir_rs::util::deploy::{
    ConnectedDemux, ConnectedDirect, ConnectedSink, ConnectedSource, ConnectedTagged, DeployPorts,
};
use serde::{Deserialize, Serialize};
use stageleft::{q, QuotedWithContext, RuntimeData};

#[derive(Default, Serialize, Deserialize)]
pub struct HydroflowPlusMeta {
    pub clusters: HashMap<usize, Vec<u32>>,
    pub cluster_id: Option<u32>,
    pub subgraph_id: usize,
}

pub fn cluster_members(
    cli: RuntimeData<&DeployPorts<HydroflowPlusMeta>>,
    of_cluster: usize,
) -> impl QuotedWithContext<&Vec<u32>, ()> + Copy {
    q!(cli.meta.clusters.get(&of_cluster).unwrap())
}

pub fn cluster_self_id(
    cli: RuntimeData<&DeployPorts<HydroflowPlusMeta>>,
) -> impl QuotedWithContext<u32, ()> + Copy {
    q!(cli
        .meta
        .cluster_id
        .expect("Tried to read Cluster ID on a non-cluster node"))
}

pub fn deploy_o2o(
    env: RuntimeData<&DeployPorts<HydroflowPlusMeta>>,
    p1_port: &str,
    p2_port: &str,
) -> (syn::Expr, syn::Expr) {
    (
        {
            q!({
                env.port(p1_port)
                    .connect_local_blocking::<ConnectedDirect>()
                    .into_sink()
            })
            .splice_untyped_ctx(&())
        },
        {
            q!({
                env.port(p2_port)
                    .connect_local_blocking::<ConnectedDirect>()
                    .into_source()
            })
            .splice_untyped_ctx(&())
        },
    )
}

pub fn deploy_o2m(
    env: RuntimeData<&DeployPorts<HydroflowPlusMeta>>,
    p1_port: &str,
    c2_port: &str,
) -> (syn::Expr, syn::Expr) {
    (
        {
            q!({
                env.port(p1_port)
                    .connect_local_blocking::<ConnectedDemux<ConnectedDirect>>()
                    .into_sink()
            })
            .splice_untyped_ctx(&())
        },
        {
            q!({
                env.port(c2_port)
                    .connect_local_blocking::<ConnectedDirect>()
                    .into_source()
            })
            .splice_untyped_ctx(&())
        },
    )
}

pub fn deploy_m2o(
    env: RuntimeData<&DeployPorts<HydroflowPlusMeta>>,
    c1_port: &str,
    p2_port: &str,
) -> (syn::Expr, syn::Expr) {
    (
        {
            q!({
                env.port(c1_port)
                    .connect_local_blocking::<ConnectedDirect>()
                    .into_sink()
            })
            .splice_untyped_ctx(&())
        },
        {
            q!({
                env.port(p2_port)
                    .connect_local_blocking::<ConnectedTagged<ConnectedDirect>>()
                    .into_source()
            })
            .splice_untyped_ctx(&())
        },
    )
}

pub fn deploy_m2m(
    env: RuntimeData<&DeployPorts<HydroflowPlusMeta>>,
    c1_port: &str,
    c2_port: &str,
) -> (syn::Expr, syn::Expr) {
    (
        {
            q!({
                env.port(c1_port)
                    .connect_local_blocking::<ConnectedDemux<ConnectedDirect>>()
                    .into_sink()
            })
            .splice_untyped_ctx(&())
        },
        {
            q!({
                env.port(c2_port)
                    .connect_local_blocking::<ConnectedTagged<ConnectedDirect>>()
                    .into_source()
            })
            .splice_untyped_ctx(&())
        },
    )
}

pub fn deploy_e2o(
    env: RuntimeData<&DeployPorts<HydroflowPlusMeta>>,
    _e1_port: &str,
    p2_port: &str,
) -> syn::Expr {
    q!({
        env.port(p2_port)
            .connect_local_blocking::<ConnectedDirect>()
            .into_source()
    })
    .splice_untyped_ctx(&())
}

pub fn deploy_o2e(
    env: RuntimeData<&DeployPorts<HydroflowPlusMeta>>,
    p1_port: &str,
    _e2_port: &str,
) -> syn::Expr {
    q!({
        env.port(p1_port)
            .connect_local_blocking::<ConnectedDirect>()
            .into_sink()
    })
    .splice_untyped_ctx(&())
}