BREP_app 0.2.1

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
//! The shared COMPONENT action set (assemblies build-spec §8.5) — the ONE
//! entry point for per-component interactions, consumed by the selection
//! context bar (this lane) and, through the same enum, by the assembly
//! structure tree's per-node action hooks (lane F seam: the tree calls
//! [`run_component_action`] with the node's owning ACOMP feature id — actions
//! always ROUTE TO the owning feature, one truth, one undo lane).
//!
//! Engine-mutating actions (Move / Fix-Unfix / Delete) run HERE against
//! [`EngineState`]; the document-level flow (Edit Part) returns a
//! [`ComponentActionRequest`] for the SHELL, which owns the open documents and
//! the file dialog.

use brep_render::engine_state::EngineState;

/// One per-component action (spec §8.5 / §8.2 tree actions).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ComponentAction {
    /// Toggle the Move gizmo on/off — arrows + rotation arcs together (fixed refuses
    /// with a toast). Engine-side.
    Move,
    /// Open the part's SOURCE document in its own document tab (shell-side).
    /// Editing a component IS opening its part: there is no separate
    /// edit-in-place session any more — the part is a document like any other,
    /// and the assembly picks the change up through the outdated badge /
    /// `panels::update_components` once the part is saved.
    OpenPart,
    /// Fix ⇄ Unfix (writes `isFixed` on the owning ACOMP; re-runs + re-solves).
    ToggleFixed,
    /// Delete the owning ACOMP feature (the library entry GC's kernel-side when
    /// its last instance goes).
    Delete,
}

impl ComponentAction {
    /// Every action, in bar order.
    pub const ALL: [ComponentAction; 4] = [
        ComponentAction::Move,
        ComponentAction::OpenPart,
        ComponentAction::ToggleFixed,
        ComponentAction::Delete,
    ];

    /// Stable id (widget keys / verifier state).
    pub fn id(self) -> &'static str {
        match self {
            ComponentAction::Move => "move",
            ComponentAction::OpenPart => "open-part",
            ComponentAction::ToggleFixed => "toggle-fixed",
            ComponentAction::Delete => "delete",
        }
    }

    /// The id in reverse (`None` for an unknown id).
    pub fn from_id(id: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|action| action.id() == id)
    }

    /// The button label. `fixed` flips the Fix/Unfix wording.
    pub fn label(self, fixed: bool) -> &'static str {
        match self {
            ComponentAction::Move => "\u{2725} Move",
            ComponentAction::OpenPart => "\u{270E} Edit Part",
            ComponentAction::ToggleFixed => {
                if fixed {
                    "\u{1F513} Unfix"
                } else {
                    "\u{1F512} Fix"
                }
            }
            ComponentAction::Delete => "\u{2716} Delete",
        }
    }

    /// Hover tooltip.
    pub fn tooltip(self) -> &'static str {
        match self {
            ComponentAction::Move => "Move/rotate gizmo on-off (arrows + arcs together)",
            ComponentAction::OpenPart => "Open the part's source document in its own tab",
            ComponentAction::ToggleFixed => "Ground / free this instance for the solver",
            ComponentAction::Delete => "Delete this component instance",
        }
    }
}

/// A document-level flow the SHELL must run (it owns the open documents + the
/// file dialog); engine-mutating actions never produce one.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ComponentActionRequest {
    OpenPart { component_id: String },
}

