Skip to main content

rskit_bench/
types.rs

1//! Core data types for the bench framework.
2//!
3//! These types mirror gokit's `bench` core types, adapted with Rust idioms.
4//! All types use serde for JSON serialization to ensure cross-language compatibility.
5
6use rskit_errors::AppResult;
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10/// A labeled data point in an evaluation dataset.
11///
12/// Generic over label type `L` for type-safe label handling.
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct BenchSample<L = String> {
15    /// Unique sample identifier.
16    pub id: String,
17    /// Raw input data (file contents).
18    #[serde(skip)]
19    pub input: Vec<u8>,
20    /// Ground-truth label.
21    pub label: L,
22    /// Optional source reference.
23    #[serde(default)]
24    pub source: String,
25    /// Additional metadata.
26    #[serde(default)]
27    pub metadata: HashMap<String, serde_json::Value>,
28}
29
30/// An evaluator's output for a single sample.
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct Prediction<L = String> {
33    /// Reference back to the sample.
34    #[serde(default)]
35    pub sample_id: String,
36    /// Predicted label.
37    pub label: L,
38    /// Primary confidence score (typically 0.0–1.0).
39    pub score: f64,
40    /// Per-label scores (for multi-class).
41    #[serde(default)]
42    pub scores: HashMap<String, f64>,
43    /// Additional prediction metadata.
44    #[serde(default)]
45    pub metadata: HashMap<String, serde_json::Value>,
46}
47
48/// Pairs ground-truth sample with its prediction for metric computation.
49#[derive(Debug, Clone)]
50pub struct ScoredSample<L = String> {
51    pub sample: BenchSample<L>,
52    pub prediction: Prediction<L>,
53}
54
55/// Converts string labels from a manifest into typed labels.
56pub type LabelMapper<L> = Box<dyn Fn(&str) -> AppResult<L> + Send + Sync>;
57
58/// A string-passthrough label mapper for simple string-labeled datasets.
59pub fn string_label_mapper() -> LabelMapper<String> {
60    Box::new(|s| Ok(s.to_string()))
61}
62
63impl<L: Default> Default for Prediction<L> {
64    fn default() -> Self {
65        Self {
66            sample_id: String::default(),
67            label: L::default(),
68            score: 0.0,
69            scores: HashMap::new(),
70            metadata: HashMap::new(),
71        }
72    }
73}