Skip to main content

jellyflow_core/ops/fragment/
model.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::core::{
6    Binding, BindingId, Edge, EdgeId, GraphId, GraphImport, Group, GroupId, Node, NodeId, Port,
7    PortId, StickyNote, StickyNoteId, Symbol, SymbolId,
8};
9
10/// Wrapper version for `GraphFragment`.
11pub const GRAPH_FRAGMENT_VERSION: u32 = 1;
12
13/// A deterministic, serializable graph fragment.
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct GraphFragment {
16    pub version: u32,
17    pub nodes: BTreeMap<NodeId, Node>,
18    pub ports: BTreeMap<PortId, Port>,
19    pub edges: BTreeMap<EdgeId, Edge>,
20    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
21    pub imports: BTreeMap<GraphId, GraphImport>,
22    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
23    pub groups: BTreeMap<GroupId, Group>,
24    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
25    pub sticky_notes: BTreeMap<StickyNoteId, StickyNote>,
26    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
27    pub symbols: BTreeMap<SymbolId, Symbol>,
28    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
29    pub bindings: BTreeMap<BindingId, Binding>,
30}
31
32impl Default for GraphFragment {
33    fn default() -> Self {
34        Self {
35            version: GRAPH_FRAGMENT_VERSION,
36            nodes: BTreeMap::new(),
37            ports: BTreeMap::new(),
38            edges: BTreeMap::new(),
39            imports: BTreeMap::new(),
40            groups: BTreeMap::new(),
41            sticky_notes: BTreeMap::new(),
42            symbols: BTreeMap::new(),
43            bindings: BTreeMap::new(),
44        }
45    }
46}
47
48impl GraphFragment {
49    pub fn nodes(&self) -> &BTreeMap<NodeId, Node> {
50        &self.nodes
51    }
52
53    pub fn node_mut(&mut self, id: &NodeId) -> Option<&mut Node> {
54        self.nodes.get_mut(id)
55    }
56
57    pub fn insert_node(&mut self, id: NodeId, value: Node) -> Option<Node> {
58        self.nodes.insert(id, value)
59    }
60
61    pub fn ports(&self) -> &BTreeMap<PortId, Port> {
62        &self.ports
63    }
64
65    pub fn port_mut(&mut self, id: &PortId) -> Option<&mut Port> {
66        self.ports.get_mut(id)
67    }
68
69    pub fn insert_port(&mut self, id: PortId, value: Port) -> Option<Port> {
70        self.ports.insert(id, value)
71    }
72
73    pub fn edges(&self) -> &BTreeMap<EdgeId, Edge> {
74        &self.edges
75    }
76
77    pub fn edge_mut(&mut self, id: &EdgeId) -> Option<&mut Edge> {
78        self.edges.get_mut(id)
79    }
80
81    pub fn insert_edge(&mut self, id: EdgeId, value: Edge) -> Option<Edge> {
82        self.edges.insert(id, value)
83    }
84
85    pub fn imports(&self) -> &BTreeMap<GraphId, GraphImport> {
86        &self.imports
87    }
88
89    pub fn insert_import(&mut self, id: GraphId, value: GraphImport) -> Option<GraphImport> {
90        self.imports.insert(id, value)
91    }
92
93    pub fn groups(&self) -> &BTreeMap<GroupId, Group> {
94        &self.groups
95    }
96
97    pub fn group_mut(&mut self, id: &GroupId) -> Option<&mut Group> {
98        self.groups.get_mut(id)
99    }
100
101    pub fn insert_group(&mut self, id: GroupId, value: Group) -> Option<Group> {
102        self.groups.insert(id, value)
103    }
104
105    pub fn sticky_notes(&self) -> &BTreeMap<StickyNoteId, StickyNote> {
106        &self.sticky_notes
107    }
108
109    pub fn sticky_note_mut(&mut self, id: &StickyNoteId) -> Option<&mut StickyNote> {
110        self.sticky_notes.get_mut(id)
111    }
112
113    pub fn insert_sticky_note(
114        &mut self,
115        id: StickyNoteId,
116        value: StickyNote,
117    ) -> Option<StickyNote> {
118        self.sticky_notes.insert(id, value)
119    }
120
121    pub fn symbols(&self) -> &BTreeMap<SymbolId, Symbol> {
122        &self.symbols
123    }
124
125    pub fn symbol_mut(&mut self, id: &SymbolId) -> Option<&mut Symbol> {
126        self.symbols.get_mut(id)
127    }
128
129    pub fn insert_symbol(&mut self, id: SymbolId, value: Symbol) -> Option<Symbol> {
130        self.symbols.insert(id, value)
131    }
132
133    pub fn bindings(&self) -> &BTreeMap<BindingId, Binding> {
134        &self.bindings
135    }
136
137    pub fn binding_mut(&mut self, id: &BindingId) -> Option<&mut Binding> {
138        self.bindings.get_mut(id)
139    }
140
141    pub fn insert_binding(&mut self, id: BindingId, value: Binding) -> Option<Binding> {
142        self.bindings.insert(id, value)
143    }
144}