nativeview-cli 0.1.0-rc.1

Developer CLI (`nv`) for Native View Engine: dev hot-reload, builds, pairing, SDK management, app scaffolding, and docs.
Documentation
//! `cargo nv docs check` — standalone generic `nativeview-mdx` validation plus optional site-package
//! delegation (so custom `PlaygroundRegistry` `<LiveDemo>` IDs are verified without linking any
//! example crate into `nativeview-cli`).

use std::path::Path;
use std::process::Command;

use nativeview_mdx::{
    CompileOptions, ComponentRegistry, DiagnosticSeverity, DocsDiagnostic, SiteSource,
    compile_site_with_validation, validate_meta_manifests,
};

use crate::cargo_env;
use crate::docs::build::resolve_site_package_bin;

/// CLI entrypoint for `cargo nv docs check`.
pub(crate) fn run_check(
    content_dir: &Path,
    site_package: Option<&str>,
    generic: bool,
) -> Result<(), String> {
    if !generic {
        let meta = cargo_env::load_cargo_metadata(None).ok();
        if let Some(site_bin) = resolve_site_package_bin(meta.as_ref(), site_package)? {
            let status = Command::new(&site_bin.bin_path)
                .arg("check")
                .arg("--content")
                .arg(content_dir)
                .status()
                .map_err(|e| {
                    format!(
                        "failed to run docs site binary '{}' ({}): {e}",
                        site_bin.bin_path.display(),
                        site_bin.package
                    )
                })?;
            return if status.success() {
                Ok(())
            } else {
                Err(format!("`{} check` exited with {status}", site_bin.package))
            };
        }
    }

    run_generic_check(content_dir)
}

/// Standalone `nativeview-mdx` validation for any external documentation directory.
pub(crate) fn run_generic_check(content_dir: &Path) -> Result<(), String> {
    if !content_dir.is_dir() {
        return Err(format!(
            "content directory does not exist: {}",
            content_dir.display()
        ));
    }

    let source = SiteSource::from_dir(content_dir)
        .map_err(|e| format!("failed to read {}: {e}", content_dir.display()))?;
    let meta_count = source
        .files
        .keys()
        .filter(|k| k.as_str() == "_meta.toml" || k.ends_with("/_meta.toml"))
        .count();

    let registry = ComponentRegistry::new();
    let (site, mdx_diags) =
        compile_site_with_validation(&source, &CompileOptions::default(), &registry);

    let mut diagnostics: Vec<DocsDiagnostic> = mdx_diags
        .into_iter()
        .filter(|d| d.severity == DiagnosticSeverity::Error)
        .map(DocsDiagnostic::from)
        .collect();

    validate_meta_manifests(&source, &mut diagnostics);

    diagnostics.sort_by(|a, b| {
        a.file
            .cmp(&b.file)
            .then_with(|| a.line.cmp(&b.line))
            .then_with(|| a.column.cmp(&b.column))
    });

    if !diagnostics.is_empty() {
        for diag in &diagnostics {
            eprintln!("  [error] {diag}");
        }
        return Err(format!(
            "docs check failed with {} error(s) across {} pages",
            diagnostics.len(),
            site.pages.len()
        ));
    }

    let search_bytes = site
        .search_index
        .encode()
        .map_err(|e| format!("failed to encode {}: {e}", nativeview_mdx::SEARCH_INDEX_FILE_NAME))?
        .len();

    println!(
        "✓ docs check passed: {} pages, {} _meta.toml manifests, {} ({:.1} KiB)",
        site.pages.len(),
        meta_count,
        nativeview_mdx::SEARCH_INDEX_FILE_NAME,
        search_bytes as f64 / 1024.0
    );
    Ok(())
}