use crate::graph::LevelGraph;
use super::closing::ChainFailure;
use super::routed::Routed;
use super::{ChainPosition, PipelineContext};
pub(super) fn close_graph<'a, F: Fn() -> u64 + Copy>(
ctx: &mut PipelineContext<F>,
graph: &LevelGraph<'a>,
routed: &mut Routed<'a>,
mut failure: ChainFailure,
failed_end: usize,
file_offset: usize,
) -> ChainFailure {
let position = ChainPosition::whole(graph.order.len());
routed.hand_over(
graph.order[failed_end - 1].id.as_str(),
failure.take_files(),
);
for (index, node) in graph.order.iter().enumerate().skip(failed_end) {
let mut files = routed.take(&graph.inputs[node.id.as_str()]);
if node.always {
failure = failure.close_with(ctx, node, files, position.offset(index), file_offset);
files = failure.take_files();
}
routed.hand_over(node.id.as_str(), files);
}
let output = routed.take(&graph.outputs);
failure.handing_over(output)
}