Skip to main content

vvv_engine/protocol/
request.rs

1//! Every request as one value, and every answer as one: what a client sends
2//! down a wire and reads back. `{"command": "rename", "name": "Config",
3//! "to": "Settings", "apply": true}` is a [`Request`]; the reply's `result`
4//! is the [`Answer`] of the same name.
5
6use serde::{Deserialize, Serialize};
7use vvv_core::Query;
8
9use super::{
10    Batch, BatchIntent, Dead, DeadQuery, Deps, DepsQuery, ExplainQuery, Explanation, File,
11    FileQuery, History, Impact, ImpactQuery, ImportsQuery, ImportsReport, Locations, Move,
12    MoveIntent, MoveSymbol, MoveSymbolIntent, Notice, Outline, OutlineQuery, References,
13    ReferencesQuery, Rename, RenameIntent, Response, Rewrite, RewriteIntent, Search, Surface,
14    SurfaceQuery, Undo, WhereQuery,
15};
16
17/// One request, tagged by `command`. A mutation carries the same fields as
18/// its [`Intent`](super::Intent) plus `apply`: false previews, true writes
19/// and records one undo — exactly what the CLI's `--apply` does.
20#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
21#[serde(tag = "command", rename_all = "snake_case")]
22pub enum Request {
23    Search(Query),
24    Outline(OutlineQuery),
25    References(ReferencesQuery),
26    Where(WhereQuery),
27    Deps(DepsQuery),
28    Explain(ExplainQuery),
29    Surface(SurfaceQuery),
30    Impact(ImpactQuery),
31    Dead(DeadQuery),
32    Imports(ImportsQuery),
33    File(FileQuery),
34    Rewrite {
35        #[serde(flatten)]
36        intent: RewriteIntent,
37        #[serde(default)]
38        apply: bool,
39    },
40    Rename {
41        #[serde(flatten)]
42        intent: RenameIntent,
43        #[serde(default)]
44        apply: bool,
45    },
46    Move {
47        #[serde(flatten)]
48        intent: MoveIntent,
49        #[serde(default)]
50        apply: bool,
51    },
52    MoveSymbol {
53        #[serde(flatten)]
54        intent: MoveSymbolIntent,
55        #[serde(default)]
56        apply: bool,
57    },
58    Batch {
59        #[serde(flatten)]
60        intent: BatchIntent,
61        #[serde(default)]
62        apply: bool,
63    },
64    History,
65    Undo,
66}
67
68/// The answer to a [`Request`]: the result type of the command asked. On
69/// the wire it is that type's shape alone — a client knows what it asked —
70/// so it serialises without a tag and is read back by the command's type.
71#[derive(Debug, Clone, Serialize)]
72#[serde(untagged)]
73// Built once and serialised once: the largest result's size is not a cost
74// worth a `Box` in every client's match.
75#[allow(clippy::large_enum_variant)]
76pub enum Answer {
77    Search(Search),
78    Outline(Outline),
79    References(References),
80    Where(Locations),
81    Deps(Deps),
82    Explain(Explanation),
83    Surface(Surface),
84    Impact(Impact),
85    Dead(Dead),
86    Imports(ImportsReport),
87    File(File),
88    Rewrite(Rewrite),
89    Rename(Rename),
90    Move(Move),
91    MoveSymbol(MoveSymbol),
92    Batch(Batch),
93    History(History),
94    Undo(Undo),
95}
96
97impl Answer {
98    /// The notices a mutating answer leaves to a person, if any.
99    pub fn notices(&self) -> &[Notice] {
100        match self {
101            Self::Move(m) => &m.notices,
102            Self::MoveSymbol(m) => &m.notices,
103            Self::Batch(b) => &b.notices,
104            _ => &[],
105        }
106    }
107
108    /// The history entry a written answer recorded, if any.
109    pub fn history_id(&self) -> Option<u64> {
110        match self {
111            Self::Rewrite(r) => r.history_id,
112            Self::Rename(r) => r.history_id,
113            Self::Move(r) => r.history_id,
114            Self::MoveSymbol(r) => r.history_id,
115            Self::Batch(b) => b.history_id,
116            _ => None,
117        }
118    }
119}
120
121/// A request on a session's wire (`vvv serve`): the request with whatever
122/// `id` the caller chose, echoed on the [`Reply`].
123#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
124pub struct Call {
125    #[serde(default, skip_serializing_if = "Option::is_none")]
126    pub id: Option<serde_json::Value>,
127    #[serde(flatten)]
128    pub request: Request,
129}
130
131/// The reply to a [`Call`]: its `id`, then the usual envelope.
132#[derive(Debug, Clone, Serialize, Deserialize)]
133pub struct Reply<T> {
134    #[serde(default, skip_serializing_if = "Option::is_none")]
135    pub id: Option<serde_json::Value>,
136    #[serde(flatten)]
137    pub response: Response<T>,
138}