reifydb_core/interface/catalog/
flow.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use std::{
5	fmt,
6	fmt::{Display, Formatter},
7	ops::Deref,
8};
9
10use reifydb_type::Blob;
11use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Visitor};
12
13use crate::interface::NamespaceId;
14
15#[repr(transparent)]
16#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
17pub struct FlowId(pub u64);
18
19impl Deref for FlowId {
20	type Target = u64;
21
22	fn deref(&self) -> &Self::Target {
23		&self.0
24	}
25}
26
27impl PartialEq<u64> for FlowId {
28	fn eq(&self, other: &u64) -> bool {
29		self.0.eq(other)
30	}
31}
32
33impl From<FlowId> for u64 {
34	fn from(value: FlowId) -> Self {
35		value.0
36	}
37}
38
39impl From<u64> for FlowId {
40	fn from(value: u64) -> Self {
41		Self(value)
42	}
43}
44
45impl Serialize for FlowId {
46	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
47	where
48		S: Serializer,
49	{
50		serializer.serialize_u64(self.0)
51	}
52}
53
54impl<'de> Deserialize<'de> for FlowId {
55	fn deserialize<D>(deserializer: D) -> Result<FlowId, D::Error>
56	where
57		D: Deserializer<'de>,
58	{
59		struct U64Visitor;
60
61		impl Visitor<'_> for U64Visitor {
62			type Value = FlowId;
63
64			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
65				formatter.write_str("an unsigned 64-bit number")
66			}
67
68			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
69				Ok(FlowId(value))
70			}
71		}
72
73		deserializer.deserialize_u64(U64Visitor)
74	}
75}
76
77#[repr(transparent)]
78#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
79pub struct FlowNodeId(pub u64);
80
81impl Display for FlowNodeId {
82	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
83		Display::fmt(&self.0, f)
84	}
85}
86
87impl Deref for FlowNodeId {
88	type Target = u64;
89
90	fn deref(&self) -> &Self::Target {
91		&self.0
92	}
93}
94
95impl PartialEq<u64> for FlowNodeId {
96	fn eq(&self, other: &u64) -> bool {
97		self.0.eq(other)
98	}
99}
100
101impl From<FlowNodeId> for u64 {
102	fn from(value: FlowNodeId) -> Self {
103		value.0
104	}
105}
106
107impl From<&FlowNodeId> for FlowNodeId {
108	fn from(value: &FlowNodeId) -> Self {
109		*value
110	}
111}
112
113impl From<u64> for FlowNodeId {
114	fn from(value: u64) -> Self {
115		Self(value)
116	}
117}
118
119impl Serialize for FlowNodeId {
120	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
121	where
122		S: Serializer,
123	{
124		serializer.serialize_u64(self.0)
125	}
126}
127
128impl<'de> Deserialize<'de> for FlowNodeId {
129	fn deserialize<D>(deserializer: D) -> Result<FlowNodeId, D::Error>
130	where
131		D: Deserializer<'de>,
132	{
133		struct U64Visitor;
134
135		impl Visitor<'_> for U64Visitor {
136			type Value = FlowNodeId;
137
138			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
139				formatter.write_str("an unsigned 64-bit number")
140			}
141
142			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
143				Ok(FlowNodeId(value))
144			}
145		}
146
147		deserializer.deserialize_u64(U64Visitor)
148	}
149}
150
151#[repr(transparent)]
152#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
153pub struct FlowEdgeId(pub u64);
154
155impl Deref for FlowEdgeId {
156	type Target = u64;
157
158	fn deref(&self) -> &Self::Target {
159		&self.0
160	}
161}
162
163impl PartialEq<u64> for FlowEdgeId {
164	fn eq(&self, other: &u64) -> bool {
165		self.0.eq(other)
166	}
167}
168
169impl From<FlowEdgeId> for u64 {
170	fn from(value: FlowEdgeId) -> Self {
171		value.0
172	}
173}
174
175impl From<u64> for FlowEdgeId {
176	fn from(value: u64) -> Self {
177		Self(value)
178	}
179}
180
181impl Serialize for FlowEdgeId {
182	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
183	where
184		S: Serializer,
185	{
186		serializer.serialize_u64(self.0)
187	}
188}
189
190impl<'de> Deserialize<'de> for FlowEdgeId {
191	fn deserialize<D>(deserializer: D) -> Result<FlowEdgeId, D::Error>
192	where
193		D: Deserializer<'de>,
194	{
195		struct U64Visitor;
196
197		impl Visitor<'_> for U64Visitor {
198			type Value = FlowEdgeId;
199
200			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
201				formatter.write_str("an unsigned 64-bit number")
202			}
203
204			fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
205				Ok(FlowEdgeId(value))
206			}
207		}
208
209		deserializer.deserialize_u64(U64Visitor)
210	}
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
214pub enum FlowStatus {
215	Active,
216	Paused,
217	Failed,
218}
219
220impl FlowStatus {
221	/// Convert FlowStatus to u8 for storage
222	pub fn to_u8(self) -> u8 {
223		match self {
224			FlowStatus::Active => 0,
225			FlowStatus::Paused => 1,
226			FlowStatus::Failed => 2,
227		}
228	}
229
230	/// Create FlowStatus from u8, defaulting to Failed for unknown values
231	pub fn from_u8(value: u8) -> Self {
232		match value {
233			0 => FlowStatus::Active,
234			1 => FlowStatus::Paused,
235			2 => FlowStatus::Failed,
236			_ => FlowStatus::Failed, // Default to Failed for unknown statuses
237		}
238	}
239}
240
241#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
242pub struct FlowDef {
243	pub id: FlowId,
244	pub namespace: NamespaceId,
245	pub name: String,
246	pub status: FlowStatus,
247}
248
249/// Catalog definition for a flow node
250/// The node type and its data are stored as a type discriminator and serialized blob
251#[derive(Debug, Clone, PartialEq)]
252pub struct FlowNodeDef {
253	pub id: FlowNodeId,
254	pub flow: FlowId,
255	pub node_type: u8, // FlowNodeType discriminator
256	pub data: Blob,    // Serialized FlowNodeType data
257}
258
259/// Catalog definition for a flow edge
260#[derive(Debug, Clone, PartialEq)]
261pub struct FlowEdgeDef {
262	pub id: FlowEdgeId,
263	pub flow: FlowId,
264	pub source: FlowNodeId,
265	pub target: FlowNodeId,
266}