1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Miscellaneous API endpoints for `OpenCode`.
//!
//! Includes: VCS, path, instance, log, LSP, formatter, global endpoints.
use crate::error::Result;
use crate::http::HttpClient;
use crate::types::api::{FormatterInfo, LspServerStatus, OpenApiDoc};
use reqwest::Method;
use serde::{Deserialize, Serialize};
/// Misc API client.
#[derive(Clone)]
pub struct MiscApi {
http: HttpClient,
}
impl MiscApi {
/// Create a new Misc API client.
pub fn new(http: HttpClient) -> Self {
Self { http }
}
/// Get current path info.
///
/// # Errors
///
/// Returns an error if the request fails.
pub async fn path(&self) -> Result<PathInfo> {
self.http.request_json(Method::GET, "/path", None).await
}
/// Get VCS info.
///
/// # Errors
///
/// Returns an error if the request fails.
pub async fn vcs(&self) -> Result<VcsInfo> {
self.http.request_json(Method::GET, "/vcs", None).await
}
/// Dispose instance.
///
/// # Errors
///
/// Returns an error if the request fails.
pub async fn dispose(&self) -> Result<()> {
self.http
.request_empty(
Method::POST,
"/instance/dispose",
Some(serde_json::json!({})),
)
.await
}
/// Write log entry.
///
/// # Errors
///
/// Returns an error if the request fails.
pub async fn log(&self, entry: &LogEntry) -> Result<()> {
let body = serde_json::to_value(entry)?;
self.http
.request_empty(Method::POST, "/log", Some(body))
.await
}
/// Get LSP server status for all configured LSP servers.
///
/// # Errors
///
/// Returns an error if the request fails.
pub async fn lsp(&self) -> Result<Vec<LspServerStatus>> {
self.http.request_json(Method::GET, "/lsp", None).await
}
/// Get formatter status for all configured formatters.
///
/// # Errors
///
/// Returns an error if the request fails.
pub async fn formatter(&self) -> Result<Vec<FormatterInfo>> {
self.http
.request_json(Method::GET, "/formatter", None)
.await
}
/// Get global health.
///
/// # Errors
///
/// Returns an error if the request fails.
pub async fn health(&self) -> Result<HealthInfo> {
self.http
.request_json(Method::GET, "/global/health", None)
.await
}
/// Dispose global.
///
/// # Errors
///
/// Returns an error if the request fails.
pub async fn global_dispose(&self) -> Result<bool> {
self.http
.request_json::<bool>(Method::POST, "/global/dispose", Some(serde_json::json!({})))
.await
}
/// Get `OpenAPI` spec.
///
/// # Errors
///
/// Returns an error if the request fails.
pub async fn doc(&self) -> Result<OpenApiDoc> {
self.http.request_json(Method::GET, "/doc", None).await
}
}
/// Path information.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PathInfo {
/// Current directory.
pub directory: String,
/// Project root.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project_root: Option<String>,
}
/// VCS information.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VcsInfo {
/// VCS type (git, etc.).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
/// Current branch.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub branch: Option<String>,
/// Remote URL.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub remote: Option<String>,
}
/// Log entry.
// TODO(3): Consider using enum for `level` field (Debug/Info/Warn/Error) with #[serde(other)] for forward-compat
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LogEntry {
/// Log level.
pub level: String,
/// Log message.
pub message: String,
/// Additional data.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub data: Option<serde_json::Value>,
}
/// Health information.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HealthInfo {
/// Whether healthy.
pub healthy: bool,
/// Server version.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
}