Skip to main content

Score

Struct Score 

Source
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>

Source

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().

Source

pub fn raw(&self) -> f64

The probability-weighted position on the scale, e.g. 1.99.

Source

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.

Source

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.

Source

pub fn at_least(&self, level: &T) -> bool

raw() >= level.index().

Source

pub fn confidence(&self) -> Option<f64>

The server’s confidence in score, when reported.

Source

pub fn confident(&self, threshold: f64) -> bool

Confidence is present and >= threshold.

Trait Implementations§

Source§

impl<T: Clone + ScoreLevels> Clone for Score<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + ScoreLevels> Debug for Score<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ScoreLevels> Display for Score<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Renders as the raw score followed by each level’s probability and the confidence, e.g. "1.88 (harmless 0%, wary 12%, lethal 88%; confidence 82%)". Levels are listed in T::levels() declaration order; a level with no reported probability shows 0%.

Source§

impl<T: PartialEq + ScoreLevels> PartialEq for Score<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: PartialEq + ScoreLevels> StructuralPartialEq for Score<T>

Auto Trait Implementations§

§

impl<T> Freeze for Score<T>
where PhantomData<T>: Freeze,

§

impl<T> RefUnwindSafe for Score<T>

§

impl<T> Send for Score<T>
where PhantomData<T>: Send,

§

impl<T> Sync for Score<T>
where PhantomData<T>: Sync,

§

impl<T> Unpin for Score<T>
where PhantomData<T>: Unpin,

§

impl<T> UnsafeUnpin for Score<T>

§

impl<T> UnwindSafe for Score<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more