use std::collections::BTreeMap;
use astrs_manifest::{Pattern, Urn};
use serde::{Deserialize, Serialize};
use crate::ids::{MachineId, NodeId, PortName};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OutputPort {
pub type_urn: Option<Urn>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InputPort {
pub type_urn: Option<Urn>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GraphNode {
pub id: NodeId,
pub outputs: BTreeMap<PortName, OutputPort>,
pub inputs: BTreeMap<PortName, InputPort>,
pub pattern: Option<Pattern>,
pub machine: MachineId,
pub spawns_process: bool,
}
impl GraphNode {
#[must_use]
pub fn output_type(&self, name: &PortName) -> Option<&Urn> {
self.outputs.get(name).and_then(|p| p.type_urn.as_ref())
}
#[must_use]
pub fn input_type(&self, name: &PortName) -> Option<&Urn> {
self.inputs.get(name).and_then(|p| p.type_urn.as_ref())
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
fn sample_node() -> GraphNode {
let mut outputs = BTreeMap::new();
outputs.insert(
PortName::new("frames"),
OutputPort {
type_urn: Some(Urn::new("std/media/v1/Image[pixel=rgb8]")),
},
);
let mut inputs = BTreeMap::new();
inputs.insert(PortName::new("tick"), InputPort { type_urn: None });
GraphNode {
id: NodeId::new("camera"),
outputs,
inputs,
pattern: None,
machine: MachineId::CoordinatorLocal,
spawns_process: true,
}
}
#[test]
fn output_type_returns_declared_type() {
let node = sample_node();
assert_eq!(
node.output_type(&PortName::new("frames")).map(Urn::as_str),
Some("std/media/v1/Image[pixel=rgb8]")
);
}
#[test]
fn output_type_is_none_for_unknown_port() {
let node = sample_node();
assert_eq!(node.output_type(&PortName::new("missing")), None);
}
#[test]
fn input_type_is_none_when_untyped() {
let node = sample_node();
assert_eq!(node.input_type(&PortName::new("tick")), None);
}
#[test]
fn graph_node_equality_is_structural() {
assert_eq!(sample_node(), sample_node());
let mut other = sample_node();
other.pattern = Some(Pattern::ServiceServer);
assert_ne!(sample_node(), other);
}
}