Skip to content

Commit 641540b

Browse files
committed
refactor(utils): consolidate size constants and remove unused code
- Move KB, MB, GB constants to module level for reuse - Refactor format_size and format_transfer_rate to use shared constants - Remove unused HTTP_TIMEOUT constant - Remove unused HashMismatch error variant - Remove futures dependency and related unused code - Clean up comments in main.rs and downloader.rs Signed-off-by: Martin Wimpress <martin@wimpress.org>
1 parent ad57969 commit 641540b

7 files changed

Lines changed: 17 additions & 85 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ edition = "2021"
1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1313

1414
[dependencies]
15-
futures = "0.3.31"
1615
indicatif = "0.18.3"
1716
md5 = "0.8.0"
1817
regex = "1.12.2"

src/constants.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
/// User agent string for HTTP requests
44
pub const USER_AGENT: &str = "ia-get";
55

6-
/// Timeout for all HTTP requests in seconds
7-
pub const HTTP_TIMEOUT: u64 = 60;
8-
96
/// Regex pattern for validating archive.org details URLs
107
pub const URL_PATTERN: &str = r"^https://archive\.org/details/[a-zA-Z0-9_\-.@]+/?$";
118

src/downloader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::sync::Arc;
88

99
use colored::*;
1010
use reqwest::header::{HeaderMap, HeaderValue};
11-
use reqwest::Client; // Add this line
11+
use reqwest::Client;
1212

1313
use crate::error::IaGetError; // Import IaGetError for explicit error conversion
1414
use crate::utils::{create_progress_bar, format_duration, format_size, format_transfer_rate};

src/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ pub enum IaGetError {
2020
#[error("Invalid archive.org URL: {0}. Expected format: https://archive.org/details/<identifier>[/]")]
2121
UrlFormat(String),
2222

23-
/// MD5 hash verification failures
24-
#[error("Hash verification failed: {0}")]
25-
HashMismatch(String),
26-
2723
/// XML parsing errors
2824
#[error("Failed to parse XML: {0}")]
2925
XmlParsing(String),

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ia_get::downloader;
1313
use ia_get::utils::{create_spinner, sanitize_filename, validate_archive_url};
1414
use ia_get::Result;
1515
use indicatif::ProgressStyle;
16-
use reqwest::Client; // Add this line
16+
use reqwest::Client;
1717

1818
/// Extended timeout for large file downloads (10 minutes for connection, no read timeout)
1919
const CONNECTION_TIMEOUT_SECS: u64 = 600;

src/utils.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ use crate::{IaGetError, Result};
55
use colored::*;
66
use indicatif::{ProgressBar, ProgressStyle};
77
use regex::Regex;
8-
use std::sync::LazyLock; // Add this line
8+
use std::sync::LazyLock;
99

1010
/// Spinner tick interval in milliseconds
1111
pub const SPINNER_TICK_INTERVAL: u64 = 100;
1212

13+
/// Size constants for formatting
14+
const KB: u64 = 1024;
15+
const MB: u64 = KB * 1024;
16+
const GB: u64 = MB * 1024;
17+
1318
/// Compiled regex for URL validation (initialized once)
1419
static URL_REGEX: LazyLock<Regex> =
1520
LazyLock::new(|| Regex::new(URL_PATTERN).expect("Invalid URL regex pattern"));
@@ -132,10 +137,6 @@ pub fn format_duration(duration: std::time::Duration) -> String {
132137

133138
/// Format a size in bytes to a human-readable string
134139
pub fn format_size(size: u64) -> String {
135-
const KB: u64 = 1024;
136-
const MB: u64 = KB * 1024;
137-
const GB: u64 = MB * 1024;
138-
139140
if size < KB {
140141
format!("{}B", size)
141142
} else if size < MB {
@@ -149,18 +150,18 @@ pub fn format_size(size: u64) -> String {
149150

150151
/// Format transfer rate to appropriate units
151152
pub fn format_transfer_rate(bytes_per_sec: f64) -> (f64, &'static str) {
152-
const KB: f64 = 1024.0;
153-
const MB: f64 = KB * 1024.0;
154-
const GB: f64 = MB * 1024.0;
153+
let kb = KB as f64;
154+
let mb = MB as f64;
155+
let gb = GB as f64;
155156

156-
if bytes_per_sec < KB {
157+
if bytes_per_sec < kb {
157158
(bytes_per_sec, "B")
158-
} else if bytes_per_sec < MB {
159-
(bytes_per_sec / KB, "KB")
160-
} else if bytes_per_sec < GB {
161-
(bytes_per_sec / MB, "MB")
159+
} else if bytes_per_sec < mb {
160+
(bytes_per_sec / kb, "KB")
161+
} else if bytes_per_sec < gb {
162+
(bytes_per_sec / mb, "MB")
162163
} else {
163-
(bytes_per_sec / GB, "GB")
164+
(bytes_per_sec / gb, "GB")
164165
}
165166
}
166167

@@ -304,11 +305,6 @@ pub fn sanitize_filename(filename: &str) -> (String, bool) {
304305
was_modified = true;
305306
}
306307

307-
// Check if result differs from original
308-
if !was_modified {
309-
was_modified = result != filename;
310-
}
311-
312308
(result, was_modified)
313309
}
314310

0 commit comments

Comments
 (0)