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
//! Direct-root `view!` prop specification lookup and conservative overlap-bitmask classification
//! for direct props, shorthands, `layout_ext` bidirectional overrides, and `style:` chains.

use crate::hot_diff::attrs::{
    AttrSpec, is_test_id_attr, lookup_standalone_attr, lookup_typed_style_arg, prop_bits,
};
use crate::hot_diff::lexer::{Lex as PropLex, tokenize as prop_tokenize};
use crate::hot_diff::typed_style_chain::scan_typed_style_chain_with_aliases;

/// Overlap effect of a direct named argument (other than `style` / `nv_style`) on a direct-root
/// `view!` element.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum DirectArgOverlap {
    /// Identity, event handler, text content, accessibility, or known disjoint style property.
    Disjoint,
    /// Directional style/shorthand property writing `bits` (shadows earlier overlapping direct
    /// props and overlaps `style:` chains).
    Shadow(u64),
    /// Bidirectional `layout_ext` override or unknown direct prop: blocks overlapping direct props
    /// regardless of argument order and overlaps `style:` chains.
    Bidirectional(u64),
}

/// Classifies the style-bitmask overlap of a direct named prop (excluding `style` / `nv_style`).
///
/// Unknown non-identity, non-event direct props fail closed as `Bidirectional(prop_bits::ALL)` so
/// an unrecognized shorthand or helper prop can never be silently clobbered by a Tier-1 patch.
pub(super) fn classify_direct_arg_overlap(name: &str) -> DirectArgOverlap {
    use prop_bits as B;

    if let Some(spec) = lookup_view_direct_spec(name) {
        return DirectArgOverlap::Shadow(spec.bits);
    }

    match name {
        // Known shorthand direct props:
        "border" => DirectArgOverlap::Shadow(B::BORDER_WIDTH | B::BORDER_COLOR),
        "padding_xy" | "padding_insets" => DirectArgOverlap::Shadow(B::PAD_ALL),
        "margin_xy" | "margin_insets" => DirectArgOverlap::Shadow(B::MAR_ALL),
        "surface" => DirectArgOverlap::Shadow(
            B::BG | B::BORDER_WIDTH | B::BORDER_COLOR | B::BORDER_RADIUS | B::OPACITY,
        ),
        "text_style" => DirectArgOverlap::Shadow(
            B::TEXT_COLOR | B::FONT_SIZE | B::FONT_WEIGHT | B::TEXT_ALIGN | B::OPACITY,
        ),
        "width_auto" => DirectArgOverlap::Shadow(B::WIDTH),
        "height_auto" => DirectArgOverlap::Shadow(B::HEIGHT),
        "flex_row" | "flex_col" | "flex_row_reverse" | "flex_col_reverse" => {
            DirectArgOverlap::Shadow(B::FLEX_DIR)
        }
        "flex_1" => DirectArgOverlap::Shadow(B::FLEX_SHORTHAND),
        "position_static"
        | "position_relative"
        | "position_absolute"
        | "position_sticky"
        | "position_fixed" => DirectArgOverlap::Shadow(B::POSITION),
        "display_grid" => DirectArgOverlap::Shadow(B::DISPLAY),
        "z_index_auto" => DirectArgOverlap::Shadow(B::Z_INDEX),

        // Bidirectional `layout_ext` logical inline-axis props (override physical X edges in
        // `apply_layout_layer` regardless of declaration order):
        "padding_inline_start" | "padding_inline_end" => DirectArgOverlap::Bidirectional(B::PAD_X),
        "margin_inline_start" | "margin_inline_end" => DirectArgOverlap::Bidirectional(B::MAR_X),

        // Known non-style identity, text content, interaction, scroll, and disjoint style props:
        "id"
        | "key"
        | "text"
        | "content"
        | "disabled"
        | "focusable"
        | "tab_index"
        | "role"
        | "selectable"
        | "horizontal"
        | "vertical"
        | "clip"
        | "bounces"
        | "scroll_enabled"
        | "paging_enabled"
        | "shows_scroll_indicator"
        | "shows_horizontal_scroll_indicator"
        | "shows_vertical_scroll_indicator"
        | "max_lines"
        | "number_of_lines"
        | "ellipsize"
        | "node_ref"
        | "ref"
        | "font_family"
        | "user_select"
        | "box_shadows"
        | "background_fill"
        | "linear_gradient"
        | "radial_gradient"
        | "overscroll_behavior"
        | "overscroll_behavior_x"
        | "overscroll_behavior_y"
        | "overflow_anchor"
        | "transform"
        | "transform_origin"
        | "translate"
        | "scale"
        | "scale_xy"
        | "rotate"
        | "direction"
        | "rtl"
        | "ltr"
        | "pointer_events"
        | "pointer_events_none"
        | "capture_mode"
        | "capture_on_primary_down"
        | "container_type"
        | "container_inline_size"
        | "container_size"
        | "grid_cols"
        | "grid_rows"
        | "grid_column"
        | "grid_row"
        | "grid_column_span"
        | "grid_row_span"
        | "show_on" => DirectArgOverlap::Disjoint,

        _ if is_test_id_attr(name)
            || name.starts_with("on_")
            || name.starts_with("aria_")
            || name.starts_with("accessibility_") =>
        {
            DirectArgOverlap::Disjoint
        }

        // Fail closed for any unknown direct prop.
        _ => DirectArgOverlap::Bidirectional(B::ALL),
    }
}

