#![forbid(unsafe_code)]
pub(crate) const FILTER_NONE: i32 = 0;
pub(crate) const FILTER_ALL: i32 = 1;
pub(crate) const TX_OFF: i32 = 0;
pub(crate) const TX_ON: 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 {
Unreadable,
Keep,
Write(Current),
}
pub(crate) fn decide(current: Option<Current>, rx: bool, tx: bool) -> Plan {
let Some(c) = current else { return Plan::Unreadable };
let mut w = c;
if rx && c.rx_filter == FILTER_NONE {
w.rx_filter = FILTER_ALL;
}
if tx && c.tx_type == TX_OFF {
w.tx_type = TX_ON;
}
if w == c { Plan::Keep } else { Plan::Write(w) }
}
pub(crate) fn effective(c: Current, rx: bool, tx: bool) -> (bool, bool) {
(rx && c.rx_filter != FILTER_NONE, tx && c.tx_type != TX_OFF)
}
#[cfg(test)]
#[allow(clippy::unreachable)]
mod tests {
use super::*;
#[test]
fn the_nic_configuration_is_never_lowered() {
for (rx, tx) in [(false, false), (true, false), (false, true), (true, true)] {
assert_eq!(decide(None, rx, tx), Plan::Unreadable);
for flags in [0, 1] {
for tx_type in 0..=3 {
for rx_filter in 0..=15 {
let c = Current { flags, tx_type, rx_filter };
let after = match decide(Some(c), rx, tx) {
Plan::Unreadable => unreachable!("a readable configuration"),
Plan::Keep => c,
Plan::Write(w) => {
assert_ne!(w, c);
w
}
};
assert_eq!(after.flags, flags);
let rx_up = rx && rx_filter == FILTER_NONE;
assert_eq!(after.rx_filter, if rx_up { FILTER_ALL } else { rx_filter });
let tx_up = tx && tx_type == TX_OFF;
assert_eq!(after.tx_type, if tx_up { TX_ON } else { tx_type });
assert_eq!(effective(after, rx, tx), (rx, tx), "everything asked for is stamped");
}
}
}
}
}
}