Skip to main content

_soma_native/
lib.rs

1//! Thin, private PyO3 bindings for deterministic Soma provider semantics.
2//!
3//! Reusable behavior remains in PyO3-free Rust crates. This extension only
4//! translates Python inputs and errors at the package boundary.
5
6use pyo3::{exceptions::PyValueError, prelude::*};
7use serde_json::Value;
8use soma_provider_core::validate_provider_manifest_value;
9
10const PROVIDER_SCHEMA_VERSION: u32 = 1;
11
12#[pyfunction]
13fn sdk_version() -> &'static str {
14    env!("CARGO_PKG_VERSION")
15}
16
17#[pyfunction]
18const fn provider_schema_version() -> u32 {
19    PROVIDER_SCHEMA_VERSION
20}
21
22#[pyfunction]
23fn validate_manifest_json(document: &str) -> PyResult<String> {
24    let value: Value = serde_json::from_str(document)
25        .map_err(|error| PyValueError::new_err(format!("invalid provider JSON: {error}")))?;
26    let catalog = validate_provider_manifest_value(&value)
27        .map_err(|error| PyValueError::new_err(error.to_string()))?;
28    serde_json::to_string(&catalog)
29        .map_err(|error| PyValueError::new_err(format!("provider serialization failed: {error}")))
30}
31
32#[pymodule]
33fn _soma_native(module: &Bound<'_, PyModule>) -> PyResult<()> {
34    module.add_function(wrap_pyfunction!(sdk_version, module)?)?;
35    module.add_function(wrap_pyfunction!(provider_schema_version, module)?)?;
36    module.add_function(wrap_pyfunction!(validate_manifest_json, module)?)?;
37    Ok(())
38}
39
40// Behavior tests live in packages/python/tests/ (run via `just
41// test-python-package`): verify_installed.py asserts native_build(),
42// validate_manifest() round-tripping, and the "invalid provider JSON" error
43// against the actually-built extension. A Rust test target is deliberately
44// disabled here — see the `test = false` note in Cargo.toml.