use gflags;
use std::any::{Any, TypeId};
use std::collections::HashMap;
pub struct ExpectedFlag<'a, T: std::marker::Sized + Any> {
pub doc: &'static [&'static str],
pub name: &'static str,
pub placeholder: Option<&'static str>,
pub generated_flag: &'a gflags::Flag<T>,
}
pub fn fetch_flags() -> HashMap<&'static str, &'static gflags::registry::Flag> {
let mut flags: HashMap<&'static str, &gflags::registry::Flag> = HashMap::new();
for flag in gflags::inventory::iter::<gflags::registry::Flag> {
flags.insert(flag.name, flag);
}
flags
}
pub fn check_flag<T: 'static>(
want: Option<ExpectedFlag<'static, T>>,
got: Option<&gflags::registry::Flag>,
) {
if want.is_none() && got.is_none() {
return;
}
assert_eq!(
want.is_none() && got.is_some(),
false,
"Unexpected flag with name --{}",
got.unwrap().name
);
assert_eq!(
want.is_some() && got.is_none(),
false,
"Failed to find flag with name --{}",
want.unwrap().name
);
let want = want.unwrap();
let got: &gflags::registry::Flag = got.unwrap();
assert_eq!(want.doc, got.doc);
assert_eq!(want.placeholder, got.placeholder);
let typed_flag: gflags::Flag<T> = gflags::Flag::null();
assert!(is_same_type(&typed_flag, want.generated_flag));
}
fn is_same_type<S: ?Sized + std::any::Any, T: ?Sized + std::any::Any>(_s: &S, _t: &T) -> bool {
TypeId::of::<S>() == TypeId::of::<T>()
}