use crate::errors::PathPlannerError;
use super::GraphNodeMap;
pub(crate) fn shortest_path<N, C>(node_map: &GraphNodeMap<N, C>, goal_index: usize) -> Result<Vec<N>, PathPlannerError>
where
N: Clone,
{
let mut path = Vec::new();
let mut current_index = goal_index;
while current_index != usize::MAX {
if let Some((node, &(parent_index, _))) = node_map.get_index(current_index) {
path.push(node.clone());
current_index = parent_index;
} else {
return Err(PathPlannerError::NoPathFound);
}
}
path.reverse();
if path.is_empty() {
return Err(PathPlannerError::NoPathFound);
}
Ok(path)
}