use crate::command::{Command, Context};
use crate::{Batch, BatchIntent, EngineError, FileChange, FilePreview, Planned, Receipt, VfsError};
impl Command for BatchIntent {
type Output = Planned<Batch>;
fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
let (engine, staging) = cx.staged();
let mut steps = Vec::new();
let mut notices = Vec::new();
let mut receipt: Option<Receipt> = None;
for intent in &self.intents {
let planned = engine.run(intent.clone())?;
notices.extend_from_slice(planned.notices());
for plan in planned.into_plans() {
let applied = plan.clone().apply(&staging)?;
receipt = Some(match receipt.take() {
Some(so_far) => so_far.then(applied),
None => applied,
});
steps.push(plan);
}
}
let preview = match &receipt {
Some(receipt) => receipt
.paths()
.map(|path| {
let mut final_path = path.to_path_buf();
for (from, to) in receipt.moves() {
if final_path == *from {
final_path = to.clone();
}
}
let before = cx.workspace.vfs().read(&cx.workspace.absolute(path))?;
let after = staging.vfs().read(&staging.absolute(&final_path))?;
Ok(FilePreview {
path: path.into(),
moved_to: (final_path != path).then_some(final_path.into()),
before,
after,
})
})
.collect::<Result<_, VfsError>>()?,
None => Vec::new(),
};
let files = FileChange::all(None, &preview);
Ok(Planned::new(
Batch {
intents: self.intents,
applied: false,
history_id: None,
notices,
files,
},
steps,
preview,
))
}
}