iridis_layout/
flows.rs

1//! This module defines the 'flows' part of the `dataflow` application.
2
3use std::collections::HashSet;
4
5use crate::prelude::*;
6
7/// Represents the flows of the application.
8#[derive(Debug, Clone)]
9pub struct FlowLayout {
10    pub connections: HashSet<(Uuid, Uuid)>, // Send -> Receive
11}
12
13impl FlowLayout {
14    fn connect_output_input(&mut self, output: impl AsRef<Uuid>, input: impl AsRef<Uuid>) {
15        self.connections.insert((*output.as_ref(), *input.as_ref()));
16    }
17
18    fn connect_queryable_query(&mut self, queryable: impl AsRef<Uuid>, query: impl AsRef<Uuid>) {
19        self.connections
20            .insert((*queryable.as_ref(), *query.as_ref()));
21
22        self.connections
23            .insert((*query.as_ref(), *queryable.as_ref()));
24    }
25
26    /// Connects two primitives in the graph. The order does not matter.
27    pub fn connect(&mut self, a: impl Into<PrimitiveID>, b: impl Into<PrimitiveID>) -> Result<()> {
28        let (a, b) = (a.into(), b.into());
29
30        match (a, b) {
31            (PrimitiveID::Input(input), PrimitiveID::Output(output)) => {
32                self.connect_output_input(output.uuid, input.uuid);
33                Ok(())
34            }
35            (PrimitiveID::Query(query), PrimitiveID::Queryable(queryable)) => {
36                self.connect_queryable_query(queryable.uuid, query.uuid);
37                Ok(())
38            }
39            (PrimitiveID::Output(output), PrimitiveID::Input(input)) => {
40                self.connect_output_input(output.uuid, input.uuid);
41                Ok(())
42            }
43            (PrimitiveID::Queryable(queryable), PrimitiveID::Query(query)) => {
44                self.connect_queryable_query(queryable.uuid, query.uuid);
45                Ok(())
46            }
47            _ => Err(eyre::eyre!(
48                "Invalid connection! types `a` and `b` must verify: a != b and (a, b) in {{Input, Output}} or {{Query, Queryable}}"
49            )),
50        }
51    }
52}