use nutype::nutype;
use serde::{Deserialize, Serialize};
#[nutype(
const_fn,
derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
From,
Into,
Display,
Serialize,
Deserialize
)
)]
pub struct ReportRate(u16);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReportRateInfo {
pub current: ReportRate,
pub supported: Vec<ReportRate>,
}
impl ReportRateInfo {
#[must_use]
pub fn supports(&self, rate: ReportRate) -> bool {
self.supported.contains(&rate)
}
}
#[cfg(test)]
mod tests {
use super::{ReportRate, ReportRateInfo};
#[test]
fn a_rate_is_whole_hertz() {
let rate = ReportRate::new(1000);
assert_eq!(u16::from(rate), 1000);
assert_eq!(rate.to_string(), "1000");
}
#[test]
fn supports_only_listed_rates() {
let info = ReportRateInfo {
current: ReportRate::new(1000),
supported: vec![ReportRate::new(125), ReportRate::new(1000)],
};
assert!(info.supports(ReportRate::new(125)));
assert!(info.supports(ReportRate::new(1000)));
assert!(!info.supports(ReportRate::new(500)));
}
}