use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum QueueWeight {
High,
Medium,
Low,
}
impl QueueWeight {
pub fn weight(&self) -> u32 {
match self {
QueueWeight::High => 6,
QueueWeight::Medium => 3,
QueueWeight::Low => 1,
}
}
}
#[derive(Debug, Clone)]
pub struct WeightedQueue {
pub name: String,
pub weight: QueueWeight,
}
impl WeightedQueue {
pub fn new(name: impl Into<String>, weight: QueueWeight) -> Self {
Self {
name: name.into(),
weight,
}
}
}
pub fn build_weighted_order(queues: &[WeightedQueue]) -> Vec<String> {
let mut order = Vec::new();
for wq in queues {
for _ in 0..wq.weight.weight() {
order.push(wq.name.clone());
}
}
order
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn weighted_order() {
let queues = vec![
WeightedQueue::new("high", QueueWeight::High),
WeightedQueue::new("low", QueueWeight::Low),
];
let order = build_weighted_order(&queues);
let high_count = order.iter().filter(|q| *q == "high").count();
let low_count = order.iter().filter(|q| *q == "low").count();
assert_eq!(high_count, 6);
assert_eq!(low_count, 1);
}
}