/// Run `action` on the component owned by ACOMP feature `component_id`.
/// Engine-mutating actions apply immediately; Edit Part returns the request the
/// shell dispatches. Unknown component ids toast.
pub fn run_component_action(
    state: &mut EngineState,
    action: ComponentAction,
    component_id: &str,
) -> Option<ComponentActionRequest> {
    match action {
        ComponentAction::Move => {
            // Toggle the full gizmo (arrows + arcs together); a FIXED component
            // refuses with a toast inside the engine (spec §8.5).
            state.component_move_toggle(component_id);
            None
        }
        ComponentAction::ToggleFixed => {
            let Some(info) = state.component_info(component_id) else {
                state.push_notice(format!("'{component_id}' is not an assembly component"));
                return None;
            };
            let mut params = serde_json::from_str::<serde_json::Value>(
                &state.feature_params_json(feature_index(state, component_id)?),
            )
            .unwrap_or_else(|_| serde_json::json!({}));
            if let Some(object) = params.as_object_mut() {
                // An EXPLICIT boolean either way — the kernel honors explicit
                // `false` (un-fixing the sole component stays possible; the
                // absent-auto-grounds rule keys on absence only).
                object.insert("isFixed".into(), serde_json::Value::Bool(!info.fixed));
            }
            let _ = state.update_feature_params(component_id, &params.to_string());
            None
        }
        ComponentAction::Delete => {
            // Deleting the feature is the ONE truth (tree/bar both route here);
            // the parts-library entry GC's at the kernel's next rebuild when its
            // last instance goes.
            let _ = state.delete_feature(component_id);
            None
        }
        ComponentAction::OpenPart => Some(ComponentActionRequest::OpenPart {
            component_id: component_id.to_string(),
        }),
    }
}

/// The feature index carrying id `id` (the engine exposes index→id, so scan).
fn feature_index(state: &EngineState, id: &str) -> Option<usize> {
    (0..state.history_len()).find(|&i| state.feature_id_at(i).as_deref() == Some(id))
}

