teamy_uom_extensions/
lib.rs

1//! teamy-uom-extensions
2//!
3//! Small companion crate providing human-friendly display helpers for `uom` types
4//! (uses `humantime` and `humansize` if the `human` feature is enabled).
5//!
6//! This crate intentionally focuses on the `f64` SI aliases from `uom` for the most
7//! convenient runtime-friendly formatting of values like `Time`, `Information` and
8//! `InformationRate`.
9#![deny(missing_docs)]
10#![doc = include_str!("../README.md")]
11#![forbid(unsafe_code)]
12
13#[cfg(all(feature = "full", feature = "human"))]
14pub use humansize;
15#[cfg(feature = "human")]
16pub use humansize::BINARY;
17#[cfg(feature = "human")]
18pub use humansize::DECIMAL;
19// Use the humansize options type when the `human` feature is enabled, otherwise
20// provide a placeholder type so the public API remains stable.
21#[cfg(feature = "human")]
22pub use humansize::FormatSizeOptions;
23#[cfg(all(feature = "full", feature = "human"))]
24pub use humantime;
25/// Re-export commonly used uom types we extend here so callers don't need to import `uom` directly.
26///
27/// If exactly one storage feature is selected we provide convenient root-level aliases so
28/// callers can continue to use `teamy_uom_extensions::Information` like before. If multiple
29/// storage types (eg `f32` and `f64`) are enabled we expose per-storage modules
30/// `teamy_uom_extensions::f32` and `teamy_uom_extensions::f64` so callers can be explicit.
31// The crate no longer provides fine-grain `f32` / `f64` convenience modules.
32// If you want everything exported from the underlying crates for demos / prototyping
33// enable the `full` feature which re-exports `uom`, `humansize` and `humantime`.
34#[cfg(feature = "full")]
35pub use uom;
36
37mod human;
38mod rate;
39
40// Re-export the public traits at crate root for ergonomic import paths (keeps examples/tests working).
41pub use crate::human::si::information::HumanInformationExt;
42pub use crate::human::si::information_rate::HumanInformationRateExt;
43pub use crate::human::si::time::HumanTimeExt;
44// Convenience helper to create information rates from information + time
45pub use crate::rate::si::information::InformationOverExt;
46
47// SI submodules implement the traits and are included above.
48
49// Implementations are associated with the public traits above and are compiled
50// into the crate automatically for the enabled feature set.
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use uom::si::information::byte;
56    use uom::si::information_rate::byte_per_second;
57    use uom::si::time::second;
58
59    #[test]
60    fn information_formatting_f64() {
61        let i = uom::si::f64::Information::new::<byte>(1536.0_f64);
62        assert!(!i.format_human(crate::DECIMAL).is_empty());
63        assert!(!i.format_human(crate::BINARY).is_empty());
64    }
65
66    #[test]
67    fn information_formatting_f32() {
68        let i = uom::si::f32::Information::new::<byte>(1536.0_f32);
69        assert!(!i.format_human(crate::DECIMAL).is_empty());
70        assert!(!i.format_human(crate::BINARY).is_empty());
71    }
72
73    #[test]
74    fn time_formatting_f64_and_f32() {
75        let t64 = uom::si::f64::Time::new::<second>(90.0_f64);
76        let _ = t64.format_human();
77        let _ = t64.format_human_precise();
78
79        let t32 = uom::si::f32::Time::new::<second>(90.0_f32);
80        let _ = t32.format_human();
81        let _ = t32.format_human_precise();
82    }
83
84    #[test]
85    fn info_rate_formatting_f64_and_f32() {
86        let r64 = uom::si::f64::InformationRate::new::<byte_per_second>(2048.0_f64);
87        assert!(!r64.format_human(crate::DECIMAL).is_empty());
88        assert!(!r64.format_human(crate::BINARY).is_empty());
89
90        let r32 = uom::si::f32::InformationRate::new::<byte_per_second>(2048.0_f32);
91        assert!(!r32.format_human(crate::DECIMAL).is_empty());
92        assert!(!r32.format_human(crate::BINARY).is_empty());
93    }
94    // The remaining tests are handled by the single/both feature-specific test modules above.
95}
96// crate-root library — no binary here.