1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use super::*;
use crate::node::{Node, NodeKind};
use crate::{NodeIndex, Schematic};
#[derive(Debug, Clone)]
#[must_use]
pub struct NodeHop<'graph, DATA> {
  index: NodeIndex,
  schematic: &'graph Schematic<DATA>,
}

impl<'graph, DATA> std::fmt::Display for NodeHop<'graph, DATA>
where
  DATA: Clone,
{
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.name())
  }
}

impl<'graph, DATA> NodeHop<'graph, DATA>
where
  DATA: Clone,
{
  pub const fn new(schematic: &'graph Schematic<DATA>, index: NodeIndex) -> Self {
    Self { schematic, index }
  }

  pub fn downstreams(&self) -> Connections<'graph, DATA> {
    let node = &self.schematic.nodes[self.index];
    let connections = node.all_downstreams();
    Connections::new(self.schematic, connections)
  }

  pub fn upstreams(&self) -> Connections<'graph, DATA> {
    let node = &self.schematic.nodes[self.index];
    let connections = node.all_upstreams();
    Connections::new(self.schematic, connections)
  }

  pub fn into_outputs(self) -> Ports<'graph, DATA> {
    let node = &self.schematic.nodes[self.index];
    let ports = node.outputs().iter().map(|p| p.detached()).collect();
    Ports::new(self.schematic, node.index(), ports)
  }

  pub fn into_inputs(self) -> Ports<'graph, DATA> {
    let node = &self.schematic.nodes[self.index];
    let ports = node.inputs().iter().map(|p| p.detached()).collect();
    Ports::new(self.schematic, node.index(), ports)
  }

  #[must_use]
  pub fn name(&self) -> &str {
    self.schematic.nodes[self.index].id()
  }

  #[must_use]
  pub const fn index(&self) -> NodeIndex {
    self.index
  }

  pub fn inner(&self) -> &'graph Node<DATA> {
    &self.schematic.nodes[self.index]
  }

  pub fn kind(&self) -> &NodeKind {
    self.schematic.nodes[self.index].kind()
  }
}