/// Returns `true` when `name` is a `layout_ext` per-axis gap prop (`row_gap` / `column_gap`),
/// which overrides `layout.gap` in `apply_layout_layer` regardless of argument order.
#[inline]
pub(super) fn is_ext_gap_direct_prop(name: &str) -> bool {
    matches!(name, "row_gap" | "row-gap" | "column_gap" | "column-gap")
}

/// Computes the conservative bitmask of style slots written by a `style:` / `nv_style:` expression.
///
/// Non-chain expressions (variables, helper calls, unknown extension methods) and `.merge*` chains
/// return `prop_bits::ALL` (fail-closed) so overlapping direct prop edits fall back to Tier-2.
pub(super) fn style_expr_written_bits(raw: &str) -> u64 {
    let Ok(tokens) = prop_tokenize(raw) else {
        return prop_bits::ALL;
    };
    let lex = PropLex::new(raw, &tokens);
    let mut steps = Vec::new();
    if !scan_typed_style_chain_with_aliases(lex, 0, lex.len(), &mut steps, true) {
        return prop_bits::ALL;
    }
    steps.iter().fold(0u64, |acc, &(_, bits)| acc | bits)
}

/// Resolves a direct named prop on a direct-root `view!` element to its canonical [`AttrSpec`],
/// reusing `hot_diff::attrs::lookup_typed_style_arg` and adding idiomatic `view!` prop aliases.
pub(super) fn lookup_view_direct_spec(name: &str) -> Option<&'static AttrSpec> {
    if let Some(spec) = lookup_typed_style_arg(name, 0) {
        // `border`, `padding_xy`, and `margin_xy` are 2-arg `TypedStyle` methods; as single named
        // props they are shorthands handled in `classify_direct_arg_overlap`.
        if name != "border" && name != "padding_xy" && name != "margin_xy" {
            return Some(spec);
        }
    }
    match name {
        "spacing" => lookup_typed_style_arg("gap", 0),
        "background_color" => lookup_typed_style_arg("background", 0),
        "padding_horizontal" => lookup_typed_style_arg("padding_x", 0),
        "padding_vertical" => lookup_typed_style_arg("padding_y", 0),
        "margin_horizontal" => lookup_typed_style_arg("margin_x", 0),
        "margin_vertical" => lookup_typed_style_arg("margin_y", 0),
        "top" => lookup_typed_style_arg("inset_top", 0),
        "right" => lookup_typed_style_arg("inset_right", 0),
        "bottom" => lookup_typed_style_arg("inset_bottom", 0),
        "left" => lookup_typed_style_arg("inset_left", 0),
        _ => lookup_standalone_attr(name),
    }
}

/// Float-accepting pixel [`AttrSpec`] for direct dimension/inset props passed bare float literals
/// (`width: 120.0`, `top: 8.0`).
pub(super) fn lookup_direct_float_spec(name: &str) -> Option<&'static AttrSpec> {
    match name {
        "width" => lookup_typed_style_arg("width_px", 0),
        "height" => lookup_typed_style_arg("height_px", 0),
        "min_width" => lookup_typed_style_arg("min_width_px", 0),
        "min_height" => lookup_typed_style_arg("min_height_px", 0),
        "max_width" => lookup_typed_style_arg("max_width_px", 0),
        "max_height" => lookup_typed_style_arg("max_height_px", 0),
        "flex_basis" => lookup_typed_style_arg("flex_basis_px", 0),
        "top" | "inset_top" => lookup_typed_style_arg("inset_top_px", 0),
        "right" | "inset_right" => lookup_typed_style_arg("inset_right_px", 0),
        "bottom" | "inset_bottom" => lookup_typed_style_arg("inset_bottom_px", 0),
        "left" | "inset_left" => lookup_typed_style_arg("inset_left_px", 0),
        _ => None,
    }
}