flow_graph/schematic/iterators/
node.rs

1use super::*;
2use crate::node::{Node, NodeKind};
3use crate::{NodeIndex, Schematic};
4#[derive(Debug, Clone)]
5#[must_use]
6pub struct NodeHop<'graph, DATA> {
7  index: NodeIndex,
8  schematic: &'graph Schematic<DATA>,
9}
10
11impl<'graph, DATA> std::fmt::Display for NodeHop<'graph, DATA>
12where
13  DATA: Clone,
14{
15  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16    write!(f, "{}", self.name())
17  }
18}
19
20impl<'graph, DATA> NodeHop<'graph, DATA>
21where
22  DATA: Clone,
23{
24  pub const fn new(schematic: &'graph Schematic<DATA>, index: NodeIndex) -> Self {
25    Self { schematic, index }
26  }
27
28  pub fn downstreams(&self) -> Connections<'graph, DATA> {
29    let node = &self.schematic.nodes[self.index];
30    let connections = node.all_downstreams();
31    Connections::new(self.schematic, connections)
32  }
33
34  pub fn upstreams(&self) -> Connections<'graph, DATA> {
35    let node = &self.schematic.nodes[self.index];
36    let connections = node.all_upstreams();
37    Connections::new(self.schematic, connections)
38  }
39
40  pub fn into_outputs(self) -> Ports<'graph, DATA> {
41    let node = &self.schematic.nodes[self.index];
42    let ports = node.outputs().iter().map(|p| p.detached()).collect();
43    Ports::new(self.schematic, node.index(), ports)
44  }
45
46  pub fn into_inputs(self) -> Ports<'graph, DATA> {
47    let node = &self.schematic.nodes[self.index];
48    let ports = node.inputs().iter().map(|p| p.detached()).collect();
49    Ports::new(self.schematic, node.index(), ports)
50  }
51
52  #[must_use]
53  pub fn name(&self) -> &str {
54    self.schematic.nodes[self.index].id()
55  }
56
57  #[must_use]
58  pub const fn index(&self) -> NodeIndex {
59    self.index
60  }
61
62  pub fn inner(&self) -> &'graph Node<DATA> {
63    &self.schematic.nodes[self.index]
64  }
65
66  pub fn kind(&self) -> &NodeKind {
67    self.schematic.nodes[self.index].kind()
68  }
69}