units.rs
This file
Path
src/quantity/units.rs
166 lines · 4.7 KiB · Rust
Last changed by
Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
History of this fileTake it
1//! This module contains common units for time and information quantities.23use std::marker::PhantomData;45use crate::quantity::{6 byte, gibibyte, hour, kibibyte, mebibyte, microsecond, millisecond, minute, second, tebibyte,7 Information, Time,8};910pub trait IsUnit {11 type Quantity;1213 fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Self::Quantity>>;1415 fn preferred_precision(&self) -> usize;1617 fn short_name(&self) -> &'static str {18 self.dispatch().short_name()19 }2021 fn format(&self, value: Self::Quantity, precision: usize) -> String {22 self.dispatch().format_value(value, precision)23 }24}2526/// Supported time units27#[derive(Debug, Clone, Copy, PartialEq, Eq)]28pub enum TimeUnit {29 MicroSecond,30 MilliSecond,31 Second,32 Minute,33 Hour,34}3536impl IsUnit for TimeUnit {37 type Quantity = Time;3839 fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Time>> {40 match self {41 TimeUnit::MicroSecond => Box::new(TimeUnitDispatcher::<microsecond>::new()),42 TimeUnit::MilliSecond => Box::new(TimeUnitDispatcher::<millisecond>::new()),43 TimeUnit::Second => Box::new(TimeUnitDispatcher::<second>::new()),44 TimeUnit::Minute => Box::new(TimeUnitDispatcher::<minute>::new()),45 TimeUnit::Hour => Box::new(TimeUnitDispatcher::<hour>::new()),46 }47 }4849 fn preferred_precision(&self) -> usize {50 match self {51 TimeUnit::Second => 3,52 _ => 1,53 }54 }55}5657/// Supported information units58#[derive(Debug, Clone, Copy, PartialEq, Eq)]5960pub enum InformationUnit {61 Byte,62 KibiByte,63 MebiByte,64 GibiByte,65 TebiByte,66}6768impl IsUnit for InformationUnit {69 type Quantity = Information;7071 fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Information>> {72 match self {73 InformationUnit::Byte => Box::new(InformationUnitDispatcher::<byte>::new()),74 InformationUnit::KibiByte => Box::new(InformationUnitDispatcher::<kibibyte>::new()),75 InformationUnit::MebiByte => Box::new(InformationUnitDispatcher::<mebibyte>::new()),76 InformationUnit::GibiByte => Box::new(InformationUnitDispatcher::<gibibyte>::new()),77 InformationUnit::TebiByte => Box::new(InformationUnitDispatcher::<tebibyte>::new()),78 }79 }8081 fn preferred_precision(&self) -> usize {82 match self {83 InformationUnit::Byte => 0,84 _ => 1,85 }86 }87}8889pub trait UnitImpl {90 type Quantity;9192 fn short_name(&self) -> &'static str;93 fn format_value(&self, value: Self::Quantity, precision: usize) -> String;94}9596struct TimeUnitDispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {97 u: PhantomData<U>,98}99100impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> TimeUnitDispatcher<U> {101 fn new() -> Self {102 TimeUnitDispatcher { u: PhantomData }103 }104}105106impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for TimeUnitDispatcher<U> {107 type Quantity = Time;108109 fn short_name(&self) -> &'static str {110 U::abbreviation()111 }112113 fn format_value(&self, value: Time, precision: usize) -> String {114 format!("{value:.precision$}", value = value.get::<U>())115 }116}117118struct InformationUnitDispatcher<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> {119 u: PhantomData<U>,120}121122impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> InformationUnitDispatcher<U> {123 fn new() -> Self {124 InformationUnitDispatcher { u: PhantomData }125 }126}127128impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> UnitImpl129 for InformationUnitDispatcher<U>130{131 type Quantity = Information;132133 fn short_name(&self) -> &'static str {134 U::abbreviation()135 }136137 fn format_value(&self, value: Information, precision: usize) -> String {138 format!("{value:.precision$}", value = value.get::<U>())139 }140}141142#[test]143fn test_time_unit_short_name() {144 assert_eq!("s", TimeUnit::Second.short_name());145 assert_eq!("ms", TimeUnit::MilliSecond.short_name());146 assert_eq!("µs", TimeUnit::MicroSecond.short_name());147 assert_eq!("min", TimeUnit::Minute.short_name());148 assert_eq!("h", TimeUnit::Hour.short_name());149}150151// Note - the values are rounded when formatted.152#[test]153fn test_unit_format() {154 use crate::quantity::FormatQuantity;155156 let value = Time::new::<second>(123.456789);157 assert_eq!("123.457", value.format_value(TimeUnit::Second));158 assert_eq!("123456.8", value.format_value(TimeUnit::MilliSecond));159160 assert_eq!(161 "1234.6",162 Time::new::<second>(0.00123456).format_value(TimeUnit::MicroSecond)163 );164 assert_eq!("2.1", value.format_value(TimeUnit::Minute));165 assert_eq!("0.0", value.format_value(TimeUnit::Hour));166}