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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use std::collections::HashMap;

use crate::error::Error;
use crate::port::{PortDirection, PortReference};
use crate::schematic::{ConnectionIndex, NodeIndex, PortIndex};
use crate::util::AsStr;

#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub enum NodeKind {
  Input(NodeReference),
  Output(NodeReference),
  Inherent(NodeReference),
  External(NodeReference),
}

impl NodeKind {
  pub fn input() -> Self {
    NodeKind::Input(NodeReference {
      name: crate::SCHEMATIC_INPUT.to_owned(),
      component_id: crate::NS_SCHEMATIC.to_owned(),
    })
  }
  pub fn output() -> Self {
    NodeKind::Output(NodeReference {
      name: crate::SCHEMATIC_OUTPUT.to_owned(),
      component_id: crate::NS_SCHEMATIC.to_owned(),
    })
  }
  pub fn cref(&self) -> &NodeReference {
    match self {
      NodeKind::Input(c) => c,
      NodeKind::Output(c) => c,
      NodeKind::Inherent(c) => c,
      NodeKind::External(c) => c,
    }
  }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct NodeReference {
  name: String,
  component_id: String,
}

impl NodeReference {
  pub fn new<T: AsStr, U: AsStr>(component_id: T, name: U) -> Self {
    Self {
      name: name.as_ref().to_owned(),
      component_id: component_id.as_ref().to_owned(),
    }
  }

