Skip to content

Commit

Permalink
ref: Move ProgressBarMode code to utils
Browse files Browse the repository at this point in the history
This code is more closely related to the utils module's progress bar code than to the api code.

Ref #2008
  • Loading branch information
szokeasaurusrex committed May 28, 2024
1 parent 20d6248 commit 7fb5ed4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
29 changes: 2 additions & 27 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use flate2::write::GzEncoder;
use if_chain::if_chain;
use lazy_static::lazy_static;
use log::{debug, info, warn};
use parking_lot::{Mutex, RwLock};
use parking_lot::Mutex;
use regex::{Captures, Regex};
use sentry::protocol::{Exception, Values};
use serde::de::{DeserializeOwned, Deserializer};
Expand All @@ -48,7 +48,7 @@ use crate::config::{Auth, Config};
use crate::constants::{ARCH, DEFAULT_URL, EXT, PLATFORM, RELEASE_REGISTRY_LATEST_URL, VERSION};
use crate::utils::file_upload::UploadContext;
use crate::utils::http::{self, is_absolute_url};
use crate::utils::progress::ProgressBar;
use crate::utils::progress::{ProgressBar, ProgressBarMode};
use crate::utils::retry::{get_default_backoff, DurationAsMilliseconds};
use crate::utils::sourcemaps::get_sourcemap_reference_from_headers;
use crate::utils::ui::{capitalize_string, make_byte_progress_bar};
Expand All @@ -62,31 +62,6 @@ lazy_static! {
static ref API: Mutex<Option<Arc<Api>>> = Mutex::new(None);
}

#[derive(Clone)]
pub enum ProgressBarMode {
Disabled,
Request,
Response,
Shared((Arc<ProgressBar>, u64, usize, Arc<RwLock<Vec<u64>>>)),
}

impl ProgressBarMode {
/// Returns if progress bars are generally enabled.
pub fn active(&self) -> bool {
!matches!(*self, ProgressBarMode::Disabled)
}

/// Returns whether a progress bar should be displayed during upload.
pub fn request(&self) -> bool {
matches!(*self, ProgressBarMode::Request)
}

/// Returns whether a progress bar should be displayed during download.
pub fn response(&self) -> bool {
matches!(*self, ProgressBarMode::Response)
}
}

/// Helper for the API access.
/// Implements the low-level API access methods, and provides high-level implementations for interacting
/// with portions of the API that do not require authentication via an auth token.
Expand Down
3 changes: 2 additions & 1 deletion src/commands/files/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use clap::{Arg, ArgAction, ArgMatches, Command};
use log::warn;
use symbolic::debuginfo::sourcebundle::SourceFileType;

use crate::api::{Api, ProgressBarMode};
use crate::api::Api;
use crate::config::Config;
use crate::constants::DEFAULT_MAX_WAIT;
use crate::utils::args::validate_distribution;
Expand All @@ -19,6 +19,7 @@ use crate::utils::file_upload::{
initialize_legacy_release_upload, FileUpload, SourceFile, UploadContext,
};
use crate::utils::fs::{decompress_gzip_content, is_gzip_compressed, path_as_url};
use crate::utils::progress::ProgressBarMode;

pub fn make_command(command: Command) -> Command {
command
Expand Down
4 changes: 2 additions & 2 deletions src/utils/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use rayon::prelude::*;
use rayon::ThreadPoolBuilder;
use sha1_smol::Digest;

use crate::api::{Api, ChunkUploadOptions, ProgressBarMode};
use crate::utils::progress::{ProgressBar, ProgressStyle};
use crate::api::{Api, ChunkUploadOptions};
use crate::utils::progress::{ProgressBar, ProgressBarMode, ProgressStyle};

/// Timeout for polling all assemble endpoints.
pub const ASSEMBLE_POLL_INTERVAL: Duration = Duration::from_millis(1000);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/file_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use symbolic::debuginfo::sourcebundle::{SourceBundleWriter, SourceFileInfo, Sour
use url::Url;

use crate::api::NewRelease;
use crate::api::{Api, ChunkUploadCapability, ChunkUploadOptions, ProgressBarMode};
use crate::api::{Api, ChunkUploadCapability, ChunkUploadOptions};
use crate::constants::DEFAULT_MAX_WAIT;
use crate::utils::chunks::{upload_chunks, Chunk, ASSEMBLE_POLL_INTERVAL};
use crate::utils::fs::{get_sha1_checksum, get_sha1_checksums, TempFile};
use crate::utils::progress::{ProgressBar, ProgressStyle};
use crate::utils::progress::{ProgressBar, ProgressBarMode, ProgressStyle};

/// Fallback concurrency for release file uploads.
static DEFAULT_CONCURRENCY: usize = 4;
Expand Down
26 changes: 26 additions & 0 deletions src/utils/progress.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use parking_lot::RwLock;
use std::env;
use std::ops::Deref;
use std::sync::Arc;
Expand Down Expand Up @@ -72,3 +73,28 @@ impl Deref for ProgressBar {
&self.inner
}
}

#[derive(Clone)]
pub enum ProgressBarMode {
Disabled,
Request,
Response,
Shared((Arc<ProgressBar>, u64, usize, Arc<RwLock<Vec<u64>>>)),
}

impl ProgressBarMode {
/// Returns if progress bars are generally enabled.
pub fn active(&self) -> bool {
!matches!(*self, ProgressBarMode::Disabled)
}

/// Returns whether a progress bar should be displayed during upload.
pub fn request(&self) -> bool {
matches!(*self, ProgressBarMode::Request)
}

/// Returns whether a progress bar should be displayed during download.
pub fn response(&self) -> bool {
matches!(*self, ProgressBarMode::Response)
}
}

0 comments on commit 7fb5ed4

Please sign in to comment.