Skip to main content

jellyflow_runtime/runtime/lookups/
parents.rs

1use std::collections::BTreeMap;
2
3use super::NodeGraphLookups;
4use jellyflow_core::core::{GroupId, NodeId};
5
6impl NodeGraphLookups {
7    pub fn parent_for_node(&self, node: NodeId) -> Option<GroupId> {
8        self.node_lookup.get(&node).and_then(|entry| entry.parent)
9    }
10
11    pub fn child_nodes_for_parent(&self, parent: GroupId) -> Vec<NodeId> {
12        let mut children: Vec<NodeId> = self
13            .node_lookup
14            .iter()
15            .filter_map(|(node, entry)| (entry.parent == Some(parent)).then_some(*node))
16            .collect();
17        children.sort();
18        children
19    }
20
21    pub fn child_nodes_by_parent(&self) -> BTreeMap<GroupId, Vec<NodeId>> {
22        let mut out: BTreeMap<GroupId, Vec<NodeId>> = BTreeMap::new();
23        for (node, entry) in &self.node_lookup {
24            let Some(parent) = entry.parent else {
25                continue;
26            };
27            out.entry(parent).or_default().push(*node);
28        }
29        for children in out.values_mut() {
30            children.sort();
31        }
32        out
33    }
34
35    pub fn root_nodes(&self) -> Vec<NodeId> {
36        let mut roots: Vec<NodeId> = self
37            .node_lookup
38            .iter()
39            .filter_map(|(node, entry)| entry.parent.is_none().then_some(*node))
40            .collect();
41        roots.sort();
42        roots
43    }
44}