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
//! RAII Wave-1 Direct-FFI [`nativeview_abi::DirectHostRegistry`] registration
//! (`PairHostRegistration`) forwarding host capability calls (`clipboard`, `linking`, `system-env`)
//! from `nv pair` apps to the paired remote device.

use std::sync::Arc;

use nativeview_abi::OwnedHostOps;
use nativeview_clipboard::spec::wire::WireClipboardClient;
use nativeview_clipboard::{box_clipboard_host_ops, NvModClipboardHostOps};
use nativeview_linking::spec::wire::WireLinkingClient;
use nativeview_linking::{box_linking_host_ops, NvModLinkingHostOps};
use nativeview_system_env::spec::wire::WireSystemEnvClient;
use nativeview_system_env::{box_system_env_host_ops, NvModSystemEnvHostOps};

use crate::pair_host::PairByteTransport;

/// RAII owner of Wave-1 OS capability `OwnedHostOps` tables backed by a [`PairByteTransport`].
///
/// Calling [`Self::register_global`] installs them into [`nativeview_abi::DirectHostRegistry::global`]
/// for the lifetime of the pairing session; dropping [`PairHostRegistration`] shuts down the
/// transport and unregisters all ops tables cleanly.
pub struct PairHostRegistration {
    transport: PairByteTransport,
    clipboard: OwnedHostOps<NvModClipboardHostOps>,
    linking: OwnedHostOps<NvModLinkingHostOps>,
    system_env: OwnedHostOps<NvModSystemEnvHostOps>,
}

impl PairHostRegistration {
    /// Boxes all Wave-1 generated `Wire<Mod>Client<PairByteTransport>` tables without modifying
    /// `DirectHostRegistry::global` (call [`Self::register_global`] for production `nv pair`).
    pub fn new(transport: PairByteTransport) -> Self {
        Self {
            clipboard: box_clipboard_host_ops(Arc::new(WireClipboardClient::new(
                transport.clone(),
            ))),
            linking: box_linking_host_ops(Arc::new(WireLinkingClient::new(transport.clone()))),
            system_env: box_system_env_host_ops(Arc::new(WireSystemEnvClient::new(
                transport.clone(),
            ))),
            transport,
        }
    }

    /// Registers all wire-backed host ops tables into [`nativeview_abi::DirectHostRegistry::global`].
    /// Returns any failed `(module_name, status_code)` entries so callers can log or abort.
    pub fn register_global(&self) -> Result<(), Vec<(&'static str, i32)>> {
        let mut errs = Vec::new();
        let mut check = |name: &'static str, status: nativeview_abi::NvModStatus| {
            if !status.is_ok() {
                errs.push((name, status as i32));
            }
        };
        check("clipboard", self.clipboard.register_global());
        check("linking", self.linking.register_global());
        check("system-env", self.system_env.register_global());
        if errs.is_empty() {
            Ok(())
        } else {
            Err(errs)
        }
    }

    pub fn transport(&self) -> &PairByteTransport {
        &self.transport
    }

    pub fn clipboard_ops(&self) -> &OwnedHostOps<NvModClipboardHostOps> {
        &self.clipboard
    }

    pub fn linking_ops(&self) -> &OwnedHostOps<NvModLinkingHostOps> {
        &self.linking
    }

    pub fn system_env_ops(&self) -> &OwnedHostOps<NvModSystemEnvHostOps> {
        &self.system_env
    }
}

impl Drop for PairHostRegistration {
    fn drop(&mut self) {
        self.transport.shutdown();
    }
}