use std::path::PathBuf;
use serde::Deserialize;
use serde_json::value::RawValue;
use super::RequestError;
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum DiagnoseDocument {
Project,
Spec,
Template,
PluginManifest,
}
impl DiagnoseDocument {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Project => "project",
Self::Spec => "spec",
Self::Template => "template",
Self::PluginManifest => "plugin_manifest",
}
}
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DiagnoseRequest {
pub protocol_version: u32,
pub request_id: String,
#[serde(default, rename = "operation")]
_operation: serde::de::IgnoredAny,
#[serde(default, rename = "attachments")]
_attachments: serde::de::IgnoredAny,
pub document: DiagnoseDocument,
pub text: String,
#[serde(default)]
pub options: Option<Box<RawValue>>,
#[serde(default)]
pub fonts: Vec<PathBuf>,
}
impl DiagnoseRequest {
#[must_use]
pub fn options_text(&self) -> &str {
self.options.as_deref().map_or("", RawValue::get)
}
pub fn check(&self) -> Result<(), RequestError> {
if !self.fonts.is_empty() && self.document != DiagnoseDocument::Spec {
return Err(RequestError::invalid_request(format!(
"fonts are read for a spec only, not for a {}",
self.document.name()
)));
}
Ok(())
}
}