This module provides utility functions and helper components used throughout the wget-rs application. It contains common functionality that supports various operations across different modules.
- URL parsing and filename extraction
- File path manipulation utilities
- Common helper functions for string processing
- Cross-module utility support
- Reusable components for consistent behavior
utils/url.rs: URL manipulation and parsing utilitiesutils/mod.rs: Exports utility functions
Extracts a filename from a URL for saving downloaded files:
- Input: URL string (e.g.,
"https://example.com/path/file.zip") - Output: Filename string (e.g.,
"file.zip") - Features:
- Handles URLs with query parameters
- Provides fallback names for URLs without clear filenames
- Removes URL encoding from filenames
- Handles edge cases like trailing slashes
use crate::utils::url::extract_filename;
fn main() {
// Basic filename extraction
let filename = extract_filename("https://example.com/downloads/file.zip");
println!("Filename: {}", filename); // Output: "file.zip"
// URL with query parameters
let filename = extract_filename("https://example.com/file.pdf?version=1.2");
println!("Filename: {}", filename); // Output: "file.pdf"
// URL without clear filename
let filename = extract_filename("https://example.com/api/data");
println!("Filename: {}", filename); // Output: "data" or fallback name
}- Parse URL: Extract the path component from the URL
- Remove Query: Strip query parameters and fragments
- Get Last Segment: Take the last path segment as filename
- Fallback Handling: Provide default names for unclear cases
- Sanitization: Ensure filename is valid for filesystem
- Designed to work with various URL formats and edge cases
- Provides consistent filename extraction across the application
- Handles both absolute and relative URL scenarios
- Integrates with file saving operations in HTTP and download modules
- Extensible design allows for additional utility functions
This module can be extended with additional utilities such as:
- File size formatting functions
- Time/date formatting helpers
- Path validation utilities
- String sanitization functions
- Configuration parsing helpers
This module ensures consistent and reliable utility operations across wget-rs.