30 lines
851 B
Rust
30 lines
851 B
Rust
|
|
/// Converts a fractional relevance score into a whole-number percentage for
|
||
|
|
/// the row's badge, rounding rather than truncating so e.g. 0.995 shows 100%
|
||
|
|
/// instead of 99%.
|
||
|
|
pub fn score_percent(final_score: Option<f32>) -> Option<i32> {
|
||
|
|
final_score.map(|s| (s * 100.0).round() as i32)
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn no_score_yields_no_percent() {
|
||
|
|
assert_eq!(score_percent(None), None);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn score_is_rounded_not_truncated() {
|
||
|
|
assert_eq!(score_percent(Some(0.995)), Some(100));
|
||
|
|
assert_eq!(score_percent(Some(0.554)), Some(55));
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn score_is_scaled_to_a_percentage() {
|
||
|
|
assert_eq!(score_percent(Some(0.5)), Some(50));
|
||
|
|
assert_eq!(score_percent(Some(0.0)), Some(0));
|
||
|
|
assert_eq!(score_percent(Some(1.0)), Some(100));
|
||
|
|
}
|
||
|
|
}
|