Skip to main content

neco_nodegraph/
port.rs

1use alloc::string::{String, ToString};
2
3use crate::error::GraphError;
4use crate::id::PortId;
5
6/// Port direction used for edge validation.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum PortDirection {
9    Input,
10    Output,
11}
12
13/// A typed port belonging to a node.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct Port {
16    id: PortId,
17    direction: PortDirection,
18    type_tag: String,
19}
20
21impl Port {
22    /// Creates a port. `type_tag` must not be empty.
23    pub fn new(id: PortId, direction: PortDirection, type_tag: &str) -> Result<Self, GraphError> {
24        if type_tag.is_empty() {
25            return Err(GraphError::EmptyTypeTag);
26        }
27
28        Ok(Self {
29            id,
30            direction,
31            type_tag: type_tag.to_string(),
32        })
33    }
34
35    /// Returns the port identifier.
36    pub fn id(&self) -> &PortId {
37        &self.id
38    }
39
40    /// Returns the direction used for connection validation.
41    pub const fn direction(&self) -> PortDirection {
42        self.direction
43    }
44
45    /// Returns the application-defined type tag.
46    pub fn type_tag(&self) -> &str {
47        self.type_tag.as_str()
48    }
49}