lupa 0.1.1

Interactive object inspector for Rust — web UI + TUI + snapshot diffing
Documentation
//! Internal API for the `lupa` macros.
//!
//! This module is **not** part of the public surface. It provides the bridge
//! between the procedural macros (`inspect!`, `snapshot!`, `snapshot_diff!`)
//! and the core inspector logic. All items are `#[doc(hidden)]` to keep them
//! out of user‑facing documentation.
//!
//! The macros expand to calls to `send_snapshot()` and `send_diff()` which
//! capture the current value, store it in the global `INSPECTOR_STATE`, and
//! – when the `web` feature is enabled – broadcast the new data over
//! WebSocket to all connected browser clients.

#![doc(hidden)]

use crate::diff::diff_snapshots;
use crate::state::{DiffEvent, INSPECTOR_STATE, Snapshot};
use serde::{Deserialize, Serialize};

/// Raw data passed from the `inspect!` macro before turning it into a full
/// `Snapshot`.
///
/// This structure is created directly inside the macro expansion using
/// `file!()`, `line!()` and the stringified expression. It is then sent to
/// `send_snapshot()` which builds the final `Snapshot`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RawSnapshot {
    /// Original source expression (e.g. `"user"`)
    pub label: String,
    /// `{:#?}` debug representation of the value
    pub debug_repr: String,
    /// Source file where the macro was invoked
    pub file: String,
    /// Line number where the macro was invoked
    pub line: u32,
}

/// Called by `inspect!` – converts a `RawSnapshot` into a full `Snapshot`,
/// stores it in the global state and (if the `web` feature is active)
/// broadcasts it to all WebSocket clients.
pub fn send_snapshot(snap: RawSnapshot) {
    let full = Snapshot::capture(&snap.label, snap.debug_repr, &snap.file, snap.line);
    INSPECTOR_STATE.push_snapshot(full.clone());

    #[cfg(feature = "web")]
    {
        use crate::server::broadcast_snapshot;
        broadcast_snapshot(&full);
    }
}

/// Called by `snapshot_diff!` – computes a line‑level diff between two
/// snapshots, stores the resulting `DiffEvent` and (if the `web` feature is
/// active) broadcasts it to all WebSocket clients.
pub fn send_diff(old: &Snapshot, new: &Snapshot) {
    let chunks = diff_snapshots(old, new);
    let diff_event = DiffEvent {
        old: old.clone(),
        new: new.clone(),
        chunks: chunks.clone(),
    };
    INSPECTOR_STATE.push_diff(old.clone(), new.clone(), chunks);

    #[cfg(feature = "web")]
    {
        use crate::server::broadcast_diff;
        broadcast_diff(&diff_event);
    }
}