BREP_app 0.4.0

The BREP CAD application: an eframe (egui + wgpu) host that draws the brep-render 3D engine into an egui frame — native + wasm from one codebase.
Documentation
//! Per-file WORKBENCH registry — a UI FILTER over feature-CREATION.
//!
//! A "workbench" trims the feature-creation UI (the "Add new feature" palette and
//! the selection context bar) and can gate workbench-specific toolbar buttons /
//! panels. It NEVER changes what the feature history executes, renders, or lets
//! the user edit: a document full of sheet-metal features opens and works
//! identically under Modeling. The ONLY things a workbench touches are the palette
//! (see [`includes_feature`]) and the context offers.
//!
//! EXTENSIBILITY is the whole point: a new workbench = one new file here + one
//! entry in [`WORKBENCHES`]. No enums to edit. Each file exposes a single
//! `'static` [`Workbench`] that OWNS its own inclusion decision (`includes`), its
//! extra toolbar buttons (data only), and the ids of existing panels it claims.
//!
//! `"All"` is special: it accepts every feature and its buttons are the DERIVED
//! union of every other workbench's buttons ([`workbench_buttons`]) — never
//! hand-maintained — so adding a workbench automatically grows it.

pub mod all;
pub mod assembly;
pub mod modeling;
pub mod pmi;
pub mod sheet_metal;
pub mod wire_harness;

/// The minimal view of one catalogue entry a workbench predicate consumes: the
/// feature `type` CODE (e.g. `"E"`, `"S"`, `"SM.F"`), read from the existing
/// app-side catalogue accessor ([`brep_render::features::feature_catalogue`]).
/// Each workbench file classifies DIRECTLY off this code (no kernel-stamped
/// category — the kernel stays untouched). Borrows from the runtime catalogue
/// JSON, hence the lifetime — the registry itself stays `'static` because the
/// fn-pointer predicate is higher-ranked over the borrow.
pub struct FeatureInfo<'a> {
    pub type_code: &'a str,
}

/// A workbench-specific toolbar button, DATA ONLY (no egui, no behavior): the
/// shared toolbar-button helper renders it and a click surfaces `id` out of the
/// toolbar to the shell, which dispatches on `id`. Phase 1 ships none.
pub struct WorkbenchButton {
    /// Stable id the shell matches on when the button is clicked.
    pub id: &'static str,
    /// The single unicode glyph shown on the square button.
    pub glyph: &'static str,
    /// Hover tooltip / human label.
    pub tooltip: &'static str,
}

/// One workbench: a UI filter + optional toolbar buttons + claimed panels. Fully
/// `'static` — fn-pointer predicate and `&'static` slices, no `OnceLock`.
pub struct Workbench {
    /// Stable id, e.g. `"all"` / `"modeling"` / `"sheetMetal"` — the value stored
    /// in `RenderSettings.workbench` and published to the verifier.
    pub id: &'static str,
    /// Human label for the dropdown, e.g. `"Sheet Metal"`.
    pub label: &'static str,
    /// SVG catalog key used by the workbench switcher.
    pub glyph: &'static str,
    /// This workbench OWNS its inclusion decision: does a catalogue entry belong
    /// in this workbench's feature-creation UI?
    pub includes: fn(&FeatureInfo<'_>) -> bool,
    /// Extra toolbar buttons this workbench adds (data only). EMPTY in v1.
    pub buttons: &'static [WorkbenchButton],
    /// Ids of EXISTING panels this workbench CLAIMS. Claim-based, not hide-based:
    /// a panel is visible unless it is claimed by ≥1 workbench and the active one
    /// does not list it (see [`panel_visible`]). EMPTY in v1 (nothing claimed →
    /// every panel visible everywhere). Phase 2 adds claims here, editing NO other
    /// file — the same anti-rot property the button union gives.
    pub panels: &'static [&'static str],
}

/// The fallback / default workbench id. Boot-read validation is implicit:
/// [`resolve`] maps any unknown stored id to this, so there is no separate boot
/// step — every consumer routes through `resolve`.
pub const DEFAULT_WORKBENCH_ID: &str = "modeling";

