uom icon indicating copy to clipboard operation
uom copied to clipboard

Display mechanism with automagic unit selection

Open HadrienG2 opened this issue 4 years ago • 1 comments

As far as I can see (but maybe I missed something), uom's Quantitydoes not implement Display, and its Debug impl has a very basic unit selection logic. For example, debug-printing a frequency will always yield to a display in s-1, even if the frequency is super-high like terahertzes or something.

It would be nice to have some kind of Display impl that automagically picks a sensible SI unit and multiple, based on the usual rule of thumb of "not more than 3 digits on the left of the decimal point".

However, things may get awkward in cases like information units, where you have both 1000- and 1024-based multiples and bits- and bytes-based units, with no obvious choice as the convention varies depending on which realm of computing you work in...

For use cases like this, a customizable pretty-printing mechanism that lets you pick between families of multiples would also be helpful.

HadrienG2 avatar Mar 20 '20 14:03 HadrienG2

Quantities do implement Display, the documentation is just really hard to find right now: #123. Unit selection is manual. https://docs.rs/uom/0.27.0/uom/si/fmt/struct.Arguments.html

For automagic unit selection, specifying a list of possible units (or having some pre-built lists?) is a good idea. into_format_args could have additional overloads to accept a list and choose the appropriate unit. format_args would be more difficult since it doesn't have a value to select on. Perhaps return a different type that delays unit selection.

let l = Length::new::<meter>(2500.0);
let a = l.into_format_args_overload_that_chooses_unit([meter, kilometer], Abbreviation);

assert_eq!("2.5 km", format!("{}", a));

iliekturtles avatar Mar 20 '20 14:03 iliekturtles