use serde::Serialize;
pub type PortId = u16;
#[derive(Debug, Clone, Serialize)]
pub struct Port {
pub port: PortId,
pub label: Option<&'static str>,
pub description: Option<&'static str>,
}
impl Port {
pub fn new(port: PortId) -> Self {
Self {
port,
label: None,
description: None,
}
}
pub fn from(port: PortId, label: &'static str, description: Option<&'static str>) -> Self {
Self {
port,
label: Some(label),
description,
}
}
}
#[derive(Debug)]
pub struct Ports(pub(crate) Vec<Port>);
impl Ports {
pub fn new(ports: Vec<Port>) -> Self {
let length = ports.len();
let mut i = 0;
while i < length {
let mut j = i + 1;
while j < length {
if ports[i].port == ports[j].port {
panic!("Found ports with same id")
}
if ports[i].label.is_some() && ports[i].label == ports[j].label {
panic!("Found ports with same label")
}
j += 1;
}
i += 1;
}
Self(ports)
}
pub fn empty() -> Self {
Ports(vec![])
}
pub fn is_empty(&self) -> bool {
return self.0.is_empty();
}
pub fn contains(&self, port: PortId) -> bool {
self.0.iter().any(|p| p.port == port)
}
pub fn contains_label(&self, label: &str) -> bool {
self.0.iter().any(|p| p.label.is_some_and(|l| l == label))
}
}
pub trait Inputs {
fn inputs(&self) -> &Ports;
fn input(&self, label: &'static str) -> PortId;
}
pub trait Outputs {
fn outputs(&self) -> &Ports;
fn output(&self, label: &'static str) -> PortId;
}