[wiggle] Impl different formatters for flags (#1299)
* Impl different formatters for flags Rather than forcing only binary formatting of flags types, how about we implement all relevant traits (`Binary`, `Octal`, `LowerHex`, and `UpperHex`) and allow the user to pick the most relevant one for their use case? Also, we use at least `Octal` and `LowerHex` in a couple of places in `wasi-common`. * fmt::Display for flags now inspired by bitflags Flags is now by default formatted similarly to how `bitflags` crate does it, namely, `dsync|append (0x11)`. In case we're dealing with an empty set, we get `empty (0x0)`. Because of this, any `Octal`, `LowerHex`, etc., formatters are redundant now. Furthermore, while here, I've rewritten `EMPTY_FLAGS` and `ALL_FLAGS` (where the former means `0x0` and the latter is the union of all possible values) to be `const fn empty()` and `const fn all()` where the latter is an expanded union of primitive representation values out of a macro. This is again largely inspired by the `bitflags` crate. * Test fmt::Display for flags
This commit is contained in:
@@ -25,7 +25,7 @@ impl<'a> flags::Flags for WasiCtx<'a> {
|
||||
}
|
||||
|
||||
fn car_config_strat() -> impl Strategy<Value = types::CarConfig> {
|
||||
(1u8..=types::CarConfig::ALL_FLAGS.into())
|
||||
(1u8..=types::CarConfig::all().into())
|
||||
.prop_map(|v| {
|
||||
types::CarConfig::try_from(v).expect("invalid value for types::CarConfig flag")
|
||||
})
|
||||
@@ -99,3 +99,15 @@ proptest! {
|
||||
e.test()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flags_fmt() {
|
||||
let empty = format!("{}", types::CarConfig::empty());
|
||||
assert_eq!(empty, "empty (0x0)");
|
||||
let one_flag = format!("{}", types::CarConfig::AWD);
|
||||
assert_eq!(one_flag, "awd (0x2)");
|
||||
let two_flags = format!("{}", types::CarConfig::AUTOMATIC | types::CarConfig::SUV);
|
||||
assert_eq!(two_flags, "automatic|suv (0x5)");
|
||||
let all = format!("{}", types::CarConfig::all());
|
||||
assert_eq!(all, "automatic|awd|suv (0x7)");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user