Skip to main content

vvv_engine/protocol/
mod.rs

1//! The JSON contract. Anything printed by `vvv --json` is one of these types,
2//! so a client can rely on the shape independently of the CLI's human output.
3//!
4//! Data only: nothing here reads a file or takes a `Workspace`. A client that
5//! only speaks JSON depends on this crate alone.
6
7mod answer;
8mod diff;
9pub mod display;
10mod failure;
11mod intent;
12mod notice;
13mod question;
14mod reach;
15mod references;
16mod request;
17mod respelling;
18mod result;
19mod search;
20mod selection;
21mod template;
22pub mod vocabulary;
23
24use serde::{Deserialize, Serialize};
25
26pub use answer::{
27    Consumer, Dead, Dep, Deps, Explanation, Exposed, File, Impact, ImportSite, Importer,
28    ImportsReport, Locations, Outline, OutlineItem, Placed, References, Site, Surface,
29    Unreferenced,
30};
31pub use diff::UnifiedDiff;
32pub use failure::{ErrorCode, Failure};
33pub use intent::{
34    BatchIntent, Intent, MoveIntent, MoveSymbolIntent, RenameIntent, RewriteIntent, RewriteOf,
35};
36pub use notice::{Notice, NoticeKind};
37pub use question::{
38    DeadQuery, DepsQuery, ExplainQuery, FileQuery, HistoryQuery, ImpactQuery, ImportsQuery,
39    OutlineQuery, SurfaceQuery, UndoLast, WhereQuery,
40};
41pub use reach::Reach;
42pub use references::ReferencesQuery;
43pub use request::{Answer, Call, Reply, Request};
44pub use respelling::Respelling;
45pub use result::{
46    Batch, FileChange, History, HistoryEntry, Move, MoveSymbol, Mutation, Rename, Rewrite, Search,
47    Undo,
48};
49pub use search::{Confidence, Match, MatchId, Occurrence, Reason, Skipped};
50pub use selection::{Selection, SelectionError};
51pub use template::{Template, TemplateError};
52
53/// The shape of this contract. Bumped when a field changes meaning or goes
54/// away; adding fields does not bump it.
55pub const SCHEMA: u32 = 1;
56
57/// Top-level envelope of every `--json` response.
58#[derive(Debug, Clone, Serialize, Deserialize)]
59#[serde(tag = "status", rename_all = "lowercase")]
60pub enum Response<T> {
61    Ok {
62        schema: u32,
63        result: T,
64    },
65    Error {
66        schema: u32,
67        #[serde(flatten)]
68        failure: Failure,
69    },
70}
71
72impl<T> Response<T> {
73    pub fn ok(result: T) -> Self {
74        Self::Ok {
75            schema: SCHEMA,
76            result,
77        }
78    }
79
80    pub fn error(failure: Failure) -> Self {
81        Self::Error {
82            schema: SCHEMA,
83            failure,
84        }
85    }
86}