flow_graph/
port.rs

1use std::fmt::Display;
2
3use crate::schematic::PortIndex;
4use crate::NodeIndex;
5
6#[derive(Debug, Clone, Default, PartialEq, Eq)]
7#[non_exhaustive]
8pub struct PortDefinition {
9  pub name: String,
10  pub index: PortIndex,
11}
12
13impl PortDefinition {
14  pub fn new<T: Into<String>>(name: T, index: PortIndex) -> Self {
15    Self {
16      name: name.into(),
17      index,
18    }
19  }
20}
21
22impl Display for PortDefinition {
23  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24    write!(f, "{}", self.name)
25  }
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
29pub struct PortReference {
30  pub(crate) node_index: NodeIndex,
31  pub(crate) port_index: PortIndex,
32  pub(crate) direction: PortDirection,
33}
34
35impl PortReference {
36  #[must_use]
37  pub const fn new(node_index: NodeIndex, port_index: PortIndex, direction: PortDirection) -> Self {
38    Self {
39      node_index,
40      port_index,
41      direction,
42    }
43  }
44
45  pub const fn direction(&self) -> &PortDirection {
46    &self.direction
47  }
48
49  #[must_use]
50  pub const fn node_index(&self) -> NodeIndex {
51    self.node_index
52  }
53
54  #[must_use]
55  pub const fn port_index(&self) -> PortIndex {
56    self.port_index
57  }
58}
59
60impl AsRef<PortReference> for PortReference {
61  fn as_ref(&self) -> &PortReference {
62    self
63  }
64}
65
66impl Display for PortReference {
67  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68    match self.direction {
69      PortDirection::In => write!(f, "{}.IN.{}", self.node_index, self.port_index),
70      PortDirection::Out => write!(f, "{}.OUT.{}", self.node_index, self.port_index),
71    }
72  }
73}
74#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
75#[must_use]
76#[allow(clippy::exhaustive_enums)]
77pub enum PortDirection {
78  In,
79  Out,
80}
81
82impl Display for PortDirection {
83  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84    write!(
85      f,
86      "{}",
87      match self {
88        PortDirection::In => "In",
89        PortDirection::Out => "Out",
90      }
91    )
92  }
93}