use frp_plexus::{PortId, TypeSig};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PortDirection {
Input,
Output,
}
impl std::fmt::Display for PortDirection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PortDirection::Input => write!(f, "Input"),
PortDirection::Output => write!(f, "Output"),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Port {
pub id: PortId,
pub name: String,
pub direction: PortDirection,
pub type_sig: TypeSig,
}
impl Port {
pub fn new_input(id: PortId, name: impl Into<String>, type_sig: TypeSig) -> Self {
Self { id, name: name.into(), direction: PortDirection::Input, type_sig }
}
pub fn new_output(id: PortId, name: impl Into<String>, type_sig: TypeSig) -> Self {
Self { id, name: name.into(), direction: PortDirection::Output, type_sig }
}
pub fn is_input(&self) -> bool {
self.direction == PortDirection::Input
}
pub fn is_output(&self) -> bool {
self.direction == PortDirection::Output
}
}
impl std::fmt::Display for Port {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}({})", self.direction, self.name, self.type_sig)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_input_port() {
let p = Port::new_input(PortId::new(1), "x", TypeSig::Int);
assert!(p.is_input());
assert!(!p.is_output());
assert_eq!(p.name, "x");
}
#[test]
fn new_output_port() {
let p = Port::new_output(PortId::new(2), "y", TypeSig::Float);
assert!(p.is_output());
assert!(!p.is_input());
}
}