usenet-dl 0.4.0

Highly configurable Usenet download manager library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! CLI-based PAR2 handler using external par2 binary

use super::parser::{ExitStatus, parse_par2_repair_output, parse_par2_verify_output};
use super::traits::{ParityCapabilities, ParityHandler, RepairResult, VerifyResult};
use async_trait::async_trait;
use std::path::{Path, PathBuf};
use tokio::process::Command;

/// CLI-based PAR2 handler using external par2 binary
///
/// This handler executes the external `par2` binary to perform verification
/// and repair operations. It provides full PAR2 functionality including both
/// verification and repair capabilities.
///
/// # Examples
///
/// ```no_run
/// use usenet_dl::parity::{CliParityHandler, ParityHandler};
/// use std::path::{Path, PathBuf};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Create with explicit path
/// let handler = CliParityHandler::new(PathBuf::from("/usr/bin/par2"));
///
/// // Or auto-discover from PATH
/// let handler = CliParityHandler::from_path()
///     .expect("par2 not found in PATH");
///
/// let result = handler.verify(Path::new("download.par2")).await?;
/// # Ok(())
/// # }
/// ```
pub struct CliParityHandler {
    binary_path: PathBuf,
}

impl CliParityHandler {
    /// Create a new CLI handler with an explicit binary path
    ///
    /// # Arguments
    ///
    /// * `binary_path` - Path to the par2 binary
    pub fn new(binary_path: PathBuf) -> Self {
        Self { binary_path }
    }

    /// Attempt to find par2 in PATH
    ///
    /// Uses the `which` crate to search for the `par2` binary in the system PATH.
    ///
    /// # Returns
    ///
    /// `Some(CliParityHandler)` if the binary is found, `None` otherwise.
    pub fn from_path() -> Option<Self> {
        which::which("par2").ok().map(Self::new)
    }
}

#[async_trait]
impl ParityHandler for CliParityHandler {
    async fn verify(&self, par2_file: &Path) -> crate::Result<VerifyResult> {
        let output = Command::new(&self.binary_path)
            .arg("v") // Verify
            .arg(par2_file)
            .output()
            .await
            .map_err(|e| crate::Error::ExternalTool(format!("Failed to execute par2: {}", e)))?;

        parse_par2_verify_output(
            &output.stdout,
            &output.stderr,
            ExitStatus::from(output.status.success()),
        )
    }

    async fn repair(&self, par2_file: &Path) -> crate::Result<RepairResult> {
        let output = Command::new(&self.binary_path)
            .arg("r") // Repair
            .arg(par2_file)
            .output()
            .await
            .map_err(|e| crate::Error::ExternalTool(format!("Failed to execute par2: {}", e)))?;

        parse_par2_repair_output(
            &output.stdout,
            &output.stderr,
            ExitStatus::from(output.status.success()),
        )
    }

    fn capabilities(&self) -> ParityCapabilities {
        ParityCapabilities {
            can_verify: true,
            can_repair: true,
        }
    }

    fn name(&self) -> &'static str {
        "cli-par2"
    }
}

