diff --git a/src/core.rs b/src/core.rs index 83bba3ab5..4df6b52a0 100644 --- a/src/core.rs +++ b/src/core.rs @@ -2,7 +2,10 @@ //! //! These types are re-exported in the root of the crate. +pub use itertools::Either; + use derive_more::From; +use itertools::Either::{Left, Right}; #[cfg(feature = "pyo3")] use pyo3::pyclass; @@ -92,35 +95,32 @@ impl Port { /// [HugrError::InvalidPortDirection] #[inline] pub fn as_incoming(&self) -> Result { - match self.direction() { - Direction::Incoming => Ok(IncomingPort { - index: self.index() as u16, - }), - dir @ Direction::Outgoing => Err(HugrError::InvalidPortDirection(dir)), - } + self.as_directed() + .left() + .ok_or(HugrError::InvalidPortDirection(self.direction())) } /// Converts to an [OutgoingPort] if this port is one; else fails with /// [HugrError::InvalidPortDirection] #[inline] pub fn as_outgoing(&self) -> Result { + self.as_directed() + .right() + .ok_or(HugrError::InvalidPortDirection(self.direction())) + } + + /// Converts to either an [IncomingPort] or an [OutgoingPort], as appropriate. + #[inline] + pub fn as_directed(&self) -> Either { match self.direction() { - Direction::Outgoing => Ok(OutgoingPort { + Direction::Incoming => Left(IncomingPort { + index: self.index() as u16, + }), + Direction::Outgoing => Right(OutgoingPort { index: self.index() as u16, }), - dir @ Direction::Incoming => Err(HugrError::InvalidPortDirection(dir)), } } - /// Creates a new outgoing port. - #[inline] - pub fn try_new_outgoing(port: impl TryInto) -> Result { - let Ok(port) = port.try_into() else { - return Err(HugrError::InvalidPortDirection(Direction::Incoming)); - }; - Ok(Self { - offset: portgraph::PortOffset::new_outgoing(port.index()), - }) - } /// Returns the direction of the port. #[inline] diff --git a/src/lib.rs b/src/lib.rs index d1675d6e7..80a7b8edd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ pub mod algorithm; pub mod builder; -mod core; +pub mod core; pub mod extension; pub mod hugr; pub mod macros;