  #[must_use]
  pub fn component_id(&self) -> &str {
    &self.component_id
  }

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

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

impl std::fmt::Display for NodeKind {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(
      f,
      "{}",
      match self {
        NodeKind::Input(v) => format!("Input({})", v),
        NodeKind::Output(v) => format!("Output({})", v),
        NodeKind::Inherent(v) => format!("Inherent({})", v),
        NodeKind::External(v) => format!("External({})", v),
      }
    )
  }
}

#[derive(Debug, Clone)]
#[must_use]
pub struct Node<DATA> {
  pub name: String,
  kind: NodeKind,
  index: NodeIndex,
  data: Option<DATA>,
  inputs: PortList,
  outputs: PortList,
}

impl<DATA> PartialEq for Node<DATA> {
  fn eq(&self, other: &Self) -> bool {
    self.index == other.index
  }
}

impl<DATA> Node<DATA>
where
  DATA: Clone,
{
  pub(crate) fn new<T: AsStr>(name: T, index: NodeIndex, kind: NodeKind, data: Option<DATA>) -> Self {
    Self {
      name: name.as_ref().to_owned(),
      kind,
      index,
      data,
      inputs: PortList::new(PortDirection::In),
      outputs: PortList::new(PortDirection::Out),
    }
  }

  pub fn kind(&self) -> &NodeKind {
    &self.kind
  }

  pub fn cref(&self) -> &NodeReference {
    self.kind.cref()
  }

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

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

  #[must_use]
  pub fn data(&self) -> &Option<DATA> {
    &self.data
  }

  #[must_use]
  pub fn has_data(&self) -> bool {
    self.data.is_some()
  }

  pub fn inputs(&self) -> &[NodePort] {
    self.inputs.inner()
  }

  #[must_use]
  pub fn input_refs(&self) -> Vec<PortReference> {
    self.inputs.inner().iter().map(|c| c.port).collect()
  }

  #[must_use]
  pub fn find_input(&self, name: &str) -> Option<&NodePort> {
    self.inputs.find(name)
  }

  #[must_use]
  pub fn get_input(&self, index: PortIndex) -> Option<&NodePort> {
    self.inputs.get(index)
  }

  pub fn add_input<T: AsStr>(&mut self, port: T) -> PortReference {
    let port_ref = match self.kind {
      NodeKind::Output(_) => {
        self.outputs.add(&port, self.index);
        self.inputs.add(&port, self.index)
      }
      NodeKind::Input(_) | NodeKind::Inherent(_) => {
        // Input/Output nodes have the same ports in & out.
        panic!("You can not manually add inputs to {} nodes", self.kind);
      }
      NodeKind::External(_) => self.inputs.add(&port, self.index),
    };
    trace!(
      index = self.index,
      port = port.as_ref(),
      node = self.id(),
      r#ref = %port_ref,
      "added input port"
    );
    port_ref
  }

  pub(crate) fn connect_input(&mut self, port: PortIndex, connection: ConnectionIndex) -> Result<(), Error> {
    self.inputs.add_connection(port, connection)
  }

  pub(crate) fn input_connections(&self, port: PortIndex) -> Option<&Vec<ConnectionIndex>> {
    self.inputs.port_connections(port)
  }

  pub(crate) fn all_upstreams(&self) -> Vec<ConnectionIndex> {
    self.inputs.all_connections()
  }

  pub fn outputs(&self) -> &[NodePort] {
    self.outputs.inner()
  }

  #[must_use]
  pub fn output_refs(&self) -> Vec<PortReference> {
    self.outputs.inner().iter().map(|c| c.port).collect()
  }

  #[must_use]
  pub fn find_output(&self, name: &str) -> Option<&NodePort> {
    self.outputs.find(name)
  }

  #[must_use]
  pub fn get_output(&self, index: PortIndex) -> Option<&NodePort> {
    self.outputs.get(index)
  }

  pub fn add_output<T: AsStr>(&mut self, port: T) -> PortReference {
    let port_ref = match self.kind {
      NodeKind::Input(_) | NodeKind::Inherent(_) => {
        self.inputs.add(&port, self.index);
        self.outputs.add(&port, self.index)
      }
      NodeKind::Output(_) => {
        // Input/Output nodes have the same ports in & out.
        panic!("You can not manually add outputs to {} nodes", self.kind);
      }
      NodeKind::External(_) => self.outputs.add(&port, self.index),
    };
    trace!(
      index = self.index,
      port = port.as_ref(),
      node = self.id(),
      r#ref = %port_ref,
      "added output port"
    );

    port_ref
  }

  pub(crate) fn connect_output(&mut self, port: PortIndex, connection: ConnectionIndex) -> Result<(), Error> {
    self.outputs.add_connection(port, connection)
  }

  pub(crate) fn output_connections(&self, port: PortIndex) -> Option<&Vec<ConnectionIndex>> {
    self.outputs.port_connections(port)
  }

  pub(crate) fn all_downstreams(&self) -> Vec<ConnectionIndex> {
    self.outputs.all_connections()
  }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct PortList {
  direction: PortDirection,
  map: HashMap<String, PortIndex>,
  list: Vec<NodePort>,
}

impl PortList {
  fn new(direction: PortDirection) -> Self {
    Self {
      direction,
      map: Default::default(),
      list: Default::default(),
    }
  }
  fn find(&self, name: &str) -> Option<&NodePort> {
    self.map.get(name).map(|index| &self.list[*index])
  }

  fn get(&self, index: PortIndex) -> Option<&NodePort> {
    self.list.get(index)
  }

  fn inner(&self) -> &[NodePort] {
    &self.list
  }

  fn add<T: AsStr>(&mut self, port_name: T, node_index: NodeIndex) -> PortReference {
    let name = port_name.as_ref();
    let existing_index = self.map.get(name);
    match existing_index {
      Some(index) => self.list[*index].port,
      None => {
        let index = self.list.len();
        let port_ref = PortReference::new(node_index, index, self.direction);
        self.map.insert(name.to_owned(), index);
        let port = NodePort::new(name, port_ref);
        self.list.push(port);
        port_ref
      }
    }
  }

  fn add_connection(&mut self, port: PortIndex, connection: ConnectionIndex) -> Result<(), Error> {
    let node_port = self.list.get_mut(port).ok_or(Error::InvalidPortIndex(port))?;

    if node_port.direction() == &PortDirection::In && !node_port.connections.is_empty() {
      return Err(Error::MultipleInputConnections(node_port.to_string()));
    }
    node_port.connections.push(connection);
    Ok(())
  }

  fn port_connections(&self, port: PortIndex) -> Option<&Vec<ConnectionIndex>> {
    self.list.get(port).map(|node| &node.connections)
  }

  fn all_connections(&self) -> Vec<ConnectionIndex> {
    self.list.iter().cloned().flat_map(|port| port.connections).collect()
  }
}

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

#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct NodePort {
  name: String,
  port: PortReference,
  connections: Vec<ConnectionIndex>,
}

impl NodePort {
  pub(crate) fn new<T: AsStr>(name: T, port: PortReference) -> Self {
    Self {
      name: name.as_ref().to_owned(),
      port,
      connections: Default::default(),
    }
  }

  #[must_use]
  pub fn is_graph_output(&self) -> bool {
    self.port.node_index == crate::schematic::SCHEMATIC_OUTPUT_INDEX
  }

  #[must_use]
  pub fn is_graph_input(&self) -> bool {
    self.port.node_index == crate::schematic::SCHEMATIC_INPUT_INDEX
  }

  #[must_use]
  pub fn connections(&self) -> &[ConnectionIndex] {
    &self.connections
  }

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

  #[must_use]
  pub fn detached(&self) -> PortReference {
    self.port
  }

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

impl From<&NodePort> for PortReference {
  fn from(port: &NodePort) -> Self {
    port.port
  }
}

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

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