libobs_bootstrapper/
status_handler.rs

1use std::{convert::Infallible, fmt::Debug};
2
3//NOTE: Maybe do not require to implement Debug here?
4pub trait ObsBootstrapStatusHandler: Debug + Send + Sync {
5    type Error: std::error::Error + Send + Sync + 'static;
6
7    /// Used to report in some way or another the download progress to the user (this is between 0.0 and 1.0)
8    /// # Errors
9    /// This should return an error if the download process should be aborted. This error will be mapped to `ObsBootstrapError::Abort`. This WILL NOT clean up any files or similar, that is the responsibility of the caller.
10    fn handle_downloading(&mut self, progress: f32, message: String) -> Result<(), Self::Error>;
11
12    /// Used to report in some way another the extraction progress to the user (this is between 0.0 and 1.0)
13    /// # Errors
14    /// This should return an error if the extraction process should be aborted. This error will be mapped to `ObsBootstrapError::Abort`. This WILL NOT clean up any files or similar, that
15    fn handle_extraction(&mut self, progress: f32, message: String) -> Result<(), Self::Error>;
16}
17
18#[derive(Debug)]
19pub struct ObsBootstrapConsoleHandler {
20    last_download_percentage: f32,
21    last_extract_percentage: f32,
22}
23
24#[cfg_attr(coverage_nightly, coverage(off))]
25impl Default for ObsBootstrapConsoleHandler {
26    fn default() -> Self {
27        Self {
28            last_download_percentage: 0.0,
29            last_extract_percentage: 0.0,
30        }
31    }
32}
33
34#[cfg_attr(coverage_nightly, coverage(off))]
35impl ObsBootstrapStatusHandler for ObsBootstrapConsoleHandler {
36    type Error = Infallible;
37
38    fn handle_downloading(&mut self, progress: f32, message: String) -> Result<(), Infallible> {
39        if progress - self.last_download_percentage >= 0.05 || progress == 1.0 {
40            self.last_download_percentage = progress;
41            println!("Downloading: {}% - {}", progress * 100.0, message);
42        }
43        Ok(())
44    }
45
46    fn handle_extraction(&mut self, progress: f32, message: String) -> Result<(), Infallible> {
47        if progress - self.last_extract_percentage >= 0.05 || progress == 1.0 {
48            self.last_extract_percentage = progress;
49            println!("Extracting: {}% - {}", progress * 100.0, message);
50        }
51        Ok(())
52    }
53}