◆ Reference · Rust
Reference › Rust
Rust
The milkflow crate is the core: pure functions, one per step, and the pipeline::run door that combines them. Rust 1.85 or later; dependencies: serde, serde_json, delaunator.
cargo add milkflow serde_json
Example
use milkflow::model::Input;
use milkflow::pipeline::{Options, run};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input: Input = serde_json::from_str(
r#"{
"nodes": [
{"id": "centro", "x": 0, "y": 0, "label": "Centro"},
{"id": "porto", "x": 3200, "y": -1400, "label": "Porto"},
{"id": "serra", "x": -3800, "y": 3600, "label": "Serra"},
{"id": "olaria", "x": -400, "y": -3900, "label": "Olaria"}
],
"flows": [
{"origin": "serra", "destination": "centro", "value": 410},
{"origin": "porto", "destination": "centro", "value": 260},
{"origin": "olaria", "destination": "centro", "value": 180},
{"origin": "centro", "destination": "porto", "value": 90},
{"origin": "serra", "destination": "porto", "value": 35}
]
}"#,
)?;
let out = run(&input, &Options { k: 2, ..Options::default() })?;
std::fs::write("mapa.svg", out.svg())?;
println!("{:?}", out.spec.meta.accounting);
Ok(())
}
Each step can also be called on its own; How it works shows the full sequence, checked against pipeline::run.
Public functions
| Function | What it does |
|---|---|
pipeline::run(&Input, &Options) -> Result<Run, Error> | The whole pipeline (Algorithm R). Run holds problem, design, represent, skeleton, routing, turns, spec, schematic and style, with the methods svg(), schematic_svg() and pairs_svg(&[bool]). |
pipeline::run_json(&str, &str) -> Result<String, Error> | The JSON door: input and options as text, the outputs as one JSON object. It is what the bindings use. |
problem::build(&Input, group: bool) -> Result<Problem, Error> | Validation, aggregation and canonical order (chapter 2). problem::build_categorized also returns the Categories of the lines mode; problem::at_level(&Input, level) returns the input with each node's group replaced by its group at that level of groups (the levels; Options::grouped_input does it from the options). |
filter::top_k(&Problem, k), dominant, disparity(&Problem, alpha), largest(&Problem, n), share(&Problem, s), threshold(&Problem, min), all | Masks over the pairs (chapter 3). Also filter::Filter (the enum the JSON door reads), terminals, dominant_flows, disparity_p_values and volume (the volume of a mask). See Filters. |
skeleton::delaunay(&Problem) -> Skeleton | The starting triangulation. |
skeleton::reference_scale(&Problem, design, &Skeleton) -> f64 | The scale that makes mu comparable across data. |
skeleton::prune(&Problem, design, &Skeleton, &PruneOptions) -> Skeleton | The greedy pruning (chapter 4). PruneOptions { mu, max_stretch, keep_connected }; here mu is absolute (multiply by the scale). skeleton::objective computes J. |
route::route(&Problem, &Skeleton, represent) -> Routing | Shortest paths and loads (chapter 5); route::path_length, route::check. |
spec::build(&Problem, &Skeleton, &Routing, represent, input_nodes, params) -> FlowSpec | Accounting, metrics, classes (chapter 6); also spec::accounting, spec::breaks, spec::class_of and spec::mark_junctions (Algorithm J: with the user's skeleton, the flowless nodes a segment touches become kind: "junction"). |
flowmodel::turns(&Problem, &Routing) -> Vec<Turn> | The volume that continues through each node (chapter 8). |
route::loads_by_category, spec::add_lines, flowmodel::category_turns, lanes::connect, lanes::apply(&mut FlowSpec) -> u64 | The lines mode (chapter 11 of lanes): loads by category, the lines of each segment, where they continue and the order that minimizes crossings (heuristic; lanes::count_crossings, lanes::lower_bound and, on small instances, lanes::optimum). See Lines. |
schematize::schematize(&FlowSpec, &[Turn], &SchematicOptions) -> Result<FlowSpec, Error> | The octilinear layout (chapter 9), with the contraction of degree-2 chains (contract_chains, on by default). schematize::check returns a Report with the violations of H1–H6 and quality figures. The JSON door takes the subset in schematic_options; the search-tuning fields and contract_chains only here. |
svg::render(&FlowSpec, &Style) -> String | The corridor figure (chapter 7); svg::render_pairs draws pairs as straight lines on the same canvas; Style::fitted fits canvas and symbols; svg::scene is what gets drawn, already placed (bands, connectors, places, labels, legend), of which the SVG and the TikZ are two writers. |
tikz::render(&FlowSpec, &Style, &TikzOptions) -> String | The figure as a LaTeX tikzpicture (chapter 12), from the same scene as the SVG; also Run::tikz and Run::schematic_tikz, tikz::picture (with the notes of what could not be drawn) and tikz::figure_style (the style of the twin SVG). See TikZ. |
html::page(&Run, &PageOptions) -> String | The self-contained interactive page (chapter 11); html::group_members gives the member names of each group for the tooltips. In the JSON door, the option html: true. |
Types
model::Input (with InNode, InFlow) reads the JSON input; Problem is the canonical instance (places, pairs, total, internal); Skeleton { edges }; Routing { paths, load }; FlowSpec, Accounting, Metrics; Error(String), which implements std::error::Error. The fields are in the reference.
Each module opens with a chapter (//!) with the algorithm, the invariants and what is not guaranteed: read them with cargo doc --open.