Skip to content

Commit

Permalink
Ensure that the list of health statuses is exhaustive
Browse files Browse the repository at this point in the history
I've finally found out how to write the macro to define the enum and
the test that matches the health statuses. Turns out I had missed one,
Inuse!!
  • Loading branch information
antifuchs committed Feb 4, 2025
1 parent 04a814b commit c3b3902
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@ use clap::Parser;
use libzetta::zpool::{Health, Zpool, ZpoolEngine, ZpoolOpen3};
use prometheus::{register_int_gauge_vec, Encoder, TextEncoder};

const ALL_HEALTH_STATUSES: &[Health] = {
// TODO: ensure that we're listing them exhaustively; there must be
// some macro magic I could do via a constructed closure with a match
// in it, but I can't figure out the syntax atm.
use Health::*;
&[
macro_rules! define_all_health_statuses {
($const_name:ident = [$($variant:tt ,)+]) => {
const $const_name: &[Health] = {
&[$(::libzetta::zpool::Health::$variant),*]
};
#[test]
fn test_all_statuses_exhaustive() {
let status = ::libzetta::zpool::Health::Online;
match status {
$(::libzetta::zpool::Health::$variant => {}),*
}
}
};
}

define_all_health_statuses!(
ALL_HEALTH_STATUSES = [
Online,
Degraded,
Faulted,
Offline,
Available,
Unavailable,
Removed,
Inuse,
]
};
);

/// Returns true if the health status is OK.
fn is_healthy(health: &Health) -> bool {
Expand Down

0 comments on commit c3b3902

Please sign in to comment.