kget/utils.rs
1//! Utility functions for KGet.
2//!
3//! This module provides helper functions used throughout the library:
4//! - Console output management
5//! - URL filename extraction
6//! - Path resolution
7//!
8//! # Example
9//!
10//! ```rust
11//! use kget::get_filename_from_url_or_default;
12//!
13//! let name = get_filename_from_url_or_default(
14//! "https://example.com/downloads/file.zip",
15//! "download"
16//! );
17//! assert_eq!(name, "file.zip");
18//! ```
19
20/// Print a message to the console if not in quiet mode.
21///
22/// # Arguments
23///
24/// * `msg` - The message to print
25/// * `quiet_mode` - If true, suppress printing the message
26///
27/// # Example
28///
29/// ```rust
30/// use kget::print;
31///
32/// print("Starting download...", false); // Prints to stdout
33/// print("Starting download...", true); // Suppressed
34/// ```
35pub fn print(msg: &str, quiet_mode: bool) {
36 if !quiet_mode {
37 println!("{}", msg);
38 }
39}
40
41/// Extract the filename from a URL or return a default.
42///
43/// Parses the URL and returns the last path segment as the filename.
44/// If parsing fails or the path is empty, returns the default filename.
45///
46/// # Arguments
47///
48/// * `url_str` - URL to extract filename from
49/// * `default_filename` - Fallback filename if extraction fails
50///
51/// # Returns
52///
53/// The extracted filename or the default.
54///
55/// # Example
56///
57/// ```rust
58/// use kget::get_filename_from_url_or_default;
59///
60/// // Successful extraction
61/// assert_eq!(
62/// get_filename_from_url_or_default("https://example.com/file.zip", "default"),
63/// "file.zip"
64/// );
65///
66/// // Fallback to default
67/// assert_eq!(
68/// get_filename_from_url_or_default("https://example.com/", "download.bin"),
69/// "download.bin"
70/// );
71/// ```
72pub fn get_filename_from_url_or_default(url_str: &str, default_filename: &str) -> String {
73 // Tries to parse the URL
74 if let Ok(parsed_url) = url::Url::parse(url_str) {
75 // Tries to get the last segment of the path
76 if let Some(segments) = parsed_url.path_segments() {
77 if let Some(last_segment) = segments.last() {
78 if !last_segment.is_empty() {
79 return last_segment.to_string();
80 }
81 }
82 }
83 }
84 // Returns the default filename if parsing fails or the path is empty/invalid
85 default_filename.to_string()
86}
87
88/// Resolve the final output path for a download.
89///
90/// Handles three cases:
91/// 1. `output_arg` is `None`: Extract filename from URL
92/// 2. `output_arg` is a directory: Append filename from URL to directory
93/// 3. `output_arg` is a file path: Use it directly
94///
95/// # Arguments
96///
97/// * `output_arg` - User-provided output path (can be file or directory)
98/// * `url` - Source URL for filename extraction
99/// * `default_name` - Fallback filename if URL doesn't contain one
100///
101/// # Returns
102///
103/// The resolved output file path.
104///
105/// # Example
106///
107/// ```rust
108/// use kget::resolve_output_path;
109///
110/// // No output specified - use filename from URL
111/// let path = resolve_output_path(None, "https://example.com/file.zip", "download");
112/// assert_eq!(path, "file.zip");
113///
114/// // Custom filename
115/// let path = resolve_output_path(Some("myfile.zip".to_string()), "https://example.com/file.zip", "download");
116/// assert_eq!(path, "myfile.zip");
117/// ```
118pub fn resolve_output_path(output_arg: Option<String>, url: &str, default_name: &str) -> String {
119 if let Some(path_str) = output_arg {
120 let path = std::path::Path::new(&path_str);
121 if path.is_dir() {
122 let filename = get_filename_from_url_or_default(url, default_name);
123 path.join(filename).to_string_lossy().to_string()
124 } else {
125 path_str
126 }
127 } else {
128 get_filename_from_url_or_default(url, default_name)
129 }
130}
131