Skip to content

Commit 2327928

Browse files
fix(notifications): delete unnecessary Download(Pop/Push)Unit notifications
This is a continuation of the work that has been done regarding the refactor of the DownloadTracker in favor of `indicatif`.
1 parent 3f6b75b commit 2327928

File tree

4 files changed

+26
-96
lines changed

4 files changed

+26
-96
lines changed

src/cli/download_tracker.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ impl DownloadTracker {
5959
self.create_progress_bar(component.to_owned(), url.to_owned());
6060
true
6161
}
62-
Notification::Install(In::Utils(Un::DownloadPushUnit(_))) => true,
63-
Notification::Install(In::Utils(Un::DownloadPopUnit)) => true,
64-
6562
_ => false,
6663
}
6764
}

src/diskio/threaded.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use tracing::debug;
1616

1717
use super::{CompletedIo, Executor, Item, perform};
1818
use crate::utils::notifications::Notification;
19-
use crate::utils::units::Unit;
2019

2120
#[derive(Copy, Clone, Debug, Enum)]
2221
pub(crate) enum Bucket {
@@ -264,7 +263,6 @@ impl Executor for Threaded<'_> {
264263
let mut prev_files = self.n_files.load(Ordering::Relaxed);
265264
if let Some(handler) = self.notify_handler {
266265
handler(Notification::DownloadFinished(None));
267-
handler(Notification::DownloadPushUnit(Unit::IO));
268266
handler(Notification::DownloadContentLengthReceived(
269267
prev_files as u64,
270268
None,
@@ -294,7 +292,6 @@ impl Executor for Threaded<'_> {
294292
self.pool.join();
295293
if let Some(handler) = self.notify_handler {
296294
handler(Notification::DownloadFinished(None));
297-
handler(Notification::DownloadPopUnit);
298295
}
299296
// close the feedback channel so that blocking reads on it can
300297
// complete. send is atomic, and we know the threads completed from the

src/utils/notifications.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::Path;
44
use url::Url;
55

66
use crate::utils::notify::NotificationLevel;
7-
use crate::utils::units::{self, Unit};
7+
use crate::utils::units;
88

99
#[derive(Debug)]
1010
pub enum Notification<'a> {
@@ -20,12 +20,6 @@ pub enum Notification<'a> {
2020
DownloadDataReceived(&'a [u8], Option<&'a str>),
2121
/// Download has finished.
2222
DownloadFinished(Option<&'a str>),
23-
/// The things we're tracking that are not counted in bytes.
24-
/// Must be paired with a pop-units; our other calls are not
25-
/// setup to guarantee this any better.
26-
DownloadPushUnit(Unit),
27-
/// finish using an unusual unit.
28-
DownloadPopUnit,
2923
NoCanonicalPath(&'a Path),
3024
ResumingPartialDownload,
3125
/// This would make more sense as a crate::notifications::Notification
@@ -55,8 +49,6 @@ impl Notification<'_> {
5549
| DownloadingFile(_, _)
5650
| DownloadContentLengthReceived(_, _)
5751
| DownloadDataReceived(_, _)
58-
| DownloadPushUnit(_)
59-
| DownloadPopUnit
6052
| DownloadFinished(_)
6153
| ResumingPartialDownload
6254
| UsingCurl
@@ -90,13 +82,11 @@ impl Display for Notification<'_> {
9082
SetDefaultBufferSize(size) => write!(
9183
f,
9284
"using up to {} of RAM to unpack components",
93-
units::Size::new(*size, units::Unit::B)
85+
units::Size::new(*size)
9486
),
9587
DownloadingFile(url, _) => write!(f, "downloading file from: '{url}'"),
9688
DownloadContentLengthReceived(len, _) => write!(f, "download size is: '{len}'"),
9789
DownloadDataReceived(data, _) => write!(f, "received some data of size {}", data.len()),
98-
DownloadPushUnit(_) => Ok(()),
99-
DownloadPopUnit => Ok(()),
10090
DownloadFinished(_) => write!(f, "download finished"),
10191
NoCanonicalPath(path) => write!(f, "could not canonicalize path: '{}'", path.display()),
10292
ResumingPartialDownload => write!(f, "resuming partial download"),

src/utils/units.rs

Lines changed: 24 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,32 @@
11
use std::fmt::{self, Display};
22

3+
/// Human readable size representation
34
#[derive(Copy, Clone, Debug)]
4-
pub enum Unit {
5-
B,
6-
IO,
7-
}
8-
9-
/// Human readable size (some units)
10-
pub(crate) enum Size {
11-
B(usize),
12-
IO(usize),
13-
}
5+
pub(crate) struct Size(usize);
146

157
impl Size {
16-
pub(crate) fn new(size: usize, unit: Unit) -> Self {
17-
match unit {
18-
Unit::B => Self::B(size),
19-
Unit::IO => Self::IO(size),
20-
}
8+
pub(crate) fn new(size: usize) -> Self {
9+
Self(size)
2110
}
2211
}
2312

2413
impl Display for Size {
2514
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26-
match self {
27-
Size::B(size) => {
28-
const KI: f64 = 1024.0;
29-
const MI: f64 = KI * KI;
30-
const GI: f64 = KI * KI * KI;
31-
let size = *size as f64;
32-
33-
let suffix: String = "".into();
34-
35-
if size >= GI {
36-
write!(f, "{:5.1} GiB{}", size / GI, suffix)
37-
} else if size >= MI {
38-
write!(f, "{:5.1} MiB{}", size / MI, suffix)
39-
} else if size >= KI {
40-
write!(f, "{:5.1} KiB{}", size / KI, suffix)
41-
} else {
42-
write!(f, "{size:3.0} B{suffix}")
43-
}
44-
}
45-
Size::IO(size) => {
46-
const K: f64 = 1000.0;
47-
const M: f64 = K * K;
48-
const G: f64 = K * K * K;
49-
let size = *size as f64;
50-
51-
let suffix: String = "IO-ops".into();
52-
53-
if size >= G {
54-
write!(f, "{:5.1} giga-{}", size / G, suffix)
55-
} else if size >= M {
56-
write!(f, "{:5.1} mega-{}", size / M, suffix)
57-
} else if size >= K {
58-
write!(f, "{:5.1} kilo-{}", size / K, suffix)
59-
} else {
60-
write!(f, "{size:3.0} {suffix}")
61-
}
62-
}
15+
const KI: f64 = 1024.0;
16+
const MI: f64 = KI * KI;
17+
const GI: f64 = KI * KI * KI;
18+
let size = self.0 as f64;
19+
20+
let suffix: String = "".into();
21+
22+
if size >= GI {
23+
write!(f, "{:5.1} GiB{}", size / GI, suffix)
24+
} else if size >= MI {
25+
write!(f, "{:5.1} MiB{}", size / MI, suffix)
26+
} else if size >= KI {
27+
write!(f, "{:5.1} KiB{}", size / KI, suffix)
28+
} else {
29+
write!(f, "{size:3.0} B{suffix}")
6330
}
6431
}
6532
}
@@ -68,33 +35,12 @@ impl Display for Size {
6835
mod tests {
6936
#[test]
7037
fn unit_formatter_test() {
71-
use crate::utils::units::{Size, Unit};
38+
use crate::utils::units::Size;
7239

7340
// Test Bytes
74-
assert_eq!(format!("{}", Size::new(1, Unit::B)), " 1 B");
75-
assert_eq!(format!("{}", Size::new(1024, Unit::B)), " 1.0 KiB");
76-
assert_eq!(
77-
format!("{}", Size::new(1024usize.pow(2), Unit::B)),
78-
" 1.0 MiB"
79-
);
80-
assert_eq!(
81-
format!("{}", Size::new(1024usize.pow(3), Unit::B)),
82-
" 1.0 GiB"
83-
);
84-
85-
//Test I/O Operations
86-
assert_eq!(format!("{}", Size::new(1, Unit::IO)), " 1 IO-ops");
87-
assert_eq!(
88-
format!("{}", Size::new(1000, Unit::IO)),
89-
" 1.0 kilo-IO-ops"
90-
);
91-
assert_eq!(
92-
format!("{}", Size::new(1000usize.pow(2), Unit::IO)),
93-
" 1.0 mega-IO-ops"
94-
);
95-
assert_eq!(
96-
format!("{}", Size::new(1000usize.pow(3), Unit::IO)),
97-
" 1.0 giga-IO-ops"
98-
);
41+
assert_eq!(format!("{}", Size::new(1)), " 1 B");
42+
assert_eq!(format!("{}", Size::new(1024)), " 1.0 KiB");
43+
assert_eq!(format!("{}", Size::new(1024usize.pow(2))), " 1.0 MiB");
44+
assert_eq!(format!("{}", Size::new(1024usize.pow(3))), " 1.0 GiB");
9945
}
10046
}

0 commit comments

Comments
 (0)