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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::fmt::Display;

use crate::schematic::PortIndex;
use crate::NodeIndex;

#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PortDefinition {
  pub name: String,
  pub index: PortIndex,
}

impl PortDefinition {
  pub fn new<T: Into<String>>(name: T, index: PortIndex) -> Self {
    Self {
      name: name.into(),
      index,
    }
  }
}

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

#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
pub struct PortReference {
  pub(crate) node_index: NodeIndex,
  pub(crate) port_index: PortIndex,
  pub(crate) direction: PortDirection,
}

impl PortReference {
  #[must_use]
  pub const fn new(node_index: NodeIndex, port_index: PortIndex, direction: PortDirection) -> Self {
    Self {
      node_index,
      port_index,
      direction,
    }
  }

  pub const fn direction(&self) -> &PortDirection {
    &self.direction
  }

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

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

impl AsRef<PortReference> for PortReference {
  fn as_ref(&self) -> &PortReference {
    self
  }
}

impl Display for PortReference {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self.direction {
      PortDirection::In => write!(f, "{}.IN.{}", self.node_index, self.port_index),
      PortDirection::Out => write!(f, "{}.OUT.{}", self.node_index, self.port_index),
    }
  }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[must_use]
#[allow(clippy::exhaustive_enums)]
pub enum PortDirection {
  In,
  Out,
}

impl Display for PortDirection {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(
      f,
      "{}",
      match self {
        PortDirection::In => "In",
        PortDirection::Out => "Out",
      }
    )
  }
}