ifc_lite_wasm/
lib.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! # IFC-Lite WebAssembly Bindings
6//!
7//! JavaScript/TypeScript API for IFC-Lite built with [wasm-bindgen](https://docs.rs/wasm-bindgen).
8//!
9//! ## Overview
10//!
11//! This crate provides WebAssembly bindings for IFC-Lite, enabling high-performance
12//! IFC parsing and geometry processing in web browsers.
13//!
14//! ## Features
15//!
16//! - **Zero-Copy Buffers**: Direct GPU buffer access without data copying
17//! - **Streaming Parse**: Event-based parsing with progress callbacks
18//! - **Small Bundle**: ~60 KB WASM binary, ~20 KB gzipped
19//!
20//! ## JavaScript Usage
21//!
22//! ```javascript
23//! import init, { IfcAPI, version } from 'ifc-lite-wasm';
24//!
25//! // Initialize WASM
26//! await init();
27//!
28//! // Create API instance
29//! const api = new IfcAPI();
30//!
31//! // Parse IFC file
32//! const buffer = await fetch('model.ifc').then(r => r.arrayBuffer());
33//! const result = api.parse(new Uint8Array(buffer));
34//!
35//! console.log(`Parsed ${result.entityCount} entities`);
36//! console.log(`Version: ${version()}`);
37//! ```
38//!
39//! ## Streaming Parse
40//!
41//! ```javascript
42//! const result = await api.parseStreaming(data, (event) => {
43//!   if (event.type === 'progress') {
44//!     console.log(`Progress: ${event.percent}%`);
45//!   }
46//! });
47//! ```
48//!
49//! ## Zero-Copy Memory Access
50//!
51//! For optimal performance, mesh data can be accessed directly from WASM memory:
52//!
53//! ```javascript
54//! const positions = api.getPositionsBuffer(expressId);
55//! const view = positions.asFloat32Array();
56//!
57//! // Upload directly to GPU without copying
58//! device.queue.writeBuffer(gpuBuffer, 0, view);
59//! ```
60
61use wasm_bindgen::prelude::*;
62
63#[cfg(feature = "console_error_panic_hook")]
64pub use console_error_panic_hook::set_once as set_panic_hook;
65
66mod api;
67mod gpu_geometry;
68mod utils;
69mod zero_copy;
70
71pub use api::IfcAPI;
72pub use gpu_geometry::{
73    GpuGeometry, GpuInstancedGeometry, GpuInstancedGeometryCollection, GpuInstancedGeometryRef,
74    GpuMeshMetadata,
75};
76pub use utils::set_panic_hook as init_panic_hook;
77pub use zero_copy::{
78    get_memory, InstanceData, InstancedGeometry, InstancedMeshCollection, MeshCollection,
79    MeshDataJs, ZeroCopyMesh,
80};
81
82/// Initialize the WASM module.
83///
84/// This function is called automatically when the WASM module is loaded.
85/// It sets up panic hooks for better error messages in the browser console.
86#[wasm_bindgen(start)]
87pub fn init() {
88    #[cfg(feature = "console_error_panic_hook")]
89    console_error_panic_hook::set_once();
90}
91
92/// Get the version of IFC-Lite.
93///
94/// # Returns
95///
96/// Version string (e.g., "0.1.0")
97///
98/// # Example
99///
100/// ```javascript
101/// console.log(`IFC-Lite version: ${version()}`);
102/// ```
103#[wasm_bindgen]
104pub fn version() -> String {
105    env!("CARGO_PKG_VERSION").to_string()
106}