Skip to main content

kget/
events.rs

1//! Event types emitted by [`DownloadBuilder::spawn`].
2//!
3//! The channel-based API lets callers react to progress without dealing with
4//! closure lifetimes or shared mutable state.
5//!
6//! # Example
7//!
8//! ```rust,no_run
9//! use kget::{builder, DownloadEvent};
10//!
11//! let (handle, events) = kget::builder("https://example.com/file.zip").spawn();
12//!
13//! for event in events {
14//!     match event {
15//!         DownloadEvent::Progress { percent, speed_bps, .. } =>
16//!             println!("{:.1}%  ({} B/s)", percent, speed_bps),
17//!         DownloadEvent::Completed { path, .. } =>
18//!             println!("Saved to {path}"),
19//!         DownloadEvent::Error(msg) =>
20//!             eprintln!("Failed: {msg}"),
21//!         _ => {}
22//!     }
23//! }
24//!
25//! handle.join().unwrap().unwrap();
26//! ```
27
28/// An event emitted by an in-progress download.
29#[derive(Debug)]
30pub enum DownloadEvent {
31    /// Periodic progress update.
32    Progress {
33        /// Completion percentage (0.0 – 100.0).
34        percent: f64,
35        /// Current transfer speed in bytes per second.
36        speed_bps: u64,
37        /// Estimated seconds remaining (`None` if unknown).
38        eta_secs: Option<u64>,
39    },
40
41    /// Informational message from the download engine (e.g. "Connecting…").
42    Status(String),
43
44    /// The download finished successfully.
45    Completed {
46        /// Absolute path of the saved file.  Empty for in-memory downloads.
47        path: String,
48        /// SHA-256 digest of the file, if verification was requested.
49        sha256: Option<String>,
50    },
51
52    /// The download failed.  Contains the error message.
53    Error(String),
54}