use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
pub trait KeywordSort {
fn sort_by_score(&mut self);
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct KeywordScore {
pub keyword: String,
pub score: f64,
}
impl KeywordScore {
pub fn from_map(mp: impl IntoIterator<Item = (String, f64)>) -> Vec<Self> {
mp.into_iter()
.map(|(kw, score)| KeywordScore { keyword: kw, score })
.collect()
}
}
impl Ord for KeywordScore {
fn cmp(&self, other: &Self) -> Ordering {
self.score
.partial_cmp(&other.score)
.unwrap_or(Ordering::Less)
}
}
impl PartialOrd for KeywordScore {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Eq for KeywordScore {}
impl PartialEq for KeywordScore {
fn eq(&self, other: &Self) -> bool {
self.score == other.score
}
}
impl KeywordSort for Vec<KeywordScore> {
fn sort_by_score(&mut self) {
self.sort_by(|a, b| b.cmp(a));
}
}