score-set
A #![no_std] + alloc Rust library for building weighted scoring
operators — combine named metrics with weights, normalize, and produce a
scoring function or breakdown.
Two public types are all you need:
ScoreSet<N>(builder): collect metrics → normalise weights → produce resultMetric<N>(single metric):fn(&C) -> Scoremeasure + Map01 normalization
Where N is 32 (f32) or 64 (f64).
Quick example
use *;
// Define metrics via a builder pipeline
let clean = metric32
.measure
.by
.map01
.linear;
let food = metric32
.measure
.by
.map01
.identity;
// Combine with weights → weighted-sum closure
let score = new
.push?
.push?
.sum?;
let r = Restaurant ;
let total: f32 = score; // ~0.87
// Or get per-metric breakdown rows
let rows: = new
.push?
.push?
.breakdown?
.into_iter
.collect;
for row in rows
# Ok::
Precision
| Feature flag | Available types | Score type |
|---|---|---|
| (default) | ScoreSet32, Metric32, metric32(), … |
f32 |
f64 |
ScoreSet64, Metric64, metric64(), … |
f64 |
both |
all of the above | both |
[]
= "0.3" # f32 only
= { = "0.3", = ["f64"] } # f64 only
= { = "0.3", = ["both"] } # both
Building a metric
metric32("name") // MetricNamingStage32
.measure() // MeasureStage32
.by(|ctx| raw) // MeasuredStage32<C> — fn pointer, no closures
.map01() // Map01Stage32<C>
.<shape>(...) // Metric32<C>
Map01 shapes
| Shape | Constructor | Formula |
|---|---|---|
| Identity | .identity() |
raw.clamp(0, 1) |
| Linear | .linear(max) |
raw / max, clamped |
| Increasing sigmoid | .inc_sigmoid(low, high) |
1/(1 + e⁻ᵏ⁽ˣ⁻ᵐⁱᵈ⁾) |
| Decreasing sigmoid | .dec_sigmoid(low, high) |
1/(1 + eᵏ⁽ˣ⁻ᵐⁱᵈ⁾) |
| Cauchy (Lorentzian) | .cauchy(center, scale) |
1/(1 + ((x-c)/s)²) |
| Custom | .by(fn(Score) -> Score) |
user-provided |
All variants (except Custom) guarantee output in [0, 1] by construction.
Custom is validated at evaluation time via Value01 witness.
API
ScoreSet<N> — builder
new // empty builder
.push? // add a metric (weight must be > 0, finite)
.sum? // → impl Fn(&C) -> Score
.breakdown? // → impl IntoIterator<Item = Breakdown32>
sum() returns a closure — call it with any number of contexts.
breakdown() evaluates all metrics against one context, returns owned rows.
Metric<N> — single metric
Breakdown<N>
no_std
This crate is #![no_std] with extern crate alloc. It only needs Vec
and String from the allocator, and libm for exp. Works on bare-metal
targets.
License
MIT OR Apache-2.0