1use crate::command::{Command, Context};
7use crate::{Batch, BatchIntent, EngineError, FileChange, FilePreview, Planned, Receipt, VfsError};
8
9impl Command for BatchIntent {
14 type Output = Planned<Batch>;
15
16 fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
17 let (engine, staging) = cx.staged();
18 let mut steps = Vec::new();
19 let mut notices = Vec::new();
20 let mut receipt: Option<Receipt> = None;
21 for intent in &self.intents {
22 let planned = engine.run(intent.clone())?;
23 notices.extend_from_slice(planned.notices());
24 for plan in planned.into_plans() {
25 let applied = plan.clone().apply(&staging)?;
26 receipt = Some(match receipt.take() {
27 Some(so_far) => so_far.then(applied),
28 None => applied,
29 });
30 steps.push(plan);
31 }
32 }
33 let preview = match &receipt {
36 Some(receipt) => receipt
37 .paths()
38 .map(|path| {
39 let mut final_path = path.to_path_buf();
40 for (from, to) in receipt.moves() {
41 if final_path == *from {
42 final_path = to.clone();
43 }
44 }
45 let before = cx.workspace.vfs().read(&cx.workspace.absolute(path))?;
46 let after = staging.vfs().read(&staging.absolute(&final_path))?;
47 Ok(FilePreview {
48 path: path.into(),
49 moved_to: (final_path != path).then_some(final_path.into()),
50 before,
51 after,
52 })
53 })
54 .collect::<Result<_, VfsError>>()?,
55 None => Vec::new(),
56 };
57 let files = FileChange::all(None, &preview);
58 Ok(Planned::new(
59 Batch {
60 intents: self.intents,
61 applied: false,
62 history_id: None,
63 notices,
64 files,
65 },
66 steps,
67 preview,
68 ))
69 }
70}