use crate::error::Result;
use crate::http::HttpClient;
use crate::types::api::{FormatterInfo, LspServerStatus, OpenApiDoc};
use reqwest::Method;
use serde::{Deserialize, Serialize};
#[derive(Clone)]
pub struct MiscApi {
http: HttpClient,
}
impl MiscApi {
pub fn new(http: HttpClient) -> Self {
Self { http }
}
pub async fn path(&self) -> Result<PathInfo> {
self.http.request_json(Method::GET, "/path", None).await
}
pub async fn vcs(&self) -> Result<VcsInfo> {
self.http.request_json(Method::GET, "/vcs", None).await
}
pub async fn dispose(&self) -> Result<()> {
self.http
.request_empty(
Method::POST,
"/instance/dispose",
Some(serde_json::json!({})),
)
.await
}
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
}
pub async fn lsp(&self) -> Result<Vec<LspServerStatus>> {
self.http.request_json(Method::GET, "/lsp", None).await
}
pub async fn formatter(&self) -> Result<Vec<FormatterInfo>> {
self.http
.request_json(Method::GET, "/formatter", None)
.await
}
pub async fn health(&self) -> Result<HealthInfo> {
self.http
.request_json(Method::GET, "/global/health", None)
.await
}
pub async fn global_dispose(&self) -> Result<()> {
self.http
.request_empty(Method::POST, "/global/dispose", Some(serde_json::json!({})))
.await
}
pub async fn doc(&self) -> Result<OpenApiDoc> {
self.http.request_json(Method::GET, "/doc", None).await
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PathInfo {
pub directory: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project_root: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VcsInfo {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub branch: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub remote: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LogEntry {
pub level: String,
pub message: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub data: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HealthInfo {
pub healthy: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
}