use std::fmt;
use astrs_manifest::Deploy;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct NodeId(String);
impl NodeId {
#[must_use]
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for NodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<String> for NodeId {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for NodeId {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl AsRef<str> for NodeId {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PortName(String);
impl PortName {
#[must_use]
pub fn new(name: impl Into<String>) -> Self {
Self(name.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for PortName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<String> for PortName {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for PortName {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl AsRef<str> for PortName {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum MachineId {
CoordinatorLocal,
Named(String),
}
impl MachineId {
#[must_use]
pub fn resolve(graph_deploy: Option<&Deploy>, node_deploy: Option<&Deploy>) -> Self {
let machine = node_deploy
.and_then(|d| d.machine.as_deref())
.or_else(|| graph_deploy.and_then(|d| d.machine.as_deref()));
match machine {
Some(name) => Self::Named(name.to_string()),
None => Self::CoordinatorLocal,
}
}
#[must_use]
pub fn is_coordinator_local(&self) -> bool {
matches!(self, Self::CoordinatorLocal)
}
}
impl fmt::Display for MachineId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CoordinatorLocal => f.write_str("coordinator"),
Self::Named(name) => f.write_str(name),
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn node_id_displays_as_its_string() {
let id = NodeId::new("camera");
assert_eq!(id.to_string(), "camera");
assert_eq!(id.as_str(), "camera");
}
#[test]
fn node_id_orders_lexicographically() {
assert!(NodeId::new("a") < NodeId::new("b"));
}
#[test]
fn port_name_wraps_and_displays() {
let name = PortName::new("frames");
assert_eq!(name.as_str(), "frames");
assert_eq!(name.to_string(), "frames");
}
#[test]
fn machine_id_resolves_to_coordinator_local_when_unset() {
assert_eq!(MachineId::resolve(None, None), MachineId::CoordinatorLocal);
assert!(MachineId::resolve(None, None).is_coordinator_local());
}
#[test]
fn machine_id_resolves_graph_default_when_node_unset() {
let graph = Deploy {
machine: Some("robot-1".to_string()),
..Deploy::default()
};
assert_eq!(
MachineId::resolve(Some(&graph), None),
MachineId::Named("robot-1".to_string())
);
}
#[test]
fn machine_id_node_override_wins() {
let graph = Deploy {
machine: Some("robot-1".to_string()),
..Deploy::default()
};
let node = Deploy {
machine: Some("robot-2".to_string()),
..Deploy::default()
};
assert_eq!(
MachineId::resolve(Some(&graph), Some(&node)),
MachineId::Named("robot-2".to_string())
);
}
#[test]
fn machine_id_node_deploy_present_but_machine_unset_falls_back_to_graph() {
let graph = Deploy {
machine: Some("robot-1".to_string()),
..Deploy::default()
};
let node = Deploy {
working_dir: Some("/opt/node".to_string()),
..Deploy::default()
};
assert_eq!(
MachineId::resolve(Some(&graph), Some(&node)),
MachineId::Named("robot-1".to_string())
);
}
#[test]
fn machine_id_display_matches_variant() {
assert_eq!(MachineId::CoordinatorLocal.to_string(), "coordinator");
assert_eq!(
MachineId::Named("robot-1".to_string()).to_string(),
"robot-1"
);
}
}