use ordered_float::NotNan;
use serde::Serialize;
use crate::core::Id;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
pub struct Candidate {
id: Id,
name: &'static str,
party: Option<&'static str>,
positions: Option<Vec<NotNan<f32>>>,
}
impl Candidate {
#[must_use]
pub fn new(
id: Id,
name: &'static str,
party: Option<&'static str>,
positions: Option<Vec<f32>>,
) -> Self {
let positions = positions.map(|positions| {
positions
.into_iter()
.map(|position| NotNan::new(position).expect("Position entry is NaN"))
.collect()
});
Self {
id,
name,
party,
positions,
}
}
#[must_use]
pub const fn id(&self) -> Id {
self.id
}
#[must_use]
pub const fn name(&self) -> &str {
self.name
}
#[must_use]
pub const fn party(&self) -> Option<&str> {
self.party
}
#[must_use]
pub fn positions(&self) -> Option<Vec<NotNan<f32>>> {
self.positions.clone()
}
}