iridis_layout/
primitives.rs

1//! This module defines all the primitive IDs used in the `dataflow` application.
2
3use crate::prelude::*;
4
5/// Represents a unique identifier for an Input in the graph.
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub struct InputID {
8    pub label: String,
9    pub uuid: Uuid,
10}
11
12/// Represents a unique identifier for an Output in the graph.
13#[derive(Debug, Clone, PartialEq, Eq, Hash)]
14pub struct OutputID {
15    pub label: String,
16    pub uuid: Uuid,
17}
18
19/// Represents a unique identifier for a Query in the graph.
20#[derive(Debug, Clone, PartialEq, Eq, Hash)]
21pub struct QueryID {
22    pub label: String,
23    pub uuid: Uuid,
24}
25
26/// Represents a unique identifier for a Queryable in the graph.
27#[derive(Debug, Clone, PartialEq, Eq, Hash)]
28pub struct QueryableID {
29    pub label: String,
30    pub uuid: Uuid,
31}
32
33/// Represents a unique identifier for a primitive in the graph.
34#[derive(Debug, Clone, PartialEq, Eq, Hash)]
35pub enum PrimitiveID {
36    Input(InputID),
37    Output(OutputID),
38    Query(QueryID),
39    Queryable(QueryableID),
40}
41
42macro_rules! impl_into_primitive {
43    ($($ty:ty => $variant:ident),*) => {
44        $(
45            impl From<$ty> for PrimitiveID {
46                fn from(value: $ty) -> PrimitiveID {
47                    PrimitiveID::$variant(value)
48                }
49            }
50
51            impl From<&$ty> for PrimitiveID {
52                fn from(value: &$ty) -> PrimitiveID {
53                    PrimitiveID::$variant(value.clone())
54                }
55            }
56        )*
57    };
58}
59
60impl_into_primitive!(
61    InputID => Input,
62    OutputID => Output,
63    QueryID => Query,
64    QueryableID => Queryable
65);
66
67macro_rules! impl_into_uuid {
68    ($($ty:ty),*) => {
69        $(
70            impl From<$ty> for Uuid {
71                fn from(value: $ty) -> Uuid {
72                    value.uuid
73                }
74            }
75
76            impl From<&$ty> for Uuid {
77                fn from(value: &$ty) -> Uuid {
78                    value.uuid
79                }
80            }
81        )*
82    };
83}
84
85impl_into_uuid!(InputID, OutputID, QueryID, QueryableID);
86
87impl PrimitiveID {
88    pub fn label(&self) -> String {
89        match self {
90            PrimitiveID::Input(input) => input.label.clone(),
91            PrimitiveID::Output(output) => output.label.clone(),
92            PrimitiveID::Query(query) => query.label.clone(),
93            PrimitiveID::Queryable(queryable) => queryable.label.clone(),
94        }
95    }
96
97    pub fn uuid(&self) -> Uuid {
98        match self {
99            PrimitiveID::Input(input) => input.uuid,
100            PrimitiveID::Output(output) => output.uuid,
101            PrimitiveID::Query(query) => query.uuid,
102            PrimitiveID::Queryable(queryable) => queryable.uuid,
103        }
104    }
105}
106
107impl TryFrom<PrimitiveID> for InputID {
108    type Error = eyre::Report;
109
110    fn try_from(value: PrimitiveID) -> eyre::Result<Self> {
111        match value {
112            PrimitiveID::Input(layout) => Ok(layout),
113            _ => Err(eyre::eyre!("Invalid type")),
114        }
115    }
116}
117
118impl TryFrom<PrimitiveID> for OutputID {
119    type Error = eyre::Report;
120
121    fn try_from(value: PrimitiveID) -> eyre::Result<Self> {
122        match value {
123            PrimitiveID::Output(layout) => Ok(layout),
124            _ => Err(eyre::eyre!("Invalid type")),
125        }
126    }
127}
128
129impl TryFrom<PrimitiveID> for QueryID {
130    type Error = eyre::Report;
131
132    fn try_from(value: PrimitiveID) -> eyre::Result<Self> {
133        match value {
134            PrimitiveID::Query(layout) => Ok(layout),
135            _ => Err(eyre::eyre!("Invalid type")),
136        }
137    }
138}
139
140impl TryFrom<PrimitiveID> for QueryableID {
141    type Error = eyre::Report;
142
143    fn try_from(value: PrimitiveID) -> eyre::Result<Self> {
144        match value {
145            PrimitiveID::Queryable(layout) => Ok(layout),
146            _ => Err(eyre::eyre!("Invalid type")),
147        }
148    }
149}
150
151impl From<PrimitiveID> for Uuid {
152    fn from(val: PrimitiveID) -> Self {
153        match val {
154            PrimitiveID::Input(input) => input.uuid,
155            PrimitiveID::Output(output) => output.uuid,
156            PrimitiveID::Query(query) => query.uuid,
157            PrimitiveID::Queryable(queryable) => queryable.uuid,
158        }
159    }
160}
161
162impl AsRef<Uuid> for PrimitiveID {
163    fn as_ref(&self) -> &Uuid {
164        match self {
165            PrimitiveID::Input(input) => &input.uuid,
166            PrimitiveID::Output(output) => &output.uuid,
167            PrimitiveID::Query(query) => &query.uuid,
168            PrimitiveID::Queryable(queryable) => &queryable.uuid,
169        }
170    }
171}
172
173impl From<&PrimitiveID> for Uuid {
174    fn from(val: &PrimitiveID) -> Self {
175        match val {
176            PrimitiveID::Input(input) => input.uuid,
177            PrimitiveID::Output(output) => output.uuid,
178            PrimitiveID::Query(query) => query.uuid,
179            PrimitiveID::Queryable(queryable) => queryable.uuid,
180        }
181    }
182}