Skip to main content

brepkit_wasm/
handles.rs

1//! Entity handle resolution and ID conversion helpers.
2//!
3//! The `resolve_*` methods convert raw `u32` handles from JavaScript
4//! into typed arena IDs, returning [`WasmError::InvalidHandle`] on failure.
5//! The `*_id_to_u32` functions do the reverse for returning handles to JS.
6
7#![allow(clippy::missing_errors_doc)]
8
9use crate::error::WasmError;
10use crate::kernel::BrepKernel;
11
12// ── resolve_* methods ─────────────────────────────────────────────
13
14impl BrepKernel {
15    /// Resolve a `u32` face handle to a typed `FaceId`.
16    pub fn resolve_face(&self, handle: u32) -> Result<brepkit_topology::face::FaceId, WasmError> {
17        let index = handle as usize;
18        self.topo
19            .face_id_from_index(index)
20            .ok_or(WasmError::InvalidHandle {
21                entity: "face",
22                index,
23            })
24    }
25
26    /// Resolve a `u32` vertex handle to a typed `VertexId`.
27    pub fn resolve_vertex(
28        &self,
29        handle: u32,
30    ) -> Result<brepkit_topology::vertex::VertexId, WasmError> {
31        let index = handle as usize;
32        self.topo
33            .vertex_id_from_index(index)
34            .ok_or(WasmError::InvalidHandle {
35                entity: "vertex",
36                index,
37            })
38    }
39
40    /// Resolve a `u32` edge handle to a typed `EdgeId`.
41    pub fn resolve_edge(&self, handle: u32) -> Result<brepkit_topology::edge::EdgeId, WasmError> {
42        let index = handle as usize;
43        self.topo
44            .edge_id_from_index(index)
45            .ok_or(WasmError::InvalidHandle {
46                entity: "edge",
47                index,
48            })
49    }
50
51    /// Resolve a `u32` solid handle to a typed `SolidId`.
52    pub fn resolve_solid(
53        &self,
54        handle: u32,
55    ) -> Result<brepkit_topology::solid::SolidId, WasmError> {
56        let index = handle as usize;
57        self.topo
58            .solid_id_from_index(index)
59            .ok_or(WasmError::InvalidHandle {
60                entity: "solid",
61                index,
62            })
63    }
64
65    /// Resolve a `u32` wire handle to a typed `WireId`.
66    pub fn resolve_wire(&self, handle: u32) -> Result<brepkit_topology::wire::WireId, WasmError> {
67        let index = handle as usize;
68        self.topo
69            .wire_id_from_index(index)
70            .ok_or(WasmError::InvalidHandle {
71                entity: "wire",
72                index,
73            })
74    }
75
76    /// Resolve a `u32` shell handle to a typed `ShellId`.
77    pub fn resolve_shell(
78        &self,
79        handle: u32,
80    ) -> Result<brepkit_topology::shell::ShellId, WasmError> {
81        let index = handle as usize;
82        self.topo
83            .shell_id_from_index(index)
84            .ok_or(WasmError::InvalidHandle {
85                entity: "shell",
86                index,
87            })
88    }
89
90    /// Resolve a `u32` compound handle to a typed `CompoundId`.
91    pub fn resolve_compound(
92        &self,
93        handle: u32,
94    ) -> Result<brepkit_topology::compound::CompoundId, WasmError> {
95        let index = handle as usize;
96        self.topo
97            .compound_id_from_index(index)
98            .ok_or(WasmError::InvalidHandle {
99                entity: "compound",
100                index,
101            })
102    }
103}
104
105// ── ID-to-u32 converters ──────────────────────────────────────────
106
107/// Convert a `FaceId` to a `u32` handle for JavaScript.
108#[allow(clippy::cast_possible_truncation)]
109pub const fn face_id_to_u32(id: brepkit_topology::face::FaceId) -> u32 {
110    id.index() as u32
111}
112
113/// Convert a `SolidId` to a `u32` handle for JavaScript.
114#[allow(clippy::cast_possible_truncation)]
115pub const fn solid_id_to_u32(id: brepkit_topology::solid::SolidId) -> u32 {
116    id.index() as u32
117}
118
119/// Convert a `VertexId` to a `u32` handle for JavaScript.
120#[allow(clippy::cast_possible_truncation)]
121pub const fn vertex_id_to_u32(id: brepkit_topology::vertex::VertexId) -> u32 {
122    id.index() as u32
123}
124
125/// Convert an `EdgeId` to a `u32` handle for JavaScript.
126#[allow(clippy::cast_possible_truncation)]
127pub const fn edge_id_to_u32(id: brepkit_topology::edge::EdgeId) -> u32 {
128    id.index() as u32
129}
130
131/// Convert a `WireId` to a `u32` handle for JavaScript.
132#[allow(clippy::cast_possible_truncation)]
133pub const fn wire_id_to_u32(id: brepkit_topology::wire::WireId) -> u32 {
134    id.index() as u32
135}
136
137/// Convert a `ShellId` to a `u32` handle for JavaScript.
138#[allow(clippy::cast_possible_truncation)]
139pub const fn shell_id_to_u32(id: brepkit_topology::shell::ShellId) -> u32 {
140    id.index() as u32
141}
142
143/// Convert a `CompoundId` to a `u32` handle for JavaScript.
144#[allow(clippy::cast_possible_truncation)]
145pub const fn compound_id_to_u32(id: brepkit_topology::compound::CompoundId) -> u32 {
146    id.index() as u32
147}