Skip to main content

jellyflow_core/core/
ids.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4/// Stable identifier for a graph document.
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
6#[serde(transparent)]
7pub struct GraphId(pub Uuid);
8
9impl GraphId {
10    /// Generates a new random graph id.
11    pub fn new() -> Self {
12        Self(Uuid::new_v4())
13    }
14
15    /// Creates a graph id from a stable 128-bit value.
16    pub fn from_u128(value: u128) -> Self {
17        Self(Uuid::from_u128(value))
18    }
19
20    /// Creates a graph id from raw UUID bytes.
21    pub fn from_bytes(bytes: [u8; 16]) -> Self {
22        Self(Uuid::from_bytes(bytes))
23    }
24}
25
26impl std::fmt::Display for GraphId {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        self.0.fmt(f)
29    }
30}
31
32/// Stable identifier for a node instance.
33#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
34#[serde(transparent)]
35pub struct NodeId(pub Uuid);
36
37impl NodeId {
38    /// Generates a new random node id.
39    pub fn new() -> Self {
40        Self(Uuid::new_v4())
41    }
42
43    /// Creates a node id from a stable 128-bit value.
44    pub fn from_u128(value: u128) -> Self {
45        Self(Uuid::from_u128(value))
46    }
47}
48
49/// Stable identifier for a port instance.
50#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
51#[serde(transparent)]
52pub struct PortId(pub Uuid);
53
54impl PortId {
55    /// Generates a new random port id.
56    pub fn new() -> Self {
57        Self(Uuid::new_v4())
58    }
59
60    /// Creates a port id from a stable 128-bit value.
61    pub fn from_u128(value: u128) -> Self {
62        Self(Uuid::from_u128(value))
63    }
64}
65
66/// Stable identifier for an edge instance.
67#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
68#[serde(transparent)]
69pub struct EdgeId(pub Uuid);
70
71impl EdgeId {
72    /// Generates a new random edge id.
73    pub fn new() -> Self {
74        Self(Uuid::new_v4())
75    }
76
77    /// Creates an edge id from a stable 128-bit value.
78    pub fn from_u128(value: u128) -> Self {
79        Self(Uuid::from_u128(value))
80    }
81}
82
83/// Stable identifier for a graph-scoped symbol.
84#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
85#[serde(transparent)]
86pub struct SymbolId(pub Uuid);
87
88impl SymbolId {
89    /// Generates a new random symbol id.
90    pub fn new() -> Self {
91        Self(Uuid::new_v4())
92    }
93
94    /// Creates a symbol id from a stable 128-bit value.
95    pub fn from_u128(value: u128) -> Self {
96        Self(Uuid::from_u128(value))
97    }
98}
99
100/// Stable identifier for a group.
101#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
102#[serde(transparent)]
103pub struct GroupId(pub Uuid);
104
105impl GroupId {
106    /// Generates a new random group id.
107    pub fn new() -> Self {
108        Self(Uuid::new_v4())
109    }
110
111    /// Creates a group id from a stable 128-bit value.
112    pub fn from_u128(value: u128) -> Self {
113        Self(Uuid::from_u128(value))
114    }
115}
116
117/// Stable identifier for a sticky note.
118#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
119#[serde(transparent)]
120pub struct StickyNoteId(pub Uuid);
121
122impl StickyNoteId {
123    /// Generates a new random sticky note id.
124    pub fn new() -> Self {
125        Self(Uuid::new_v4())
126    }
127
128    /// Creates a sticky note id from a stable 128-bit value.
129    pub fn from_u128(value: u128) -> Self {
130        Self(Uuid::from_u128(value))
131    }
132}
133
134/// Stable identifier for a knowledge-canvas binding.
135#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
136#[serde(transparent)]
137pub struct BindingId(pub Uuid);
138
139impl BindingId {
140    /// Generates a new random binding id.
141    pub fn new() -> Self {
142        Self(Uuid::new_v4())
143    }
144
145    /// Creates a binding id from a stable 128-bit value.
146    pub fn from_u128(value: u128) -> Self {
147        Self(Uuid::from_u128(value))
148    }
149}
150
151/// Stable identifier for a node kind.
152///
153/// This is a namespaced string identifier (e.g. `core.math.add`, `plugin.acme.http_request`).
154#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
155#[serde(transparent)]
156pub struct NodeKindKey(pub String);
157
158impl NodeKindKey {
159    /// Creates a new node kind key.
160    pub fn new(key: impl Into<String>) -> Self {
161        Self(key.into())
162    }
163}
164
165/// Stable identifier for a schema-declared port.
166///
167/// `PortKey` must remain stable across versions of a node kind.
168#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
169#[serde(transparent)]
170pub struct PortKey(pub String);
171
172impl PortKey {
173    /// Creates a new port key.
174    pub fn new(key: impl Into<String>) -> Self {
175        Self(key.into())
176    }
177}