Skip to main content

reifydb_core/interface/catalog/
flow.rs

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