use crate::command::{Command, Context};
use crate::{
Answer, Apply, EngineError, HistoryQuery, Intent, Mutation, Planned, Request, UndoLast,
};
impl Command for Request {
type Output = Answer;
fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
Ok(match self {
Self::Search(q) => Answer::Search(q.run(cx)?),
Self::Outline(q) => Answer::Outline(q.run(cx)?),
Self::References(q) => Answer::References(q.run(cx)?),
Self::Where(q) => Answer::Where(q.run(cx)?),
Self::Deps(q) => Answer::Deps(q.run(cx)?),
Self::Explain(q) => Answer::Explain(q.run(cx)?),
Self::Surface(q) => Answer::Surface(q.run(cx)?),
Self::Impact(q) => Answer::Impact(q.run(cx)?),
Self::Dead(q) => Answer::Dead(q.run(cx)?),
Self::Imports(q) => Answer::Imports(q.run(cx)?),
Self::File(q) => Answer::File(q.run(cx)?),
Self::Rewrite { intent, apply } => mutate(Intent::Rewrite(intent), apply, cx)?,
Self::Rename { intent, apply } => mutate(Intent::Rename(intent), apply, cx)?,
Self::Move { intent, apply } => mutate(Intent::Move(intent), apply, cx)?,
Self::MoveSymbol { intent, apply } => mutate(Intent::MoveSymbol(intent), apply, cx)?,
Self::Batch { intent, apply } => mutate(Intent::Batch(intent), apply, cx)?,
Self::History => Answer::History(HistoryQuery.run(cx)?),
Self::Undo => Answer::Undo(UndoLast.run(cx)?),
})
}
}
fn mutate(intent: Intent, apply: bool, cx: &mut Context<'_>) -> Result<Answer, EngineError> {
let planned = intent.run(cx)?;
if apply {
Apply(planned).run(cx)
} else {
Ok(planned.into_inner())
}
}
impl Command for Intent {
type Output = Planned<Answer>;
fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
Ok(match self {
Self::Rewrite(i) => i.run(cx)?.map(Answer::Rewrite),
Self::Rename(i) => i.run(cx)?.map(Answer::Rename),
Self::Move(i) => i.run(cx)?.map(Answer::Move),
Self::MoveSymbol(i) => i.run(cx)?.map(Answer::MoveSymbol),
Self::Batch(i) => i.run(cx)?.map(Answer::Batch),
})
}
}
impl Mutation for Answer {
fn intent(&self) -> Intent {
match self {
Self::Rewrite(r) => r.intent(),
Self::Rename(r) => r.intent(),
Self::Move(r) => r.intent(),
Self::MoveSymbol(r) => r.intent(),
Self::Batch(b) => b.intent(),
_ => unreachable!("a query answer is never written"),
}
}
fn applied(&mut self, id: u64) {
match self {
Self::Rewrite(r) => r.applied(id),
Self::Rename(r) => r.applied(id),
Self::Move(r) => r.applied(id),
Self::MoveSymbol(r) => r.applied(id),
Self::Batch(b) => b.applied(id),
_ => {}
}
}
}