protoflow_core/
port.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::{
4    prelude::{fmt, MaybeLabeled, MaybeNamed, ToString},
5    PortError, PortID, PortResult, PortState,
6};
7
8/// The common interface for ports, whether for input or output.
9pub trait Port: MaybeNamed + MaybeLabeled {
10    /// A unique identifier for this port.
11    fn id(&self) -> PortID;
12
13    /// The current state of this port.
14    fn state(&self) -> PortState;
15
16    /// Checks whether this port is currently closed.
17    fn is_closed(&self) -> bool {
18        self.state().is_closed()
19    }
20
21    /// Checks whether this port is currently open.
22    fn is_open(&self) -> bool {
23        self.state().is_open()
24    }
25
26    /// Checks whether this port is currently connected.
27    fn is_connected(&self) -> bool {
28        self.state().is_connected()
29    }
30
31    /// Closes this port, returning immediately.
32    ///
33    /// If the port had an open connection, it will be disconnected.
34    /// If the port was already closed, no further action is taken.
35    /// There is no facility to reopen a port once it has been closed.
36    ///
37    /// Returns `Ok(true)` if the port was successfully closed.
38    /// Returns `Ok(false)` if the port was already closed.
39    /// Returns `Err(PortError)` if an error occurs.
40    fn close(&mut self) -> PortResult<bool> {
41        Err(PortError::Other("not implemented".to_string()))
42    }
43}
44
45impl fmt::Debug for &dyn Port {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        f.debug_struct("Port")
48            .field("id", &self.id())
49            .field("name", &self.name())
50            .field("state", &self.state())
51            .finish()
52    }
53}