1use crate::plan::Preview;
7use crate::protocol::vocabulary::IntentLine;
8use crate::report::{Block, Document, MoveCounts};
9use serde::{Deserialize, Serialize};
10
11use crate::{
12 EngineError, FileChange, FilePreview, Intent, Mutation, MutationAnswer, Notice, Planned,
13 Receipt, VfsError,
14};
15
16#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
20pub struct BatchIntent {
21 pub intents: Vec<Intent>,
22}
23
24impl BatchIntent {
25 pub fn new(intents: impl IntoIterator<Item = Intent>) -> Self {
26 Self {
27 intents: intents.into_iter().collect(),
28 }
29 }
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
34#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
35pub struct Batch {
36 pub intents: Vec<Intent>,
37 #[serde(flatten)]
39 pub state: crate::MutationState,
40 #[serde(default, skip_serializing_if = "Vec::is_empty")]
41 pub notices: Vec<Notice>,
42 pub files: Vec<FileChange>,
46}
47
48impl Mutation for Batch {
49 fn into_mutation(self) -> MutationAnswer {
50 MutationAnswer::Batch(self)
51 }
52 fn applied(&mut self, id: u64) {
53 self.state = crate::MutationState::Applied { history_id: id };
54 }
55}
56
57impl BatchIntent {
62 pub fn plan(self, engine: &crate::Engine) -> Result<Planned<Batch>, EngineError> {
64 let _operation = engine.operation();
65 self.plan_in(engine)
66 }
67
68 pub(crate) fn plan_in(self, engine: &crate::Engine) -> Result<Planned<Batch>, EngineError> {
69 let workspace = engine.workspace();
70 let (engine, staging) = engine.staged();
71 let mut steps = Vec::new();
72 let mut notices = Vec::new();
73 let mut receipt: Option<Receipt> = None;
74 for intent in &self.intents {
75 let planned = engine
76 .run(intent.clone().into_request(false))?
77 .into_preview()?;
78 notices.extend_from_slice(planned.notices());
79 for plan in planned.into_plans() {
80 let applied = plan.clone().apply(&staging)?;
81 receipt = Some(match receipt.take() {
82 Some(so_far) => so_far.then(applied),
83 None => applied,
84 });
85 steps.push(plan);
86 }
87 }
88 let preview = match &receipt {
91 Some(receipt) => receipt
92 .paths()
93 .map(|path| {
94 let mut final_path = path.to_path_buf();
95 for (from, to) in receipt.moves() {
96 if final_path == *from {
97 final_path = to.clone();
98 }
99 }
100 let before = workspace.vfs().read(&workspace.absolute(path))?;
101 let after = staging.vfs().read(&staging.absolute(&final_path))?;
102 Ok(FilePreview {
103 path: path.into(),
104 moved_to: (final_path != path).then_some(final_path.into()),
105 before,
106 after,
107 })
108 })
109 .collect::<Result<_, VfsError>>()?,
110 None => Vec::new(),
111 };
112 let preview = Preview { files: preview };
113 let files = preview.file_changes();
114 Ok(Planned::new(
115 Intent::Batch(self.clone()),
116 Batch {
117 intents: self.intents,
118 state: crate::MutationState::Preview,
119 notices,
120 files,
121 },
122 steps,
123 preview.files,
124 ))
125 }
126}
127
128impl Document {
129 pub(crate) fn batch(result: &Batch) -> Self {
130 let mut report = Self::new();
131 report.title(IntentLine(&Intent::Batch(BatchIntent {
132 intents: result.intents.clone(),
133 })));
134 report.block_body(Block::Batch(result.intents.clone()));
135 report.block_body(Block::Blank);
136 let structural = report.moved(result.state, &result.files, &[], &result.notices);
138 report.moved_summary(
139 result.state,
140 MoveCounts {
141 respellings: 0,
142 structural,
143 notices: result.notices.len(),
144 files: result.files.len(),
145 },
146 );
147 report
148 }
149}