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
//! Parallel processing utilities
//!
//! This module provides utilities for processing multiple files in parallel.
//! When the `parallel` feature is enabled, files are processed using Rayon's
//! parallel iterators. Otherwise, files are processed sequentially.
//!
//! # Examples
//!
//! ```no_run
//! use omniparse::core::Extractor;
//! use omniparse::utils::parallel::process_files_parallel;
//!
//! let extractor = Extractor::new();
//! let files = vec!["file1.pdf", "file2.docx", "file3.txt"];
//!
//! let results = process_files_parallel(&extractor, &files);
//!
//! for file_result in results {
//! match file_result.result {
//! Ok(extraction) => {
//! println!("{}: {} ({})",
//! file_result.path,
//! extraction.mime_type,
//! extraction.detection_confidence
//! );
//! }
//! Err(e) => {
//! eprintln!("{}: Error - {}", file_result.path, e);
//! }
//! }
//! }
//! ```
use crate;
use crateExtractionResult;
use Path;
use *;
/// Result of processing a single file in a batch
///
/// This structure contains the file path and the extraction result or error
/// for that file. It's used when processing multiple files to track which
/// files succeeded and which failed.
///
/// # Examples
///
/// ```no_run
/// use omniparse::utils::parallel::FileResult;
///
/// fn print_result(file_result: &FileResult) {
/// match &file_result.result {
/// Ok(extraction) => {
/// println!("{}: Success - {}", file_result.path, extraction.mime_type);
/// }
/// Err(e) => {
/// println!("{}: Error - {}", file_result.path, e);
/// }
/// }
/// }
/// ```
/// Process multiple files in parallel
///
/// When the `parallel` feature is enabled, this function uses Rayon to process
/// files in parallel across multiple threads. Each file is processed independently,
/// and errors in one file don't affect the processing of others.
///
/// **Note:** This function requires the `parallel` feature to be enabled for
/// true parallel processing. Without the feature, files are processed sequentially.
///
/// # Arguments
///
/// * `extractor` - The extractor to use for processing
/// * `paths` - Slice of file paths to process
///
/// # Returns
///
/// A vector of `FileResult` containing the result for each file.
///
/// # Examples
///
/// ```no_run
/// use omniparse::core::Extractor;
/// use omniparse::utils::parallel::process_files_parallel;
///
/// let extractor = Extractor::new();
/// let files = vec!["doc1.pdf", "doc2.docx", "doc3.txt"];
///
/// let results = process_files_parallel(&extractor, &files);
///
/// let success_count = results.iter().filter(|r| r.result.is_ok()).count();
/// println!("Successfully processed {} out of {} files", success_count, files.len());
/// ```
/// Process multiple files sequentially (fallback when parallel feature is disabled)
///
/// This is the fallback implementation used when the `parallel` feature is not enabled.
/// It processes files one at a time in order.
/// Process multiple files sequentially
///
/// This function always processes files sequentially, regardless of whether
/// the `parallel` feature is enabled. Use this when you need deterministic
/// ordering or when parallel processing is not desired.
///
/// # Arguments
///
/// * `extractor` - The extractor to use for processing
/// * `paths` - Slice of file paths to process
///
/// # Returns
///
/// A vector of `FileResult` containing the result for each file, in order.
///
/// # Examples
///
/// ```no_run
/// use omniparse::core::Extractor;
/// use omniparse::utils::parallel::process_files_sequential;
///
/// let extractor = Extractor::new();
/// let files = vec!["file1.txt", "file2.txt", "file3.txt"];
///
/// let results = process_files_sequential(&extractor, &files);
///
/// for (i, file_result) in results.iter().enumerate() {
/// println!("File {}: {}", i + 1, file_result.path);
/// }
/// ```