Skip to main content

openquiver_graph/
edge.rs

1//! Wires between pins.
2
3use serde::{Deserialize, Serialize};
4
5use crate::NodeName;
6
7/// The kind of wire connecting two pins.
8///
9/// Separating exec from data is the central idea of Blueprints and the main
10/// thing setting this system apart from dataflow builders: step order is stated
11/// explicitly rather than inferred from who passed a value to whom.
12///
13/// Variant order is significant. It defines the canonical edge order in a file,
14/// where the exec block precedes the data block.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
16pub enum EdgeKind {
17    Exec,
18    Data,
19}
20
21/// A reference to one pin of one node, as in `ask.text`.
22#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
23pub struct PinRef {
24    pub(crate) node: NodeName,
25    pub(crate) pin: String,
26}
27
28impl PinRef {
29    pub fn new(node: NodeName, pin: impl Into<String>) -> Self {
30        Self {
31            node,
32            pin: pin.into(),
33        }
34    }
35
36    pub fn node(&self) -> &str {
37        self.node.as_str()
38    }
39
40    pub fn pin(&self) -> &str {
41        &self.pin
42    }
43}
44
45/// A wire between two pins.
46#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
47pub struct Edge {
48    pub(crate) kind: EdgeKind,
49    from: PinRef,
50    to: PinRef,
51}
52
53impl Edge {
54    pub fn new(kind: EdgeKind, from: PinRef, to: PinRef) -> Self {
55        Self { kind, from, to }
56    }
57
58    pub fn kind(&self) -> EdgeKind {
59        self.kind
60    }
61
62    pub fn from(&self) -> &PinRef {
63        &self.from
64    }
65
66    pub fn to(&self) -> &PinRef {
67        &self.to
68    }
69}