this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: use io::Error::other() and eliminate unwrap() calls

+102 -152
+3 -1
src/backends/gzip.rs
··· 68 68 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 69 69 if let CmprssOutput::Path(out_path) = &output { 70 70 if out_path.is_dir() { 71 - return cmprss_error("Gzip does not support compressing to a directory. Please specify an output file."); 71 + return cmprss_error( 72 + "Gzip does not support compressing to a directory. Please specify an output file.", 73 + ); 72 74 } 73 75 } 74 76 if let CmprssInput::Path(input_paths) = &input {
+6 -2
src/backends/lz4.rs
··· 42 42 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 43 43 if let CmprssOutput::Path(out_path) = &output { 44 44 if out_path.is_dir() { 45 - return cmprss_error("LZ4 does not support compressing to a directory. Please specify an output file."); 45 + return cmprss_error( 46 + "LZ4 does not support compressing to a directory. Please specify an output file.", 47 + ); 46 48 } 47 49 } 48 50 if let CmprssInput::Path(input_paths) = &input { ··· 100 102 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 101 103 if let CmprssOutput::Path(out_path) = &output { 102 104 if out_path.is_dir() { 103 - return cmprss_error("LZ4 does not support extracting to a directory. Please specify an output file."); 105 + return cmprss_error( 106 + "LZ4 does not support extracting to a directory. Please specify an output file.", 107 + ); 104 108 } 105 109 } 106 110
+12 -12
src/backends/pipeline.rs
··· 251 251 last_compressor.compress(current_thread_input, output)?; 252 252 253 253 for handle in handles { 254 - handle.join().map_err(|_| { 255 - io::Error::new(io::ErrorKind::Other, "Compression thread panicked") 256 - })??; 254 + handle 255 + .join() 256 + .map_err(|_| io::Error::other("Compression thread panicked"))??; 257 257 } 258 258 Ok(()) 259 259 } ··· 302 302 303 303 let final_output = match output { 304 304 CmprssOutput::Path(ref p) => { 305 - if last_extractor.default_extracted_target() == ExtractedTarget::DIRECTORY { 306 - if !p.exists() { 307 - std::fs::create_dir_all(p)?; 308 - } 309 - // If it's a directory, the tar extractor (usually innermost) will handle it. 310 - // The path provided should be the target directory. 305 + if last_extractor.default_extracted_target() == ExtractedTarget::DIRECTORY 306 + && !p.exists() 307 + { 308 + std::fs::create_dir_all(p)?; 311 309 } 310 + // If it's a directory, the tar extractor (usually innermost) will handle it. 311 + // The path provided should be the target directory. 312 312 // Always pass the path; the backend decides how to use it. 313 313 CmprssOutput::Path(p.clone()) 314 314 } ··· 319 319 last_extractor.extract(current_thread_input, final_output)?; 320 320 321 321 for handle in handles { 322 - handle.join().map_err(|_| { 323 - io::Error::new(io::ErrorKind::Other, "Extraction thread panicked") 324 - })??; 322 + handle 323 + .join() 324 + .map_err(|_| io::Error::other("Extraction thread panicked"))??; 325 325 } 326 326 Ok(()) 327 327 }
+5 -11
src/backends/zip.rs
··· 117 117 } 118 118 let file = File::open(&paths[0])?; 119 119 let mut archive = ZipArchive::new(file)?; 120 - archive 121 - .extract(out_dir) 122 - .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) 120 + archive.extract(out_dir).map_err(io::Error::other) 123 121 } 124 122 CmprssInput::Pipe(mut pipe) => { 125 123 // Create a temporary file to store the zip content ··· 133 131 134 132 // Extract from the temporary file 135 133 let mut archive = ZipArchive::new(temp_file)?; 136 - archive 137 - .extract(out_dir) 138 - .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) 139 - } 140 - CmprssInput::Reader(_) => { 141 - return cmprss_error( 142 - "Cannot extract from a reader input for zip (requires seekable input)", 143 - ); 134 + archive.extract(out_dir).map_err(io::Error::other) 144 135 } 136 + CmprssInput::Reader(_) => cmprss_error( 137 + "Cannot extract from a reader input for zip (requires seekable input)", 138 + ), 145 139 } 146 140 } 147 141 CmprssOutput::Pipe(_) => cmprss_error("zip extraction to stdout is not supported"),
+6 -2
src/backends/zstd.rs
··· 90 90 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 91 91 if let CmprssOutput::Path(out_path) = &output { 92 92 if out_path.is_dir() { 93 - return cmprss_error("Zstd does not support compressing to a directory. Please specify an output file."); 93 + return cmprss_error( 94 + "Zstd does not support compressing to a directory. Please specify an output file.", 95 + ); 94 96 } 95 97 } 96 98 if let CmprssInput::Path(input_paths) = &input { ··· 148 150 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 149 151 if let CmprssOutput::Path(out_path) = &output { 150 152 if out_path.is_dir() { 151 - return cmprss_error("Zstd does not support extracting to a directory. Please specify an output file."); 153 + return cmprss_error( 154 + "Zstd does not support extracting to a directory. Please specify an output file.", 155 + ); 152 156 } 153 157 } 154 158
+69 -123
src/main.rs
··· 55 55 match input { 56 56 CmprssInput::Path(paths) => match paths.first() { 57 57 Some(path) => Ok(path), 58 - None => Err(io::Error::new( 59 - io::ErrorKind::Other, 60 - "error: no input specified", 61 - )), 58 + None => Err(io::Error::other("error: no input specified")), 62 59 }, 63 60 CmprssInput::Pipe(_) => Ok(Path::new("archive")), 64 61 CmprssInput::Reader(_) => Ok(Path::new("piped_data")), ··· 234 231 match get_path(in_file) { 235 232 Some(path) => inputs.push(path), 236 233 None => { 237 - return Err(io::Error::new( 238 - io::ErrorKind::Other, 239 - "Specified input path does not exist", 240 - )); 234 + return Err(io::Error::other("Specified input path does not exist")); 241 235 } 242 236 } 243 237 } ··· 247 241 let path = Path::new(output); 248 242 if path.try_exists()? && !path.is_dir() { 249 243 // Output path exists, bail out 250 - return Err(io::Error::new( 251 - io::ErrorKind::Other, 252 - "Specified output path already exists", 253 - )); 244 + return Err(io::Error::other("Specified output path already exists")); 254 245 } 255 246 Some(path) 256 247 } ··· 295 286 if let Some(path) = get_path(input) { 296 287 inputs.push(path); 297 288 } else { 298 - return Err(io::Error::new( 299 - io::ErrorKind::Other, 300 - "Specified input path does not exist", 301 - )); 289 + return Err(io::Error::other("Specified input path does not exist")); 302 290 } 303 291 } 304 292 ··· 311 299 { 312 300 CmprssInput::Pipe(std::io::stdin()) 313 301 } else { 314 - return Err(io::Error::new(io::ErrorKind::Other, "No specified input")); 302 + return Err(io::Error::other("No specified input")); 315 303 } 316 304 } 317 305 false => CmprssInput::Path(inputs), ··· 328 316 } else { 329 317 match action { 330 318 Action::Compress => { 331 - if compressor.is_none() { 332 - return Err(io::Error::new( 333 - io::ErrorKind::Other, 334 - "Must specify a compressor", 335 - )); 336 - } 319 + let c = compressor 320 + .as_ref() 321 + .ok_or_else(|| io::Error::other("Must specify a compressor"))?; 337 322 CmprssOutput::Path(PathBuf::from( 338 - compressor 339 - .as_ref() 340 - .unwrap() 341 - .default_compressed_filename(get_input_filename(&cmprss_input)?), 323 + c.default_compressed_filename(get_input_filename(&cmprss_input)?), 342 324 )) 343 325 } 344 326 Action::Extract => { 345 327 if compressor.is_none() { 346 328 compressor = 347 329 get_compressor_from_filename(get_input_filename(&cmprss_input)?); 348 - if compressor.is_none() { 349 - return Err(io::Error::new( 350 - io::ErrorKind::Other, 351 - "Must specify a compressor", 352 - )); 353 - } 354 330 } 331 + let c = compressor 332 + .as_ref() 333 + .ok_or_else(|| io::Error::other("Must specify a compressor"))?; 355 334 CmprssOutput::Path(PathBuf::from( 356 - compressor 357 - .as_ref() 358 - .unwrap() 359 - .default_extracted_filename(get_input_filename(&cmprss_input)?), 335 + c.default_extracted_filename(get_input_filename(&cmprss_input)?), 360 336 )) 361 337 } 362 338 Action::Unknown => { 363 - if compressor.is_none() { 364 - // Can still work if the input is an archive 365 - compressor = 366 - get_compressor_from_filename(get_input_filename(&cmprss_input)?); 367 - if compressor.is_none() { 368 - return Err(io::Error::new( 369 - io::ErrorKind::Other, 370 - "Must specify a compressor", 371 - )); 372 - } 373 - action = Action::Extract; 374 - CmprssOutput::Path(PathBuf::from( 375 - compressor 376 - .as_ref() 377 - .unwrap() 378 - .default_extracted_filename(get_input_filename(&cmprss_input)?), 379 - )) 380 - } else { 339 + if let Some(ref c) = compressor { 381 340 // We know the compressor, does the input have the same extension? 382 341 if let Some(compressor_from_input) = 383 342 get_compressor_from_filename(get_input_filename(&cmprss_input)?) 384 343 { 385 - if compressor.as_ref().unwrap().name() 386 - == compressor_from_input.name() 387 - { 344 + if c.name() == compressor_from_input.name() { 388 345 action = Action::Extract; 389 - CmprssOutput::Path(PathBuf::from( 390 - compressor.as_ref().unwrap().default_extracted_filename( 391 - get_input_filename(&cmprss_input)?, 392 - ), 393 - )) 346 + CmprssOutput::Path(PathBuf::from(c.default_extracted_filename( 347 + get_input_filename(&cmprss_input)?, 348 + ))) 394 349 } else { 395 350 action = Action::Compress; 396 351 CmprssOutput::Path(PathBuf::from( 397 - compressor.as_ref().unwrap().default_compressed_filename( 398 - get_input_filename(&cmprss_input)?, 399 - ), 352 + c.default_compressed_filename(get_input_filename( 353 + &cmprss_input, 354 + )?), 400 355 )) 401 356 } 402 357 } else { 403 358 action = Action::Compress; 404 - CmprssOutput::Path(PathBuf::from( 405 - compressor.as_ref().unwrap().default_compressed_filename( 406 - get_input_filename(&cmprss_input)?, 407 - ), 408 - )) 359 + CmprssOutput::Path(PathBuf::from(c.default_compressed_filename( 360 + get_input_filename(&cmprss_input)?, 361 + ))) 409 362 } 363 + } else { 364 + // Can still work if the input is an archive 365 + compressor = 366 + get_compressor_from_filename(get_input_filename(&cmprss_input)?); 367 + let c = compressor 368 + .as_ref() 369 + .ok_or_else(|| io::Error::other("Must specify a compressor"))?; 370 + action = Action::Extract; 371 + CmprssOutput::Path(PathBuf::from( 372 + c.default_extracted_filename(get_input_filename(&cmprss_input)?), 373 + )) 410 374 } 411 375 } 412 376 } ··· 426 390 Action::Extract => { 427 391 if let CmprssInput::Path(paths) = &cmprss_input { 428 392 if paths.len() != 1 { 429 - return Err(io::Error::new( 430 - io::ErrorKind::Other, 431 - "Expected a single archive to extract", 432 - )); 393 + return Err(io::Error::other("Expected a single archive to extract")); 433 394 } 434 395 compressor = get_compressor_from_filename(paths.first().unwrap()); 435 396 } ··· 441 402 action = Action::Extract; 442 403 443 404 if compressor.is_none() { 444 - return Err(io::Error::new( 445 - io::ErrorKind::Other, 446 - format!( 447 - "Couldn't determine how to extract {:?}", 448 - paths.first().unwrap() 449 - ), 450 - )); 405 + return Err(io::Error::other(format!( 406 + "Couldn't determine how to extract {:?}", 407 + paths.first().unwrap() 408 + ))); 451 409 } 452 410 } else { 453 411 let (guessed_compressor, guessed_action) = ··· 457 415 } 458 416 } 459 417 (CmprssInput::Path(paths), CmprssOutput::Pipe(_)) => { 460 - if compressor.is_none() { 418 + if let Some(ref c) = compressor { 419 + if let Some(input_c) = get_compressor_from_filename(paths.first().unwrap()) 420 + { 421 + if c.name() == input_c.name() { 422 + action = Action::Extract; 423 + } else { 424 + action = Action::Compress; 425 + } 426 + } else { 427 + action = Action::Compress; 428 + } 429 + } else { 461 430 if paths.len() != 1 { 462 - return Err(io::Error::new( 463 - io::ErrorKind::Other, 431 + return Err(io::Error::other( 464 432 "Expected a single input file for piping to stdout", 465 433 )); 466 434 } ··· 468 436 if compressor.is_some() { 469 437 action = Action::Extract; 470 438 } else { 471 - return Err(io::Error::new( 472 - io::ErrorKind::Other, 473 - "Can't guess compressor to use", 474 - )); 475 - } 476 - } else if let Some(c) = get_compressor_from_filename(paths.first().unwrap()) { 477 - if compressor.as_ref().unwrap().name() == c.name() { 478 - action = Action::Extract; 479 - } else { 480 - action = Action::Compress; 439 + return Err(io::Error::other("Can't guess compressor to use")); 481 440 } 482 - } else { 483 - action = Action::Compress; 484 441 } 485 442 } 486 443 (CmprssInput::Pipe(_), CmprssOutput::Path(path)) => { 487 - if compressor.is_none() { 444 + if let Some(ref c) = compressor { 445 + if get_compressor_from_filename(path) 446 + .is_some_and(|pc| c.name() == pc.name()) 447 + { 448 + action = Action::Compress; 449 + } else { 450 + action = Action::Extract; 451 + } 452 + } else { 488 453 compressor = get_compressor_from_filename(path); 489 454 if compressor.is_some() { 490 455 action = Action::Compress; 491 456 } else { 492 - return Err(io::Error::new( 493 - io::ErrorKind::Other, 494 - "Can't guess compressor to use", 495 - )); 457 + return Err(io::Error::other("Can't guess compressor to use")); 496 458 } 497 - } else if compressor.as_ref().unwrap().name() 498 - == get_compressor_from_filename(path).unwrap().name() 499 - { 500 - action = Action::Compress; 501 - } else { 502 - action = Action::Extract; 503 459 } 504 460 } 505 461 (CmprssInput::Pipe(_), CmprssOutput::Pipe(_)) => { ··· 520 476 } 521 477 } 522 478 523 - if compressor.is_none() { 524 - return Err(io::Error::new( 525 - io::ErrorKind::Other, 526 - "Could not determine compressor to use", 527 - )); 528 - } 479 + let compressor = 480 + compressor.ok_or_else(|| io::Error::other("Could not determine compressor to use"))?; 529 481 if action == Action::Unknown { 530 - return Err(io::Error::new( 531 - io::ErrorKind::Other, 532 - "Could not determine action to take", 533 - )); 482 + return Err(io::Error::other("Could not determine action to take")); 534 483 } 535 484 536 485 Ok(Job { 537 - compressor: compressor.unwrap(), 486 + compressor, 538 487 input: cmprss_input, 539 488 output: cmprss_output, 540 489 action, ··· 548 497 Action::Compress => job.compressor.compress(job.input, job.output)?, 549 498 Action::Extract => job.compressor.extract(job.input, job.output)?, 550 499 _ => { 551 - return Err(io::Error::new( 552 - io::ErrorKind::Other, 553 - "Unknown action requested", 554 - )); 500 + return Err(io::Error::other("Unknown action requested")); 555 501 } 556 502 }; 557 503
+1 -1
src/utils.rs
··· 228 228 } 229 229 230 230 pub fn cmprss_error(message: &str) -> Result<(), io::Error> { 231 - Err(io::Error::new(io::ErrorKind::Other, message)) 231 + Err(io::Error::other(message)) 232 232 } 233 233 234 234 /// Wrapper for Read + Send to allow Debug