refactor: improve archive.org URL validation

- Improves archive.org URL validation to ensure the identifier after "/details/" is not empty.
- Updates error message for invalid URLs to provide a more descriptive explanation of the expected format.
- Streamlines error handling in the main function by directly displaying the error message from the validation function.
This commit is contained in:
Martin Wimpress
2025-05-25 14:44:50 +01:00
committed by Martin Wimpress
parent d7211b3dab
commit 3bc46f8f9d
3 changed files with 10 additions and 10 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ pub enum IaGetError {
FileSystem(String),
/// URL format or parsing errors
#[error("Invalid URL: {0}")]
#[error("Invalid archive.org URL: {0}. Expected format: https://archive.org/details/<identifier>[/]")]
UrlFormat(String),
/// MD5 hash verification failures
+1 -3
View File
@@ -119,9 +119,7 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
// Validate URL format using consolidated function
if let Err(e) = validate_archive_url(&cli.url) {
spinner.finish_with_message(format!(" Invalid archive.org URL format: {}", cli.url));
println!("├╼ Archive.org URL is not in the expected format");
println!("╰╼ Expected format: https://archive.org/details/<identifier>[/]");
spinner.finish_with_message(format!("{}", e));
return Err(e.into());
}
+8 -6
View File
@@ -33,13 +33,15 @@ static URL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
/// ```
pub fn validate_archive_url(url: &str) -> Result<()> {
if URL_REGEX.is_match(url) {
Ok(())
} else {
Err(IaGetError::UrlFormat(format!(
"URL '{}' does not match expected format. Expected: https://archive.org/details/<identifier>[/]",
url
)))
// Further check: ensure there's an identifier after "details/"
// and that the identifier is not empty.
if let Some(path_segment) = url.split("/details/").nth(1) {
if !path_segment.trim_end_matches('/').is_empty() {
return Ok(());
}
}
}
Err(IaGetError::UrlFormat(url.to_string()))
}
/// Create a progress bar with consistent styling