Skip to main content

vvv_engine/
request.rs

1//! A [`Request`] is a command like any other: it runs the command it names
2//! against the same context and answers with the [`Answer`] of that name.
3//! A mutation with `apply` runs the intent, then [`Apply`] on what it
4//! planned — one request, one undo — as the CLI's `--apply` does.
5//!
6//! An [`Intent`] is a command too: it plans to the wire [`Answer`], so a
7//! caller that holds one never has to match its variants.
8
9use 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
41/// Plan a mutating intent and, when asked, write it: the one place `--apply`,
42/// `serve`'s `apply` and a batch step agree on what applying means.
43fn 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
52/// Plan any intent to the answer it will print. A caller that holds an
53/// `Intent` — a batch step, the picker, `serve` — asks once, without
54/// matching the variant.
55impl 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
69/// A written answer records the intent it wrote and the history entry.
70///
71/// Only a mutating answer reaches [`Apply`]: a `Planned<Answer>` is built
72/// from a mutating intent, so the query variants are unreachable here.
73impl 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}