// unwrap/expect are acceptable in tests for concise failure-on-error assertions
#[allow(clippy::unwrap_used, clippy::expect_used)]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_from_path_returns_none_for_nonexistent_binary() {
        // This test will pass as long as there's no binary named "nonexistent-par2-binary-xyz"
        let result = which::which("nonexistent-par2-binary-xyz");
        assert!(result.is_err());
    }

    #[test]
    fn test_from_path_binary_discovery() {
        // Test the from_path() method behavior
        // This test doesn't depend on whether par2 is actually installed

        // First, check if par2 exists in PATH using which directly
        let which_result = which::which("par2");

        // Now test from_path() behavior
        let from_path_result = CliParityHandler::from_path();

        match which_result {
            Ok(expected_path) => {
                // If which finds it, from_path should return Some
                assert!(
                    from_path_result.is_some(),
                    "from_path() should return Some when par2 is in PATH"
                );

                let handler = from_path_result.unwrap();
                assert_eq!(
                    handler.binary_path, expected_path,
                    "from_path() should use the path found by which"
                );

                // Verify the handler has correct capabilities
                let caps = handler.capabilities();
                assert!(caps.can_verify, "CLI handler should support verification");
                assert!(caps.can_repair, "CLI handler should support repair");
                assert_eq!(
                    handler.name(),
                    "cli-par2",
                    "CLI handler should have correct name"
                );
            }
            Err(_) => {
                // If which doesn't find it, from_path should return None
                assert!(
                    from_path_result.is_none(),
                    "from_path() should return None when par2 is not in PATH"
                );
            }
        }
    }

    #[test]
    fn test_from_path_consistency_with_which_crate() {
        // Verify that from_path() always returns results consistent with which::which()
        let which_result = which::which("par2");
        let from_path_result = CliParityHandler::from_path();

        // Both should agree on whether the binary exists
        assert_eq!(
            which_result.is_ok(),
            from_path_result.is_some(),
            "from_path() should return Some if and only if which::which() succeeds"
        );
    }

    // Integration tests that require actual par2 binary
    // Run with: cargo test --lib parity::cli -- --ignored

    #[tokio::test]
    #[ignore] // Requires par2 binary in PATH
    async fn test_verify_with_nonexistent_file() {
        let handler = match CliParityHandler::from_path() {
            Some(h) => h,
            None => {
                println!("Skipping test: par2 binary not found in PATH");
                return;
            }
        };

        let result = handler
            .verify(Path::new("/tmp/nonexistent-file.par2"))
            .await;

        // Should fail because the file doesn't exist
        assert!(result.is_err());
    }

    #[tokio::test]
    #[ignore] // Requires par2 binary in PATH
    async fn test_repair_with_nonexistent_file() {
        let handler = match CliParityHandler::from_path() {
            Some(h) => h,
            None => {
                println!("Skipping test: par2 binary not found in PATH");
                return;
            }
        };

        let result = handler
            .repair(Path::new("/tmp/nonexistent-file.par2"))
            .await;

        // Should fail because the file doesn't exist
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_verify_with_invalid_binary_path() {
        let handler = CliParityHandler::new(PathBuf::from("/nonexistent/path/to/par2"));

        let result = handler.verify(Path::new("test.par2")).await;

        // Should return ExternalTool error
        assert!(result.is_err());
        if let Err(e) = result {
            match e {
                crate::Error::ExternalTool(msg) => {
                    assert!(msg.contains("Failed to execute par2"));
                }
                _ => panic!("Expected ExternalTool error, got: {:?}", e),
            }
        }
    }

    #[tokio::test]
    async fn test_repair_with_invalid_binary_path() {
        let handler = CliParityHandler::new(PathBuf::from("/nonexistent/path/to/par2"));

        let result = handler.repair(Path::new("test.par2")).await;

        // Should return ExternalTool error
        assert!(result.is_err());
        if let Err(e) = result {
            match e {
                crate::Error::ExternalTool(msg) => {
                    assert!(msg.contains("Failed to execute par2"));
                }
                _ => panic!("Expected ExternalTool error, got: {:?}", e),
            }
        }
    }

    // Integration tests with real PAR2 files
    // These tests require the par2 binary in PATH and will create temporary test files
    // Run with: cargo test --lib parity::cli::tests::integration -- --ignored --nocapture

    #[tokio::test]
    #[ignore] // Requires par2 binary in PATH
    async fn integration_test_verify_intact_files() {
        use std::fs;
        use tempfile::TempDir;

        let handler = match CliParityHandler::from_path() {
            Some(h) => h,
            None => {
                println!("Skipping test: par2 binary not found in PATH");
                return;
            }
        };

        // Create temporary directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let test_file_path = temp_dir.path().join("test.txt");
        let par2_file_path = temp_dir.path().join("test.txt.par2");

        // Create test file with known content
        let test_content = b"Hello, PAR2! This is test content for verification.\n";
        fs::write(&test_file_path, test_content).expect("Failed to write test file");

        // Create PAR2 recovery data using external par2 binary
        let create_output = tokio::process::Command::new(&handler.binary_path)
            .arg("c") // Create
            .arg("-r10") // 10% recovery
            .arg(&test_file_path)
            .current_dir(temp_dir.path())
            .output()
            .await
            .expect("Failed to create PAR2 file");

        if !create_output.status.success() {
            panic!(
                "Failed to create PAR2 file: {}",
                String::from_utf8_lossy(&create_output.stderr)
            );
        }

        // Verify the intact files
        let result = handler.verify(&par2_file_path).await;

        assert!(result.is_ok(), "Verify should succeed for intact files");
        let verify_result = result.unwrap();

        assert!(
            verify_result.is_complete,
            "Files should be complete and intact"
        );
        assert_eq!(
            verify_result.damaged_blocks, 0,
            "No blocks should be damaged"
        );
        assert!(
            verify_result.recovery_blocks_available > 0,
            "Recovery blocks should be available"
        );
        assert!(
            verify_result.damaged_files.is_empty(),
            "No files should be damaged"
        );
        assert!(
            verify_result.missing_files.is_empty(),
            "No files should be missing"
        );
    }

    #[tokio::test]
    #[ignore] // Requires par2 binary in PATH
    async fn integration_test_verify_damaged_file() {
        use std::fs::{self, OpenOptions};
        use std::io::Write;
        use tempfile::TempDir;

        let handler = match CliParityHandler::from_path() {
            Some(h) => h,
            None => {
                println!("Skipping test: par2 binary not found in PATH");
                return;
            }
        };

        // Create temporary directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let test_file_path = temp_dir.path().join("test.txt");
        let par2_file_path = temp_dir.path().join("test.txt.par2");

        // Create test file with known content
        let test_content = b"Hello, PAR2! This is test content that will be damaged.\n";
        fs::write(&test_file_path, test_content).expect("Failed to write test file");

        // Create PAR2 recovery data
        let create_output = tokio::process::Command::new(&handler.binary_path)
            .arg("c")
            .arg("-r20") // 20% recovery for better repair chances
            .arg(&test_file_path)
            .current_dir(temp_dir.path())
            .output()
            .await
            .expect("Failed to create PAR2 file");

        if !create_output.status.success() {
            panic!(
                "Failed to create PAR2 file: {}",
                String::from_utf8_lossy(&create_output.stderr)
            );
        }

        // Damage the test file by corrupting some bytes
        {
            let mut file = OpenOptions::new()
                .write(true)
                .open(&test_file_path)
                .expect("Failed to open test file for corruption");

            // Overwrite the beginning with different content
            file.write_all(b"CORRUPTED DATA!!!")
                .expect("Failed to corrupt file");
        }

        // Verify the damaged file
        let result = handler.verify(&par2_file_path).await;

        assert!(
            result.is_ok(),
            "Verify should succeed even with damaged files"
        );
        let verify_result = result.unwrap();

        assert!(
            !verify_result.is_complete,
            "Files should not be complete (damaged)"
        );
        assert!(
            verify_result.damaged_blocks > 0,
            "Should detect damaged blocks"
        );
        assert!(
            verify_result.recovery_blocks_available > 0,
            "Recovery blocks should be available"
        );
        assert!(
            verify_result.repairable,
            "Damage should be repairable with available recovery data"
        );
    }

    #[tokio::test]
    #[ignore] // Requires par2 binary in PATH
    async fn integration_test_repair_damaged_file() {
        use std::fs::{self, OpenOptions};
        use std::io::Write;
        use tempfile::TempDir;

        let handler = match CliParityHandler::from_path() {
            Some(h) => h,
            None => {
                println!("Skipping test: par2 binary not found in PATH");
                return;
            }
        };

        // Create temporary directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let test_file_path = temp_dir.path().join("test.txt");
        let par2_file_path = temp_dir.path().join("test.txt.par2");

        // Create test file with known content
        let original_content =
            b"Hello, PAR2! This is test content that will be damaged and repaired.\n";
        fs::write(&test_file_path, original_content).expect("Failed to write test file");

        // Create PAR2 recovery data with sufficient redundancy
        let create_output = tokio::process::Command::new(&handler.binary_path)
            .arg("c")
            .arg("-r30") // 30% recovery for reliable repair
            .arg(&test_file_path)
            .current_dir(temp_dir.path())
            .output()
            .await
            .expect("Failed to create PAR2 file");

        if !create_output.status.success() {
            panic!(
                "Failed to create PAR2 file: {}",
                String::from_utf8_lossy(&create_output.stderr)
            );
        }

        // Damage the test file
        {
            let mut file = OpenOptions::new()
                .write(true)
                .open(&test_file_path)
                .expect("Failed to open test file for corruption");

            file.write_all(b"CORRUPTED!!!!")
                .expect("Failed to corrupt file");
        }

        // Repair the damaged file
        let result = handler.repair(&par2_file_path).await;

        assert!(result.is_ok(), "Repair should succeed");
        let repair_result = result.unwrap();

        assert!(repair_result.success, "Repair should be successful");
        assert!(
            !repair_result.repaired_files.is_empty() || repair_result.failed_files.is_empty(),
            "Should report repaired files or have no failed files"
        );
        assert!(
            repair_result.error.is_none(),
            "Should have no error message on success"
        );

        // Verify the file is now intact by reading and comparing content
        let repaired_content = fs::read(&test_file_path).expect("Failed to read repaired file");
        assert_eq!(
            repaired_content, original_content,
            "Repaired file should match original content"
        );
    }

    #[tokio::test]
    #[ignore] // Requires par2 binary in PATH
    async fn integration_test_verify_missing_file() {
        use std::fs;
        use tempfile::TempDir;

        let handler = match CliParityHandler::from_path() {
            Some(h) => h,
            None => {
                println!("Skipping test: par2 binary not found in PATH");
                return;
            }
        };

        // Create temporary directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let test_file_path = temp_dir.path().join("test.txt");
        let par2_file_path = temp_dir.path().join("test.txt.par2");

        // Create test file
        let test_content = b"This file will be deleted to test missing file detection.\n";
        fs::write(&test_file_path, test_content).expect("Failed to write test file");

        // Create PAR2 recovery data
        let create_output = tokio::process::Command::new(&handler.binary_path)
            .arg("c")
            .arg("-r10")
            .arg(&test_file_path)
            .current_dir(temp_dir.path())
            .output()
            .await
            .expect("Failed to create PAR2 file");

        if !create_output.status.success() {
            panic!(
                "Failed to create PAR2 file: {}",
                String::from_utf8_lossy(&create_output.stderr)
            );
        }

        // Delete the test file to simulate missing file
        fs::remove_file(&test_file_path).expect("Failed to delete test file");

        // Verify with missing file
        let result = handler.verify(&par2_file_path).await;

        assert!(
            result.is_ok(),
            "Verify should succeed and report missing file"
        );
        let verify_result = result.unwrap();

        assert!(
            !verify_result.is_complete,
            "Files should not be complete (file missing)"
        );
        assert!(
            !verify_result.missing_files.is_empty(),
            "Should detect missing files"
        );
        assert!(
            verify_result.repairable,
            "Missing file should be recoverable from PAR2 data"
        );
    }

    #[tokio::test]
    #[ignore] // Requires par2 binary in PATH
    async fn integration_test_repair_missing_file() {
        use std::fs;
        use tempfile::TempDir;

        let handler = match CliParityHandler::from_path() {
            Some(h) => h,
            None => {
                println!("Skipping test: par2 binary not found in PATH");
                return;
            }
        };

        // Create temporary directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let test_file_path = temp_dir.path().join("test.txt");
        let par2_file_path = temp_dir.path().join("test.txt.par2");

        // Create test file
        let original_content = b"This file will be deleted and then recovered from PAR2 data.\n";
        fs::write(&test_file_path, original_content).expect("Failed to write test file");

        // Create PAR2 recovery data with high redundancy
        let create_output = tokio::process::Command::new(&handler.binary_path)
            .arg("c")
            .arg("-r50") // 50% recovery to ensure we can restore the file
            .arg(&test_file_path)
            .current_dir(temp_dir.path())
            .output()
            .await
            .expect("Failed to create PAR2 file");

        if !create_output.status.success() {
            panic!(
                "Failed to create PAR2 file: {}",
                String::from_utf8_lossy(&create_output.stderr)
            );
        }

        // Delete the test file
        fs::remove_file(&test_file_path).expect("Failed to delete test file");

        // Repair should restore the missing file
        let result = handler.repair(&par2_file_path).await;

        assert!(result.is_ok(), "Repair should succeed");
        let repair_result = result.unwrap();

        assert!(
            repair_result.success,
            "Repair should successfully restore missing file"
        );
        assert!(
            repair_result.error.is_none(),
            "Should have no error on successful repair"
        );

        // Verify the file was restored with correct content
        assert!(
            test_file_path.exists(),
            "Test file should be restored after repair"
        );
        let restored_content = fs::read(&test_file_path).expect("Failed to read restored file");
        assert_eq!(
            restored_content, original_content,
            "Restored file should match original content"
        );
    }
}