Skip to main content

vvv_engine/protocol/intent/
mod.rs

1//! What the user wants done, as data.
2//!
3//! An intent is built by the CLI from arguments, by a TUI from a form, or by
4//! a client from JSON, and handed to the engine. Nothing here knows how the
5//! intent is fulfilled.
6
7use crate::capabilities::moves::{MoveIntent, MoveSymbolIntent};
8use crate::capabilities::rename::RenameIntent;
9use serde::{Deserialize, Serialize};
10
11pub use crate::batch::BatchIntent;
12pub use crate::rewrite::{RewriteIntent, RewriteOf};
13
14/// Any mutating request, as one value: what history records, what a summary
15/// renders, what a remote client sends.
16#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
17#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
18#[serde(tag = "command", rename_all = "snake_case")]
19pub enum Intent {
20    Rewrite(RewriteIntent),
21    Rename(RenameIntent),
22    Move(MoveIntent),
23    MoveSymbol(MoveSymbolIntent),
24    /// Several intents planned in sequence, each against the state the
25    /// previous one leaves, and applied as one.
26    Batch(BatchIntent),
27}
28
29impl Intent {
30    /// Add execution policy to a mutation description without executing it.
31    pub fn into_request(self, apply: bool) -> crate::Request {
32        match self {
33            Self::Rename(intent) => crate::Request::Rename { intent, apply },
34            Self::Move(intent) => crate::Request::Move { intent, apply },
35            Self::MoveSymbol(intent) => crate::Request::MoveSymbol { intent, apply },
36            Self::Rewrite(intent) => crate::Request::Rewrite { intent, apply },
37            Self::Batch(intent) => crate::Request::Batch { intent, apply },
38        }
39    }
40}
41
42impl From<BatchIntent> for Intent {
43    fn from(i: BatchIntent) -> Self {
44        Self::Batch(i)
45    }
46}
47
48impl From<RewriteIntent> for Intent {
49    fn from(i: RewriteIntent) -> Self {
50        Self::Rewrite(i)
51    }
52}
53
54impl From<RenameIntent> for Intent {
55    fn from(i: RenameIntent) -> Self {
56        Self::Rename(i)
57    }
58}
59
60impl From<MoveIntent> for Intent {
61    fn from(i: MoveIntent) -> Self {
62        Self::Move(i)
63    }
64}
65
66impl From<MoveSymbolIntent> for Intent {
67    fn from(i: MoveSymbolIntent) -> Self {
68        Self::MoveSymbol(i)
69    }
70}