Skip to main content

jellyflow_core/core/model/
graph.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::core::ids::{
6    BindingId, EdgeId, GraphId, GroupId, NodeId, PortId, StickyNoteId, SymbolId,
7};
8use crate::core::imports::GraphImport;
9
10use super::binding::Binding;
11use super::edge::Edge;
12use super::node::Node;
13use super::port::Port;
14use super::resources::{Group, StickyNote, Symbol};
15
16/// Graph schema version (v1).
17pub const GRAPH_VERSION: u32 = 1;
18
19/// Node graph document.
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct Graph {
22    /// Stable identity for editor-state lookup and cross-graph references.
23    pub graph_id: GraphId,
24    /// Schema version for migrations.
25    pub graph_version: u32,
26
27    /// Transitive graph dependencies (semantic subgraphs / libraries).
28    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
29    pub imports: BTreeMap<GraphId, GraphImport>,
30
31    /// Graph-scoped symbols (blackboard/variables).
32    pub symbols: BTreeMap<SymbolId, Symbol>,
33
34    /// Node instances.
35    pub nodes: BTreeMap<NodeId, Node>,
36
37    /// Port instances (owned by nodes, but stored in a flat map for stable lookup).
38    pub ports: BTreeMap<PortId, Port>,
39
40    /// Edges between ports.
41    pub edges: BTreeMap<EdgeId, Edge>,
42
43    /// Optional groups.
44    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
45    pub groups: BTreeMap<GroupId, Group>,
46
47    /// Optional sticky notes.
48    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
49    pub sticky_notes: BTreeMap<StickyNoteId, StickyNote>,
50
51    /// Optional knowledge-canvas bindings.
52    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
53    pub bindings: BTreeMap<BindingId, Binding>,
54}
55
56impl Default for Graph {
57    fn default() -> Self {
58        Self::new(GraphId::new())
59    }
60}
61
62impl Graph {
63    /// Creates a new, empty graph with the given id.
64    pub fn new(graph_id: GraphId) -> Self {
65        Self {
66            graph_id,
67            graph_version: GRAPH_VERSION,
68            imports: BTreeMap::new(),
69            symbols: BTreeMap::new(),
70            nodes: BTreeMap::new(),
71            ports: BTreeMap::new(),
72            edges: BTreeMap::new(),
73            groups: BTreeMap::new(),
74            sticky_notes: BTreeMap::new(),
75            bindings: BTreeMap::new(),
76        }
77    }
78}