/// The component's parts-library `sourceKey` (the ModelStore document name the
/// part was inserted from), read off the document's `partsLibrary` block —
/// `None` when the component / entry / key is absent (an imported or
/// embedded-only part, which therefore has no file to open). The Edit-Part flow
/// keys its store lookup on this.
pub fn part_source_key(state: &EngineState, component_id: &str) -> Option<String> {
    let info = state.component_info(component_id)?;
    let document: serde_json::Value =
        serde_json::from_str(&state.history_request_json()).ok()?;
    document["partsLibrary"][&info.part_name]["sourceKey"]
        .as_str()
        .filter(|key| !key.is_empty())
        .map(str::to_string)
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;

    /// The two-instance assembly fixture (mirrors the engine-state one: ACOMP1
    /// fixed at the origin, ACOMP2 free at +20 X, one embedded cube part with
    /// an empty snapshot so the kernel self-heal lane builds it).
    pub(crate) fn two_instance_assembly_json() -> String {
        serde_json::json!({
            "expressions": "",
            "configurator": {},
            "features": [
                {
                    "type": "ACOMP",
                    "inputParams": {
                        "id": "ACOMP1",
                        "partName": "widget",
                        "transform": { "translate": [0.0, 0.0, 0.0], "rotateEulerDeg": [0.0, 0.0, 0.0] },
                        "isFixed": true
                    },
                    "persistentData": {}
                },
                {
                    "type": "ACOMP",
                    "inputParams": {
                        "id": "ACOMP2",
                        "partName": "widget",
                        "transform": { "translate": [20.0, 0.0, 0.0], "rotateEulerDeg": [0.0, 0.0, 0.0] }
                    },
                    "persistentData": {}
                }
            ],
            "partsLibrary": {
                "widget": {
                    "sourceKey": "widget",
                    "sourceSignature": "sig-1",
                    "document": {
                        "expressions": "",
                        "configurator": {},
                        "features": [{
                            "type": "P.CU",
                            "inputParams": {
                                "id": "Part",
                                "sizeX": 10.0, "sizeY": 10.0, "sizeZ": 10.0,
                                "transform": {
                                    "position": [0.0, 0.0, 0.0],
                                    "rotationEuler": [0.0, 0.0, 0.0],
                                    "scale": [1.0, 1.0, 1.0]
                                },
                                "boolean": { "targets": [], "operation": "NONE" }
                            },
                            "persistentData": {}
                        }]
                    },
                    "snapshot": ""
                }
            }
        })
        .to_string()
    }

    pub(crate) fn assembly_engine() -> EngineState {
        let mut engine = EngineState::new();
        engine
            .set_history_json(&two_instance_assembly_json())
            .expect("assembly loads");
        engine
    }

    #[test]
    fn toggle_fixed_writes_an_explicit_boolean_both_ways() {
        let mut engine = assembly_engine();
        assert!(!engine.component_info("ACOMP2").unwrap().fixed);

        // Fix the free instance…
        run_component_action(&mut engine, ComponentAction::ToggleFixed, "ACOMP2");
        assert!(engine.component_info("ACOMP2").unwrap().fixed);

        // …and UNFIX the first (explicitly-fixed) one: the explicit `false`
        // must be honored (the auto-ground rule keys on ABSENCE only).
        run_component_action(&mut engine, ComponentAction::ToggleFixed, "ACOMP1");
        assert!(!engine.component_info("ACOMP1").unwrap().fixed);
    }

    #[test]
    fn delete_removes_the_instance_and_its_members() {
        let mut engine = assembly_engine();
        assert_eq!(engine.scene.solids().len(), 2);
        run_component_action(&mut engine, ComponentAction::Delete, "ACOMP2");
        assert_eq!(engine.history_len(), 1, "the ACOMP feature is gone");
        let names: Vec<&str> = engine.scene.solids().iter().map(|s| s.name.as_str()).collect();
        assert_eq!(names, ["ACOMP1:Part"], "only the surviving instance renders");
    }

    #[test]
    fn move_action_arms_free_and_toasts_fixed() {
        let mut engine = assembly_engine();
        run_component_action(&mut engine, ComponentAction::Move, "ACOMP2");
        assert!(engine.component_move_armed());
        assert_eq!(engine.component_move_armed_feature(), "ACOMP2");

        run_component_action(&mut engine, ComponentAction::Move, "ACOMP1");
        assert_eq!(
            engine.component_move_armed_feature(),
            "ACOMP2",
            "the fixed instance never arms (the free one stays armed)"
        );
        let notices = engine.take_notices();
        assert!(notices.iter().any(|n| n.contains("fixed")), "{notices:?}");
    }

    #[test]
    fn document_flows_return_shell_requests() {
        let mut engine = assembly_engine();
        assert_eq!(
            run_component_action(&mut engine, ComponentAction::OpenPart, "ACOMP1"),
            Some(ComponentActionRequest::OpenPart { component_id: "ACOMP1".into() })
        );
    }

    #[test]
    fn part_source_key_reads_the_library_entry() {
        let engine = assembly_engine();
        assert_eq!(
            part_source_key(&engine, "ACOMP1").as_deref(),
            Some("widget"),
            "the entry's sourceKey"
        );
        assert_eq!(part_source_key(&engine, "ACOMP9"), None, "unknown component");

        // An EMPTY sourceKey (embedded-only part) reads as None → the Edit-Part
        // flow has no file to open and says so.
        let mut doc: serde_json::Value =
            serde_json::from_str(&two_instance_assembly_json()).unwrap();
        doc["partsLibrary"]["widget"]["sourceKey"] = serde_json::json!("");
        let mut engine = EngineState::new();
        engine.set_history_json(&doc.to_string()).unwrap();
        assert_eq!(part_source_key(&engine, "ACOMP1"), None);
    }

    #[test]
    fn action_ids_round_trip() {
        for action in ComponentAction::ALL {
            assert_eq!(ComponentAction::from_id(action.id()), Some(action));
        }
        assert_eq!(ComponentAction::from_id("bogus"), None);
        // Fix/Unfix wording flips on the fixed flag.
        assert!(ComponentAction::ToggleFixed.label(false).contains("Fix"));
        assert!(ComponentAction::ToggleFixed.label(true).contains("Unfix"));
    }
}