Skip to content

Rust bindings

The neverblink-linkml crate calls the same native library as the Python bindings, so there’s no JVM and no process per schema.

Terminal window
cargo add neverblink-linkml

The build downloads the library of the crate’s release from GitHub, and checks it against the SHA-256 in the crate.

OS Architectures
Linux (glibc) x86-64, ARM64
macOS Apple silicon, Intel
Windows x86-64, ARM64

At run time, the crate looks for the library in this order:

  1. LINKML_SCALA_LIB: the library, or the directory holding it.
  2. Next to the executable. The build copies the library next to the binaries in target/, so ship it beside your binary.
  3. Where the build downloaded it.

To build offline, or with a library you built yourself, set LINKML_SCALA_LIB when building and turn off the default download feature. It is possible to build the library for other platforms – for reference please see how sdist is built in Python bindings.

use ohi_linkml::{load_file, LoadOptions, Severity, ShaclGeneratorOptions};
let schema = load_file("model.yaml", &LoadOptions::default())?;
println!("{}", schema.json_schema(&Default::default())?);
println!("{}", schema.shacl(&ShaclGeneratorOptions { open: true, ..Default::default() })?);
for issue in schema.issues(Some(Severity::Warning)) {
println!("{} {}", issue["severity"], issue["message"]);
}

The API follows the Python bindings:

  • load_file, load_string and load_path load a schema.
  • load_owl and load_ossie import an ontology.
  • Each generator is a method of Schema that takes its own options struct, such as JsonSchemaGeneratorOptions.
  • The option structs are made by the Rust generator from model/converter, in the module ohi_linkml::options. Their Default has the defaults of the options, and they load and dump with serde, such as an OwlImportConfig from its YAML.
  • Generators that write several files return a BTreeMap of file name to content.
  • Dropping a Schema releases it.

Reports, statistics and build info are serde_json::Values, in the shape of the LinkML models in model/.

Schema is Send and Sync, and every function can be called from any thread. The calls into the library run one at a time.

Calls run on the stack of the thread that makes them. A spawned thread’s default 2 MiB is enough for the schemas we tested, including large ones like the CIM, but very deep is_a chains need more. Running out of stack crashes the process instead of returning an error. For such schemas, call from a thread with a bigger stack, such as one from std::thread::Builder::new().stack_size(8 * 1024 * 1024).

You need JDK 17+, clang and cargo. Then:

Terminal window
./mill-native nativelib.native.rustTest

That builds the library and runs the crate’s tests in rust/ against it. To use that library from your own crate, set LINKML_SCALA_LIB=out/nativelib/native/sharedLibrary.dest.