Skip to main content

openbim_ifc_py/
lib.rs

1//! Python bindings for the IFC facade (ADR 0013, #39).
2//!
3//! The compiled module is `openbim_ifc._native`; the public API is the pure
4//! Python package `openbim_ifc`, which wraps it and defines the value
5//! classes. Values cross as plain dicts in the tagged encoding shared with
6//! the WASM and C bindings; `openbim_ifc` turns them into frozen dataclasses.
7//!
8//! This crate adds calling-convention glue only. IFC behaviour belongs in
9//! `openbim-ifc`, shared binding behaviour in `openbim-ifc-binding-core`.
10
11mod authoring;
12mod convert;
13mod edits;
14mod error;
15mod model;
16mod records;
17mod reports;
18mod units;
19
20use pyo3::prelude::*;
21
22/// Opt-in allocator (feature `rusty_alloc`, off by default). It only covers
23/// this extension's Rust allocations; Python's own allocator is untouched.
24/// See #49 for the measurements.
25#[cfg(feature = "rusty_alloc")]
26#[global_allocator]
27static ALLOCATOR: rusty_alloc_api::RustyAlloc = rusty_alloc_api::RustyAlloc;
28
29/// A new random `GlobalId` (RFC 9562 version 4), #463.
30#[pyfunction]
31fn new_guid() -> PyResult<String> {
32    openbim_ifc_binding_core::new_guid().map_err(error::py_err)
33}
34
35/// The reproducible `GlobalId` named by `parts` (RFC 9562 version 8).
36#[pyfunction]
37fn name_based_guid(parts: Vec<String>) -> String {
38    openbim_ifc_binding_core::name_based_guid(&parts)
39}
40
41/// A selector in its canonical form (#464); raises `invalid-selector`.
42#[pyfunction]
43fn canonical_selector(query: &str) -> PyResult<String> {
44    openbim_ifc_binding_core::canonical_selector(query).map_err(error::py_err)
45}
46
47/// The `openbim_ifc._native` extension module.
48#[pymodule]
49fn _native(module: &Bound<'_, PyModule>) -> PyResult<()> {
50    module.add_class::<model::NativeModel>()?;
51    module.add_function(wrap_pyfunction!(new_guid, module)?)?;
52    module.add_function(wrap_pyfunction!(name_based_guid, module)?)?;
53    module.add_function(wrap_pyfunction!(canonical_selector, module)?)?;
54    units::register(module)?;
55    module.add(
56        "SELECTOR_GRAMMAR_VERSION",
57        openbim_ifc_binding_core::SELECTOR_GRAMMAR_VERSION,
58    )?;
59    module.add("IfcError", module.py().get_type::<error::IfcError>())?;
60    Ok(())
61}