use crate::{
dto::cascade::{
StateSnapshotInput, TopologyChildren, TopologyDirectChild as TopologyDirectChildDto,
TopologyPathNode as TopologyPathNodeDto, TopologySnapshotInput,
},
workflow::cascade::snapshot::{
StateSnapshot, TopologyDirectChild, TopologyPathNode, TopologySnapshot,
},
};
pub struct StateSnapshotAdapter;
impl StateSnapshotAdapter {
#[must_use]
pub fn to_input(snapshot: &StateSnapshot) -> StateSnapshotInput {
StateSnapshotInput {
fleet_state: snapshot.fleet_state,
fleet_directory: snapshot.fleet_directory.clone(),
subnet_directory: snapshot.subnet_directory.clone(),
}
}
#[must_use]
pub fn from_input(view: StateSnapshotInput) -> StateSnapshot {
StateSnapshot::from(view)
}
}
pub struct TopologySnapshotAdapter;
impl TopologySnapshotAdapter {
#[must_use]
pub fn to_input(snapshot: &TopologySnapshot) -> TopologySnapshotInput {
let mut children_map = snapshot.children_map.iter().collect::<Vec<_>>();
children_map.sort_by_key(|(pid, _)| pid.as_slice().to_vec());
TopologySnapshotInput {
parents: snapshot
.parents
.iter()
.map(|p| TopologyPathNodeDto {
pid: p.pid,
role: p.role.clone(),
parent_pid: p.parent_pid,
})
.collect(),
children_map: children_map
.into_iter()
.map(|(pid, children)| TopologyChildren {
parent_pid: *pid,
children: {
let mut children = children.clone();
children.sort_by_key(|child| child.pid.as_slice().to_vec());
children
.iter()
.map(|c| TopologyDirectChildDto {
pid: c.pid,
role: c.role.clone(),
})
.collect()
},
})
.collect(),
}
}
#[must_use]
pub fn from_input(view: TopologySnapshotInput) -> TopologySnapshot {
TopologySnapshot {
parents: view
.parents
.into_iter()
.map(|p| TopologyPathNode {
pid: p.pid,
role: p.role,
parent_pid: p.parent_pid,
})
.collect(),
children_map: view
.children_map
.into_iter()
.map(|entry| {
let mapped = entry
.children
.into_iter()
.map(|child| TopologyDirectChild {
pid: child.pid,
role: child.role,
})
.collect();
(entry.parent_pid, mapped)
})
.collect(),
}
}
}