/rust/registry/src/index.crates.io-6f17d22bba15001f/prodash-29.0.0/src/unit/mod.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use std::{fmt, ops::Deref, sync::Arc}; |
2 | | |
3 | | use crate::progress::Step; |
4 | | |
5 | | #[cfg(feature = "unit-bytes")] |
6 | | mod bytes; |
7 | | #[cfg(feature = "unit-bytes")] |
8 | | pub use bytes::Bytes; |
9 | | |
10 | | #[cfg(feature = "unit-duration")] |
11 | | mod duration; |
12 | | #[cfg(feature = "unit-duration")] |
13 | | pub use duration::Duration; |
14 | | |
15 | | #[cfg(feature = "unit-human")] |
16 | | /// |
17 | | pub mod human; |
18 | | #[cfg(feature = "unit-human")] |
19 | | #[doc(inline)] |
20 | | pub use human::Human; |
21 | | |
22 | | mod range; |
23 | | pub use range::Range; |
24 | | |
25 | | mod traits; |
26 | | pub use traits::DisplayValue; |
27 | | |
28 | | /// Various utilities to display values and units. |
29 | | pub mod display; |
30 | | |
31 | | /// A configurable and flexible unit for use in [Progress::init()][crate::Progress::init()]. |
32 | | #[derive(Debug, Clone, Hash)] |
33 | | pub struct Unit { |
34 | | kind: Kind, |
35 | | mode: Option<display::Mode>, |
36 | | } |
37 | | |
38 | | /// Either a static label or a dynamic one implementing [`DisplayValue`]. |
39 | | #[derive(Clone)] |
40 | | pub enum Kind { |
41 | | /// Display only the given statically known label. |
42 | | Label(&'static str), |
43 | | /// Display a label created dynamically. |
44 | | Dynamic(Arc<dyn DisplayValue + Send + Sync>), |
45 | | } |
46 | | |
47 | | impl std::hash::Hash for Kind { |
48 | 0 | fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
49 | 0 | match self { |
50 | 0 | Kind::Label(s) => { |
51 | 0 | 0.hash(state); |
52 | 0 | s.dyn_hash(state) |
53 | | } |
54 | 0 | Kind::Dynamic(label) => { |
55 | 0 | 1.hash(state); |
56 | 0 | label.dyn_hash(state); |
57 | 0 | } |
58 | | } |
59 | 0 | } |
60 | | } |
61 | | |
62 | | impl fmt::Debug for Kind { |
63 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
64 | 0 | match self { |
65 | 0 | Kind::Label(name) => f.write_fmt(format_args!("Unit::Label({:?})", name)), |
66 | 0 | Kind::Dynamic(_) => f.write_fmt(format_args!("Unit::Dynamic(..)")), |
67 | | } |
68 | 0 | } |
69 | | } |
70 | | |
71 | | impl From<&'static str> for Unit { |
72 | 0 | fn from(v: &'static str) -> Self { |
73 | 0 | label(v) |
74 | 0 | } |
75 | | } |
76 | | |
77 | | /// Returns a unit that is a static `label`. |
78 | 0 | pub fn label(label: &'static str) -> Unit { |
79 | 0 | Unit { |
80 | 0 | kind: Kind::Label(label), |
81 | 0 | mode: None, |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | /// Returns a unit that is a static `label` along with information on where to display a fraction and throughput. |
86 | 0 | pub fn label_and_mode(label: &'static str, mode: display::Mode) -> Unit { |
87 | 0 | Unit { |
88 | 0 | kind: Kind::Label(label), |
89 | 0 | mode: Some(mode), |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | /// Returns a unit that is a dynamic `label`. |
94 | 0 | pub fn dynamic(label: impl DisplayValue + Send + Sync + 'static) -> Unit { |
95 | 0 | Unit { |
96 | 0 | kind: Kind::Dynamic(Arc::new(label)), |
97 | 0 | mode: None, |
98 | 0 | } |
99 | 0 | } Unexecuted instantiation: prodash::unit::dynamic::<prodash::unit::range::Range> Unexecuted instantiation: prodash::unit::dynamic::<_> Unexecuted instantiation: prodash::unit::dynamic::<prodash::unit::range::Range> |
100 | | |
101 | | /// Returns a unit that is a dynamic `label` along with information on where to display a fraction and throughput. |
102 | 0 | pub fn dynamic_and_mode(label: impl DisplayValue + Send + Sync + 'static, mode: display::Mode) -> Unit { |
103 | 0 | Unit { |
104 | 0 | kind: Kind::Dynamic(Arc::new(label)), |
105 | 0 | mode: Some(mode), |
106 | 0 | } |
107 | 0 | } |
108 | | |
109 | | /// Display and utilities |
110 | | impl Unit { |
111 | | /// Create a representation of `self` implementing [`Display`][std::fmt::Display] in configurable fashion. |
112 | | /// |
113 | | /// * `current_value` is the progress value to display. |
114 | | /// * `upper_bound` is the possibly available upper bound of `current_value`. |
115 | | /// * `throughput` configures how throughput should be displayed if already available. |
116 | | /// |
117 | | /// Note that `throughput` is usually not available the first time a value is displayed. |
118 | 0 | pub fn display( |
119 | 0 | &self, |
120 | 0 | current_value: Step, |
121 | 0 | upper_bound: Option<Step>, |
122 | 0 | throughput: impl Into<Option<display::Throughput>>, |
123 | 0 | ) -> display::UnitDisplay { |
124 | 0 | display::UnitDisplay { |
125 | 0 | current_value, |
126 | 0 | upper_bound, |
127 | 0 | throughput: throughput.into(), |
128 | 0 | parent: self, |
129 | 0 | display: display::What::ValuesAndUnit, |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | | /// Return `self` as trait object implementing `DisplayValue`. |
134 | 0 | pub fn as_display_value(&self) -> &dyn DisplayValue { |
135 | 0 | match self.kind { |
136 | 0 | Kind::Label(ref unit) => unit, |
137 | 0 | Kind::Dynamic(ref unit) => unit.deref(), |
138 | | } |
139 | 0 | } |
140 | | } |