Queue work from a file, filtering by prefix, suffix and infix if provided Returns the estimated total number of items to process
(
input_tx: Sender<String>,
file_path: &str,
prefix: &str,
suffix: &str,
infix: Option<&str>
)
| 150 | /// Queue work from a file, filtering by prefix, suffix and infix if provided |
| 151 | /// Returns the estimated total number of items to process |
| 152 | pub async fn queue_from_file( |
| 153 | input_tx: Sender<String>, |
| 154 | file_path: &str, |
| 155 | prefix: &str, |
| 156 | suffix: &str, |
| 157 | infix: Option<&str> |
| 158 | ) -> Result<(), Error> { |
| 159 | // Check if file exists |
| 160 | if !tokio::fs::try_exists(file_path).await? { |
| 161 | return Err(anyhow!("File not found: {}", file_path)); |
| 162 | } |
| 163 | |
| 164 | // Check if file is empty |
| 165 | let metadata = tokio::fs::metadata(file_path).await?; |
| 166 | if metadata.len() == 0 { |
| 167 | return Err(anyhow!("File is empty: {}", file_path)); |
| 168 | } |
| 169 | |
| 170 | // Process the file |
| 171 | let file = File::open(file_path).await?; |
| 172 | let reader = BufReader::new(file); |
| 173 | let mut lines = reader.lines(); |
| 174 | |
| 175 | let mut actual_count = 0; |
| 176 | let check_suffix = !suffix.is_empty(); |
| 177 | let check_prefix = !prefix.is_empty(); |
| 178 | let check_infix = infix.is_some(); |
| 179 | |
| 180 | while let Some(line) = lines.next_line().await? { |
| 181 | // Skip empty lines |
| 182 | if line.trim().is_empty() { |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | let phone = line.trim(); |
| 187 | |
| 188 | // Check prefix and suffix conditions |
| 189 | let suffix_match = !check_suffix || phone.ends_with(suffix); |
| 190 | let prefix_match = !check_prefix || phone.starts_with(prefix); |
| 191 | |
| 192 | // Check infix if needed |
| 193 | let infix_match = if check_infix { |
| 194 | let infix_val = infix.unwrap(); |
| 195 | if phone.len() >= 6 { |
| 196 | // Extract the infix (6th and 5th characters from the end) |
| 197 | let potential_infix = &phone[phone.len() - 6..phone.len() - 4]; |
| 198 | potential_infix == infix_val |
| 199 | } else { |
| 200 | false // Phone number too short for infix |
| 201 | } |
| 202 | } else { |
| 203 | true // No infix check needed |
| 204 | }; |
| 205 | |
| 206 | // Only queue if all checks pass |
| 207 | if suffix_match && prefix_match && infix_match { |
| 208 | if let Err(error) = input_tx.send(phone.to_string()).await { |
| 209 | error!("Failed to send to channel: {}", error); |