pub struct Score<T: ScoreLevels> { /* private fields */ }Expand description
A score answer, carrying the raw continuous score alongside the typed nearest
level.
§Examples
use jev::{Score, ScoreLevels};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Urgency {
NextRelease,
ThisWeek,
BlockingRevenue,
}
impl ScoreLevels for Urgency {
fn levels() -> &'static [&'static str] {
&["Can wait.", "This week.", "Blocking revenue."]
}
fn from_index(index: usize) -> Option<Self> {
match index {
0 => Some(Urgency::NextRelease),
1 => Some(Urgency::ThisWeek),
2 => Some(Urgency::BlockingRevenue),
_ => None,
}
}
fn index(&self) -> usize {
match self {
Urgency::NextRelease => 0,
Urgency::ThisWeek => 1,
Urgency::BlockingRevenue => 2,
}
}
}
let score = Score::<Urgency>::try_new("urgency", 1.9, None, Default::default()).unwrap();
assert_eq!(score.nearest(), Urgency::BlockingRevenue);Implementations§
Source§impl<T: ScoreLevels> Score<T>
impl<T: ScoreLevels> Score<T>
Sourcepub fn try_new(
key: &str,
raw: f64,
confidence: Option<f64>,
probabilities: IndexMap<String, f64>,
) -> Result<Self, Error>
pub fn try_new( key: &str, raw: f64, confidence: Option<f64>, probabilities: IndexMap<String, f64>, ) -> Result<Self, Error>
Builds a Score from a raw wire score, confidence, and probability map.
key names the question this answer came from, used only in the
Error::ScoreRange message if raw is out of range for T.
§Errors
Returns Error::ScoreRange when raw falls outside 0.0..=(levels - 1)
for T::levels().
Sourcepub fn nearest(&self) -> T
pub fn nearest(&self) -> T
The nearest discrete level.
Always returns a value: a Score<T> can only be constructed from a raw score
already validated against T::levels() by Self::try_new, so rounding
raw() to the nearest integer always lands on a valid index.
§Panics
Never panics for a T whose ScoreLevels impl keeps index()/from_index()
consistent with levels() (guaranteed by #[derive(ScoreLevels)]). A
hand-written ScoreLevels impl that violates that contract — for example,
from_index(0) returning None — is a caller-side bug this method cannot
recover from.
Sourcepub fn normalized(&self) -> f64
pub fn normalized(&self) -> f64
raw() normalized to 0.0..=1.0, handy for sorting across different scales.
A single-level scale (levels() == 1) normalizes to 0.0.
Sourcepub fn confidence(&self) -> Option<f64>
pub fn confidence(&self) -> Option<f64>
The server’s confidence in score, when reported.