Rust
Project wraps the C ABI and returns Result values.
Basic examples
Parse text with the scinumtools3 crate and read a typed value:
use scinumtools3::Project;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut project = Project::new()?;
project.add_source("simulation.steps int = 100")?;
project.parse()?;
println!("{}", project.node("simulation.steps")?.as_i64()?); // 100
Ok(())
}
Apply an override before evaluating a new project:
use scinumtools3::Project;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut project = Project::new()?;
project.add_source("simulation.steps int = 100")?;
project.add_override("simulation.steps = 200")?;
project.parse()?;
println!("{}", project.node("simulation.steps")?.as_i64()?); // 200
Ok(())
}
Common workflows
Task |
Call chain |
Result |
|---|---|---|
Create an empty project |
|
|
Load an artifact |
|
|
Compose a project |
|
|
Evaluate |
|
|
Read a known value |
|
|
Read a collection |
|
|
Persist or compare |
|
DIPH5 file; |
Release resources |
Drop the node and project |
Automatic; no manual free function |
use scinumtools3::{compare_diph5, Project};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut project = Project::new()?; // Project
project.add_source(
"simulation.steps int = 100\nsimulation.samples int[3] = [1, 2, 3]",
)?; // ()
project.add_override("simulation.steps = 200")?;
project.parse()?; // ()
let steps = project.node("simulation.steps")?; // Node<'_>
println!("{}", steps.as_i64()?); // i64
println!("{:?}", project.node("simulation.samples")?.as_i64_array()?);
project.save("run.diph5")?;
let changes = compare_diph5("before.diph5", "run.diph5")?;
println!("{} changed", changes.changed);
Ok(())
}
Register sources, schemas, and overrides before parse(). A project cannot
be reconfigured after evaluation; create a new Project instead. Nodes
borrow their project and must not outlive it. Native handles are released by
Rust’s Drop implementation, including on error.
See the Rust binding guide for overrides, persistence, and comparison.
DIPL developer tools. Rust parsing validates the DIPL model and returns an
error for invalid input. Use format_dip_source and format_dip_file to
apply the native DIPL formatter; file formatting reads the nearest DIPfile’s
format settings. The crate does not currently wrap the standalone validator or
linter; use snt dip validate and snt dip lint when those results are
needed. See the DIPL developer tools
page for the CLI workflow.
Current limitations and differences
Compared with the canonical C++ implementation:
The safe crate wraps a subset of the C ABI. It has no native Inspector or semantic preview API, host callbacks, adapters, report/export helpers, editing sessions, guided setup, or experiment lifecycle.
A Project is evaluated once. Register sources and overrides before
parse()and create a new Project to evaluate a different configuration. Node handles borrow that project.The standalone validator and linter are not wrapped. Parsing validates the input; use
snt dip validateandsnt dip lintfor those checks.
Binding reference
This is a lookup index, not a sequence to read from top to bottom. Use
Common workflows above to choose a task, then find the exact Rust name
below. Result<T> means the call can fail; properties are shown without
parentheses and callable names include their arguments.
Project lifecycle
Project::new()andProject::load(path).add_source(source),add_file(path), andadd_project(path).add_schema(name, body)andadd_override(body).parse(),node(path),save(path), andsource_files().
Node access
path(),kind(),value_type(), andshape().as_bool(),as_i64(),as_u64(),as_f64(), andas_string().as_bool_array(),as_i64_array(),as_u64_array(),as_f64_array(), andas_string_array().units(),metadata(name),provenance(), andis_overridden().len(),is_empty(),value_len(), andentries().
Comparison
compare_diph5(before, after)returnsComparisonwithadded,removed,changed, anddifferences.Differenceexposespath,category,kind,fields,before,after,changed_elements, andexample_indices.
Formatting
format_dip_source(source, options)formats text withFormatOptions.format_dip_file(path)uses the nearest DIPfile’s format settings.