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
//! Additive Tier-1 hot-patch differ for `view! { ... }` Rust source files powered by
//! [`nativeview_view_syntax`].
//!
//! Compares old vs new `.rs` source files containing `view!` blocks:
//! - Single-sources lexing, `view!` scanning/parsing, deterministic `XXH64` [`AnchorKey`]
//!   computation, string interpolation analysis, and the Tier-1 direct-root allowlist
//!   (`Node`, `LeafNode`, `Text`, `Pressable`, `ScrollView`, `Column`, `Row`, bare string literals)
//!   directly from `nativeview-view-syntax`.
//! - Reuses `hot_diff`'s typed attribute specs, const evaluator, `TypedStyle`/`Style` chain
//!   scanner, and direct binary `RowPatch` word lowering (zero CSS string emission).
//! - Emits [`HotPatchMsg::TemplatePatch`] with `template_key = anchor_key.0` and
//!   `node_path = Vec::new()`, matching `nativeview_hot_reload::HotPatchIndex`'s O(1) contract.
//! - Falls back cleanly to [`ViewDiffOutcome::NeedsRebuild`] for non-`view!` Rust code edits,
//!   structural tree edits, control-flow changes, dynamic expressions, interpolated strings, or
//!   composite component (`Button`, `Card`, `Dialog`, path-qualified tags) edits.

mod diff;
pub mod patch;
mod prop_specs;
mod props;
mod tokens;

#[cfg(test)]
mod tests;

use nativeview_engine::HotPatchMsg;
use nativeview_view_syntax::{Lex, scan_lex_views, workspace_rel_file};

use crate::hot_diff::cache::FileDiff;

pub use diff::diff_view_source;
pub use patch::{TypedViewPatch, ViewAnchorTarget, ViewAttrChange, ViewTextChange};

use diff::diff_scanned_views;
use tokens::{lex_has_view_macro, lex_streams_equal, may_contain_view_macro};

/// Classification of a watched `.rs` file save containing `view! { ... }` blocks.
#[derive(Debug, Clone, PartialEq)]
pub enum ViewDiffOutcome {
    /// Comment-only, whitespace-only, or semantically identical save (no patch or rebuild needed).
    NoOp,
    /// Static text/style/prop edits on direct-root patchable `view!` nodes, lowered directly to
    /// `AnchorKey`-addressed binary `HotPatchMsg::TemplatePatch` messages (`node_path: vec![]`).
    Tier1Patch {
        patch: TypedViewPatch,
        messages: Vec<HotPatchMsg>,
    },
    /// Non-`view!` Rust edit, structural/control-flow/dynamic change, composite component edit,
    /// or syntax error requiring a Tier-2 rebuild.
    NeedsRebuild { reason: String },
}

/// Fast, zero-allocation pre-check followed by token confirmation returning `true` iff `src`
/// contains at least one `view! { ... }` / `view!(...)` / `view![...]` macro invocation outside
/// comments, string literals, `macro_rules!` definitions, and `.view!` method positions.
pub fn has_view_macro(src: &str) -> bool {
    if !may_contain_view_macro(src) {
        return false;
    }
    let Ok(lex) = Lex::tokenize(src) else {
        // Unbalanced/unterminated syntax while `view!` pattern is present: let `diff_view_source`
        // surface the syntax error cleanly as Tier-2.
        return true;
    };
    lex_has_view_macro(&lex)
}

/// Watch-loop classifier behind [`crate::hot_diff::WatchCache::classify_batch`].
///
/// Comment/whitespace-only saves are [`FileDiff::NoOp`]; edits to files without any `view!`
/// invocation are [`FileDiff::Tier2`] with zero AST parsing; `view!` files tokenize and scan
/// `old_src` / `new_src` at most once.
pub fn classify_for_watch(rel_file: &str, old_src: &str, new_src: &str, generation: u64) -> FileDiff {
    if old_src == new_src {
        return FileDiff::NoOp;
    }
    let old_lex = match Lex::tokenize(old_src) {
        Ok(lex) => lex,
        Err(err) => return FileDiff::Tier2(format!("baseline lex error: {err:?}")),
    };
    let new_lex = match Lex::tokenize(new_src) {
        Ok(lex) => lex,
        Err(err) => return FileDiff::Tier2(format!("syntax error: {err:?}")),
    };
    if lex_streams_equal(&old_lex, &new_lex) {
        return FileDiff::NoOp;
    }
    if !lex_has_view_macro(&old_lex) && !lex_has_view_macro(&new_lex) {
        return FileDiff::Tier2(format!("Rust code changed in {rel_file} (no view! invocation)"));
    }

    let norm_rel = workspace_rel_file(rel_file, "");
    let old_calls = scan_lex_views(&old_lex, &norm_rel);
    let new_calls = scan_lex_views(&new_lex, &norm_rel);
    match diff_scanned_views(&old_lex, &new_lex, &old_calls, &new_calls, generation) {
        ViewDiffOutcome::NoOp => FileDiff::NoOp,
        ViewDiffOutcome::Tier1Patch { patch, messages } => FileDiff::Tier1 {
            logs: patch.log_lines(),
            messages,
        },
        ViewDiffOutcome::NeedsRebuild { reason } => FileDiff::Tier2(reason),
    }
}