Skip to main content

greplm_core/
proto.rs

1//! Wire protocol shared by the daemon and its clients.
2//!
3//! Messages are newline-delimited JSON over a Unix domain socket. Each request
4//! is one JSON object on a line; each response is one JSON object on a line.
5
6use serde::{Deserialize, Serialize};
7
8use crate::search::{SearchQuery, SymbolQuery};
9
10/// Default socket path relative to a project's `.greplm` directory.
11pub const SOCKET_NAME: &str = "greplmd.sock";
12
13/// A request from a client to the daemon.
14#[derive(Debug, Clone, Serialize, Deserialize)]
15#[serde(tag = "op", rename_all = "snake_case")]
16pub enum Request {
17    Ping,
18    Status,
19    Summary,
20    Reindex {
21        force: bool,
22    },
23    Search(SearchQuery),
24    Symbols(SymbolQuery),
25    Refs {
26        name: String,
27        limit: usize,
28        offset: usize,
29    },
30    /// Resolved references (definitions + call sites + imports) from the
31    /// structural reference index.
32    RefsResolved {
33        name: String,
34        limit: usize,
35        offset: usize,
36    },
37    /// Call sites that target a symbol (who calls it).
38    Callers {
39        name: String,
40        limit: usize,
41        offset: usize,
42    },
43    /// Call sites inside a symbol's body (what it calls).
44    Callees {
45        name: String,
46        limit: usize,
47        offset: usize,
48    },
49    /// Symbols transitively affected by changing a symbol (reverse call graph).
50    BlastRadius {
51        name: String,
52        depth: u32,
53        limit: usize,
54    },
55    /// Typed go-to-definition for the identifier at a source position.
56    Definition {
57        file: String,
58        line: u32,
59        col: u32,
60    },
61    /// Resolved references for the identifier at a source position.
62    ReferencesAt {
63        file: String,
64        line: u32,
65        col: u32,
66    },
67    /// Structural (AST) search by tree-sitter query or meta-variable pattern.
68    Structural {
69        pattern: String,
70        lang: String,
71        limit: usize,
72        offset: usize,
73    },
74    /// Build a token-budgeted context pack for a task.
75    ContextPack {
76        task: String,
77        budget: u64,
78    },
79    /// Git blame for a single line.
80    Blame {
81        file: String,
82        line: u32,
83    },
84    /// Commit history of a symbol's definition.
85    History {
86        name: String,
87        limit: usize,
88    },
89    /// Files (with symbols) changed since a revision.
90    ChangedSince {
91        rev: String,
92    },
93    Outline {
94        file: String,
95    },
96    Snippet {
97        file: String,
98        start: u32,
99        end: u32,
100        context: u32,
101    },
102}
103
104/// A response from the daemon to a client.
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct Response {
107    pub ok: bool,
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub result: Option<serde_json::Value>,
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub error: Option<String>,
112}
113
114impl Response {
115    pub fn ok(value: serde_json::Value) -> Self {
116        Response {
117            ok: true,
118            result: Some(value),
119            error: None,
120        }
121    }
122
123    pub fn err(message: impl Into<String>) -> Self {
124        Response {
125            ok: false,
126            result: None,
127            error: Some(message.into()),
128        }
129    }
130}