alpha2-host-core 0.10.0

FFmpeg-free native host logic for alpha2: the policies, audio timelines, cursor rules, providers, plugin runtime, protocol frames and request parsing native hosts share, and the decode, export and still pipeline the mobile hosts run.
Documentation
//! The `diagnose` operation: a document's problems as a report, without a GPU or reading media.

use std::path::PathBuf;

use serde::Deserialize;
use serde_json::value::RawValue;

use super::RequestError;

/// The document kinds `diagnose` reads.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum DiagnoseDocument {
    /// A project document.
    Project,
    /// A compose spec.
    Spec,
    /// A compose template.
    Template,
    /// A plugin manifest.
    PluginManifest,
}

impl DiagnoseDocument {
    /// The name the protocol and the report use.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Project => "project",
            Self::Spec => "spec",
            Self::Template => "template",
            Self::PluginManifest => "plugin_manifest",
        }
    }
}

/// A `diagnose` request: the document as text and what its diagnosis takes.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DiagnoseRequest {
    /// Always the protocol version.
    pub protocol_version: u32,
    /// The caller's id for the request.
    pub request_id: String,
    #[serde(default, rename = "operation")]
    _operation: serde::de::IgnoredAny,
    #[serde(default, rename = "attachments")]
    _attachments: serde::de::IgnoredAny,
    /// What the text is.
    pub document: DiagnoseDocument,
    /// The document as text, which need not be valid JSON.
    pub text: String,
    /// The options of the document kind, as every surface takes them; the defaults when absent.
    #[serde(default)]
    pub options: Option<Box<RawValue>>,
    /// Font files a spec's text is measured with when it compiles.
    #[serde(default)]
    pub fonts: Vec<PathBuf>,
}

impl DiagnoseRequest {
    /// The options as JSON text, empty for the defaults.
    #[must_use]
    pub fn options_text(&self) -> &str {
        self.options.as_deref().map_or("", RawValue::get)
    }

    /// Checks the fields together.
    ///
    /// # Errors
    /// `INVALID_REQUEST` for font files given with a document other than a spec.
    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(())
    }
}