#![forbid(unsafe_code)]
pub(crate) const FILTER_NONE: i32 = 0;
pub(crate) const FILTER_ALL: i32 = 1;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Current {
pub flags: i32,
pub tx_type: i32,
pub rx_filter: i32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Plan {
Software,
Hardware,
Widen(Current),
}
pub(crate) fn decide(current: Option<Current>) -> Plan {
match current {
None => Plan::Software,
Some(c) if c.rx_filter != FILTER_NONE => Plan::Hardware,
Some(c) => Plan::Widen(Current { rx_filter: FILTER_ALL, ..c }),
}
}
#[cfg(test)]
#[allow(clippy::unreachable)]
mod tests {
use super::*;
#[test]
fn the_nic_configuration_is_never_lowered() {
assert_eq!(decide(None), Plan::Software);
for flags in [0, 1] {
for tx_type in 0..=3 {
for rx_filter in 0..=15 {
let current = Current { flags, tx_type, rx_filter };
match decide(Some(current)) {
Plan::Software => unreachable!("a readable configuration is used or widened"),
Plan::Hardware => assert_ne!(rx_filter, FILTER_NONE),
Plan::Widen(w) => {
assert_eq!(rx_filter, FILTER_NONE, "only a NIC that stamps nothing is widened");
assert_eq!((w.flags, w.tx_type, w.rx_filter), (flags, tx_type, FILTER_ALL));
}
}
}
}
}
}
}