/// Every workbench, in DROPDOWN ORDER: All, Modeling, Sheet Metal. The dropdown
/// iterates THIS — labels are never hardcoded. A new workbench is appended here.
pub static WORKBENCHES: &[&Workbench] = &[
    &all::ALL,
    &modeling::MODELING,
    &sheet_metal::SHEET_METAL,
    // Placeholders — established for the dropdown, fleshed out later.
    &wire_harness::WIRE_HARNESS,
    &assembly::ASSEMBLY,
    &pmi::PMI,
];

/// Pure lookup by id — `None` if there is no such workbench.
pub fn workbench_by_id(id: &str) -> Option<&'static Workbench> {
    WORKBENCHES.iter().copied().find(|w| w.id == id)
}

/// Resolve a (possibly stale / unknown) stored id to a live workbench, falling
/// back to the default. This IS the boot/read validation — route ALL consumers
/// (dropdown display, palette/offer filters, buttons, panels) through here.
pub fn resolve(id: &str) -> &'static Workbench {
    workbench_by_id(id).unwrap_or_else(|| {
        workbench_by_id(DEFAULT_WORKBENCH_ID).expect("default workbench must be registered")
    })
}

/// The active workbench's toolbar buttons. For `"all"` this is the DEDUPED UNION
/// of every workbench's buttons (so "All" shows every icon without a
/// hand-maintained list); for any other workbench it is that workbench's own
/// buttons.
pub fn workbench_buttons(id: &str) -> Vec<&'static WorkbenchButton> {
    let wb = resolve(id);
    if wb.id == "all" {
        dedupe_buttons(WORKBENCHES.iter().map(|w| w.buttons))
    } else {
        wb.buttons.iter().collect()
    }
}

/// Collect buttons across lists, keeping FIRST occurrence per `id` and preserving
/// order. The union logic behind [`workbench_buttons`] for `"all"`.
fn dedupe_buttons<'a>(
    lists: impl Iterator<Item = &'a [WorkbenchButton]>,
) -> Vec<&'a WorkbenchButton> {
    let mut seen = std::collections::HashSet::new();
    let mut out = Vec::new();
    for list in lists {
        for button in list {
            if seen.insert(button.id) {
                out.push(button);
            }
        }
    }
    out
}

/// Whether a catalogue entry belongs in workbench `active_id`'s feature-creation
/// UI. The ONE entry point the palette + context-offer filters call: builds a
/// [`FeatureInfo`] from the feature `type_code` and runs the resolved workbench's
/// predicate (each workbench file owns its classification off the code).
pub fn includes_feature(active_id: &str, type_code: &str) -> bool {
    let info = FeatureInfo { type_code };
    (resolve(active_id).includes)(&info)
}

/// Whether panel `panel_id` is visible in workbench `active_id`. Claim-based: a
/// panel is visible UNLESS it is claimed by ≥1 workbench and the active workbench
/// does not list it. `"all"` sees every panel (mirrors the button union). With
/// v1's empty `panels` slices nothing is claimed, so every panel is visible in
/// every workbench — the scaffolding is wired but hides nothing.
pub fn panel_visible(active_id: &str, panel_id: &str) -> bool {
    let claimed = WORKBENCHES.iter().any(|w| w.panels.contains(&panel_id));
    if !claimed {
        return true;
    }
    let wb = resolve(active_id);
    if wb.id == "all" {
        return true;
    }
    wb.panels.contains(&panel_id)
}

/// The workbench list as `{id, label}` JSON plus the resolved current id — the
/// `__brepWorkbench` logical-state global the headed verifier reads to drive the
/// dropdown and confirm the active workbench. `current` is the RESOLVED id (an
/// unknown stored id reads back as the default).
pub fn workbench_state_json(current_stored: &str) -> String {
    let available: Vec<serde_json::Value> = WORKBENCHES
        .iter()
        .map(|w| serde_json::json!({ "id": w.id, "label": w.label }))
        .collect();
    serde_json::json!({
        "current": resolve(current_stored).id,
        "available": available,
    })
    .to_string()
}

// BREP private tests: 2f65fdf0861e7fcb