daedalus_runtime/
handles.rs

1/// Handle to a node port (alias + port name).
2///
3/// ```
4/// use daedalus_runtime::handles::PortHandle;
5/// let port = PortHandle::new("node", "out");
6/// assert_eq!(port.node_alias, "node");
7/// ```
8#[derive(Clone, Debug)]
9pub struct PortHandle {
10    pub node_alias: String,
11    pub port: String,
12}
13
14impl PortHandle {
15    /// Build a new port handle.
16    pub fn new(node_alias: impl Into<String>, port: impl Into<String>) -> Self {
17        Self {
18            node_alias: node_alias.into(),
19            port: port.into(),
20        }
21    }
22}
23
24/// Handle to a node id + alias pair.
25///
26/// ```
27/// use daedalus_runtime::handles::NodeHandle;
28/// let node = NodeHandle::new("demo:node").alias("alias");
29/// assert_eq!(node.alias, "alias");
30/// ```
31#[derive(Clone, Debug)]
32pub struct NodeHandle {
33    pub id: String,
34    pub alias: String,
35}
36
37impl NodeHandle {
38    /// Create a handle that uses the id as the initial alias.
39    pub fn new(id: impl Into<String>) -> Self {
40        let id = id.into();
41        Self {
42            alias: id.clone(),
43            id,
44        }
45    }
46
47    /// Return a cloned handle with a new alias.
48    pub fn alias(&self, alias: impl Into<String>) -> Self {
49        let mut cloned = self.clone();
50        cloned.alias = alias.into();
51        cloned
52    }
53
54    /// Build an input port handle.
55    pub fn input(&self, name: impl Into<String>) -> PortHandle {
56        PortHandle::new(self.alias.clone(), name)
57    }
58
59    /// Build an output port handle.
60    pub fn output(&self, name: impl Into<String>) -> PortHandle {
61        PortHandle::new(self.alias.clone(), name)
62    }
63}
64
65/// Common interface for node handles.
66///
67/// ```
68/// use daedalus_runtime::handles::{NodeHandle, NodeHandleLike};
69/// let node = NodeHandle::new("demo");
70/// assert_eq!(node.id(), "demo");
71/// ```
72pub trait NodeHandleLike {
73    fn id(&self) -> &str;
74    fn alias(&self) -> &str;
75}
76
77impl NodeHandleLike for NodeHandle {
78    fn id(&self) -> &str {
79        &self.id
80    }
81
82    fn alias(&self) -> &str {
83        &self.alias
84    }
85}
86
87impl<T> NodeHandleLike for &T
88where
89    T: NodeHandleLike + ?Sized,
90{
91    fn id(&self) -> &str {
92        (*self).id()
93    }
94
95    fn alias(&self) -> &str {
96        (*self).alias()
97    }
98}