1use crate::command::{Command, Context};
10use crate::{
11 Answer, Apply, EngineError, HistoryQuery, Intent, Mutation, Planned, Request, UndoLast,
12};
13
14impl Command for Request {
15 type Output = Answer;
16
17 fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
18 Ok(match self {
19 Self::Search(q) => Answer::Search(q.run(cx)?),
20 Self::Outline(q) => Answer::Outline(q.run(cx)?),
21 Self::References(q) => Answer::References(q.run(cx)?),
22 Self::Where(q) => Answer::Where(q.run(cx)?),
23 Self::Deps(q) => Answer::Deps(q.run(cx)?),
24 Self::Explain(q) => Answer::Explain(q.run(cx)?),
25 Self::Surface(q) => Answer::Surface(q.run(cx)?),
26 Self::Impact(q) => Answer::Impact(q.run(cx)?),
27 Self::Dead(q) => Answer::Dead(q.run(cx)?),
28 Self::Imports(q) => Answer::Imports(q.run(cx)?),
29 Self::File(q) => Answer::File(q.run(cx)?),
30 Self::Rewrite { intent, apply } => mutate(Intent::Rewrite(intent), apply, cx)?,
31 Self::Rename { intent, apply } => mutate(Intent::Rename(intent), apply, cx)?,
32 Self::Move { intent, apply } => mutate(Intent::Move(intent), apply, cx)?,
33 Self::MoveSymbol { intent, apply } => mutate(Intent::MoveSymbol(intent), apply, cx)?,
34 Self::Batch { intent, apply } => mutate(Intent::Batch(intent), apply, cx)?,
35 Self::History => Answer::History(HistoryQuery.run(cx)?),
36 Self::Undo => Answer::Undo(UndoLast.run(cx)?),
37 })
38 }
39}
40
41fn mutate(intent: Intent, apply: bool, cx: &mut Context<'_>) -> Result<Answer, EngineError> {
44 let planned = intent.run(cx)?;
45 if apply {
46 Apply(planned).run(cx)
47 } else {
48 Ok(planned.into_inner())
49 }
50}
51
52impl Command for Intent {
56 type Output = Planned<Answer>;
57
58 fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
59 Ok(match self {
60 Self::Rewrite(i) => i.run(cx)?.map(Answer::Rewrite),
61 Self::Rename(i) => i.run(cx)?.map(Answer::Rename),
62 Self::Move(i) => i.run(cx)?.map(Answer::Move),
63 Self::MoveSymbol(i) => i.run(cx)?.map(Answer::MoveSymbol),
64 Self::Batch(i) => i.run(cx)?.map(Answer::Batch),
65 })
66 }
67}
68
69impl Mutation for Answer {
74 fn intent(&self) -> Intent {
75 match self {
76 Self::Rewrite(r) => r.intent(),
77 Self::Rename(r) => r.intent(),
78 Self::Move(r) => r.intent(),
79 Self::MoveSymbol(r) => r.intent(),
80 Self::Batch(b) => b.intent(),
81 _ => unreachable!("a query answer is never written"),
82 }
83 }
84
85 fn applied(&mut self, id: u64) {
86 match self {
87 Self::Rewrite(r) => r.applied(id),
88 Self::Rename(r) => r.applied(id),
89 Self::Move(r) => r.applied(id),
90 Self::MoveSymbol(r) => r.applied(id),
91 Self::Batch(b) => b.applied(id),
92 _ => {}
93 }
94 }
95}