multiio 0.2.3

A unified I/O orchestration library for CLI/server applications
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
//! Synchronous I/O engine for orchestrating read and write operations.

use std::io::Read;

use serde::{Serialize, de::DeserializeOwned};

use crate::config::{FileExistsPolicy, InputSpec, OutputSpec};
use crate::error::{AggregateError, ErrorPolicy, SingleIoError, Stage};
use crate::format::FormatRegistry;

/// Synchronous I/O engine for orchestrating multi-input/multi-output operations.
pub struct IoEngine {
    registry: FormatRegistry,
    error_policy: ErrorPolicy,
    inputs: Vec<InputSpec>,
    outputs: Vec<OutputSpec>,
}

impl IoEngine {
    /// Create a new I/O engine.
    pub fn new(
        registry: FormatRegistry,
        error_policy: ErrorPolicy,
        inputs: Vec<InputSpec>,
        outputs: Vec<OutputSpec>,
    ) -> Self {
        Self {
            registry,
            error_policy,
            inputs,
            outputs,
        }
    }

    /// Get the format registry.
    pub fn registry(&self) -> &FormatRegistry {
        &self.registry
    }

    /// Get the error policy.
    pub fn error_policy(&self) -> ErrorPolicy {
        self.error_policy
    }

    /// Get the input specifications.
    pub fn inputs(&self) -> &[InputSpec] {
        &self.inputs
    }

    /// Get the output specifications.
    pub fn outputs(&self) -> &[OutputSpec] {
        &self.outputs
    }

    /// Read all inputs and deserialize each into type T.
    ///
    /// Returns a vector of deserialized values, one for each input.
    /// If error_policy is FastFail, stops at the first error.
    /// If error_policy is Accumulate, collects all errors.
    pub fn read_all<T>(&self) -> Result<Vec<T>, AggregateError>
    where
        T: DeserializeOwned,
    {
        let mut results = Vec::with_capacity(self.inputs.len());
        let mut errors = Vec::new();
        let mut buffer = Vec::new();

        for spec in &self.inputs {
            match self.read_one_with_buffer::<T>(spec, &mut buffer) {
                Ok(value) => results.push(value),
                Err(e) => {
                    errors.push(e);
                    if matches!(self.error_policy, ErrorPolicy::FastFail) {
                        return Err(AggregateError { errors });
                    }
                }
            }
        }

        if errors.is_empty() {
            Ok(results)
        } else {
            Err(AggregateError { errors })
        }
    }

    /// Read a single input and deserialize into type T.
    fn read_one<T>(&self, spec: &InputSpec) -> Result<T, SingleIoError>
    where
        T: DeserializeOwned,
    {
        let mut buffer = Vec::new();
        self.read_one_with_buffer::<T>(spec, &mut buffer)
    }

    fn read_one_with_buffer<T>(
        &self,
        spec: &InputSpec,
        buffer: &mut Vec<u8>,
    ) -> Result<T, SingleIoError>
    where
        T: DeserializeOwned,
    {
        // Open the input stream
        let mut reader = spec.provider.open().map_err(|e| SingleIoError {
            stage: Stage::Open,
            target: spec.raw.clone(),
            error: Box::new(e),
        })?;

        // Read all bytes into the reusable buffer
        buffer.clear();
        reader.read_to_end(buffer).map_err(|e| SingleIoError {
            stage: Stage::Open,
            target: spec.raw.clone(),
            error: Box::new(e),
        })?;

        // Deserialize (handles both built-in and custom formats)
        self.registry
            .deserialize_value::<T>(
                spec.explicit_format.as_ref(),
                &spec.format_candidates,
                buffer,
            )
            .map_err(|e| SingleIoError {
                stage: Stage::Parse,
                target: spec.raw.clone(),
                error: Box::new(e),
            })
    }

    /// Write values to all outputs.
    ///
    /// Each output receives the same serialized values.
    pub fn write_all<T>(&self, values: &[T]) -> Result<(), AggregateError>
    where
        T: Serialize,
    {
        let mut errors = Vec::new();

        for spec in &self.outputs {
            if let Err(e) = self.write_one(spec, values) {
                errors.push(e);
                if matches!(self.error_policy, ErrorPolicy::FastFail) {
                    return Err(AggregateError { errors });
                }
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(AggregateError { errors })
        }
    }

    /// Write a single value to all outputs.
    pub fn write_one_value<T>(&self, value: &T) -> Result<(), AggregateError>
    where
        T: Serialize,
    {
        let mut errors = Vec::new();

        for spec in &self.outputs {
            if let Err(e) = self.write_single(spec, value) {
                errors.push(e);
                if matches!(self.error_policy, ErrorPolicy::FastFail) {
                    return Err(AggregateError { errors });
                }
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(AggregateError { errors })
        }
    }

    /// Write values to a single output.
    fn write_one<T>(&self, spec: &OutputSpec, values: &[T]) -> Result<(), SingleIoError>
    where
        T: Serialize,
    {
        // Serialize to bytes (handles both built-in and custom formats)
        let bytes = self
            .registry
            .serialize_value::<&[T]>(
                spec.explicit_format.as_ref(),
                &spec.format_candidates,
                &values,
            )
            .map_err(|e| SingleIoError {
                stage: Stage::Serialize,
                target: spec.raw.clone(),
                error: Box::new(e),
            })?;

        // Open the output stream based on policy
        let mut writer = self.open_output(spec)?;

        // Write bytes
        std::io::Write::write_all(&mut *writer, &bytes).map_err(|e| SingleIoError {
            stage: Stage::Serialize,
            target: spec.raw.clone(),
            error: Box::new(e),
        })
    }

    /// Write a single value to a specific output.
    fn write_single<T>(&self, spec: &OutputSpec, value: &T) -> Result<(), SingleIoError>
    where
        T: Serialize,
    {
        // Serialize to bytes (handles both built-in and custom formats)
        let bytes = self
            .registry
            .serialize_value(
                spec.explicit_format.as_ref(),
                &spec.format_candidates,
                value,
            )
            .map_err(|e| SingleIoError {
                stage: Stage::Serialize,
                target: spec.raw.clone(),
                error: Box::new(e),
            })?;

        // Open the output stream based on policy
        let mut writer = self.open_output(spec)?;

        // Write bytes
        std::io::Write::write_all(&mut *writer, &bytes).map_err(|e| SingleIoError {
            stage: Stage::Serialize,
            target: spec.raw.clone(),
            error: Box::new(e),
        })
    }

    /// Open an output based on the file exists policy.
    fn open_output(
        &self,
        spec: &OutputSpec,
    ) -> Result<Box<dyn std::io::Write + Send>, SingleIoError> {
        let result = match spec.file_exists_policy {
            FileExistsPolicy::Overwrite => spec.target.open_overwrite(),
            FileExistsPolicy::Append => spec.target.open_append(),
            FileExistsPolicy::Error => {
                // For file outputs, check if file exists
                // For now, just use overwrite (can be enhanced)
                spec.target.open_overwrite()
            }
        };

        result.map_err(|e| SingleIoError {
            stage: Stage::Open,
            target: spec.raw.clone(),
            error: Box::new(e),
        })
    }

    /// Create an iterator that reads each input lazily.
    ///
    /// This allows processing inputs one at a time without loading all into memory.
    pub fn read_stream<T>(&self) -> impl Iterator<Item = Result<T, SingleIoError>> + '_
    where
        T: DeserializeOwned,
    {
        self.inputs.iter().map(|spec| self.read_one::<T>(spec))
    }

    /// Stream records from all inputs using their resolved formats.
    ///
    /// This uses format-specific streaming implementations when available
    /// (e.g. JSON NDJSON, CSV rows, custom streaming handlers), and falls
    /// back to non-streaming deserialization as a single item for formats
    /// that do not support multi-record streaming.
    pub fn read_records<T>(&self) -> impl Iterator<Item = Result<T, SingleIoError>> + '_
    where
        T: DeserializeOwned + 'static,
    {
        self.inputs
            .iter()
            .flat_map(move |spec| self.records_stream_for_spec::<T>(spec))
    }

    /// Stream JSON records from all inputs whose resolved format is JSON.
    ///
    /// Each top-level JSON value is deserialized into `T`. Errors are reported per-record
    /// as `SingleIoError` with appropriate stage and target information.
    #[cfg(feature = "json")]
    pub fn read_json_records<T>(&self) -> impl Iterator<Item = Result<T, SingleIoError>> + '_
    where
        T: DeserializeOwned + 'static,
    {
        self.inputs
            .iter()
            .flat_map(move |spec| self.json_stream_for_spec::<T>(spec))
    }

    /// Stream CSV records from all inputs whose resolved format is CSV.
    ///
    /// Each CSV record is deserialized into `T`. Errors are reported per-record
    /// as `SingleIoError` with appropriate stage and target information.
    #[cfg(feature = "csv")]
    pub fn read_csv_records<T>(&self) -> impl Iterator<Item = Result<T, SingleIoError>> + '_
    where
        T: DeserializeOwned + 'static,
    {
        self.inputs
            .iter()
            .flat_map(move |spec| self.csv_stream_for_spec::<T>(spec))
    }

    #[cfg(feature = "csv")]
    fn csv_stream_for_spec<T>(
        &self,
        spec: &InputSpec,
    ) -> Box<dyn Iterator<Item = Result<T, SingleIoError>> + '_>
    where
        T: DeserializeOwned + 'static,
    {
        // Resolve format first
        let kind = match self
            .registry
            .resolve(spec.explicit_format.as_ref(), &spec.format_candidates)
        {
            Ok(k) => k,
            Err(e) => {
                let err = SingleIoError {
                    stage: Stage::ResolveInput,
                    target: spec.raw.clone(),
                    error: Box::new(e),
                };
                return Box::new(std::iter::once(Err(err)));
            }
        };

        if kind != crate::format::FormatKind::Csv {
            let err = SingleIoError {
                stage: Stage::ResolveInput,
                target: spec.raw.clone(),
                error: Box::new(crate::format::FormatError::UnknownFormat(kind)),
            };
            return Box::new(std::iter::once(Err(err)));
        }

        // Open the input
        let reader = match spec.provider.open() {
            Ok(r) => r,
            Err(e) => {
                let err = SingleIoError {
                    stage: Stage::Open,
                    target: spec.raw.clone(),
                    error: Box::new(e),
                };
                return Box::new(std::iter::once(Err(err)));
            }
        };

        let target = spec.raw.clone();
        let iter = crate::format::deserialize_csv_stream::<T, _>(reader).map(move |res| {
            res.map_err(|e| SingleIoError {
                stage: Stage::Parse,
                target: target.clone(),
                error: Box::new(e),
            })
        });

        Box::new(iter)
    }

    fn records_stream_for_spec<T>(
        &self,
        spec: &InputSpec,
    ) -> Box<dyn Iterator<Item = Result<T, SingleIoError>> + '_>
    where
        T: DeserializeOwned + 'static,
    {
        // Open the input
        let reader = match spec.provider.open() {
            Ok(r) => r,
            Err(e) => {
                let err = SingleIoError {
                    stage: Stage::Open,
                    target: spec.raw.clone(),
                    error: Box::new(e),
                };
                return Box::new(std::iter::once(Err(err)));
            }
        };

        let target = spec.raw.clone();
        let iter_result = self.registry.stream_deserialize_into::<T>(
            spec.explicit_format.as_ref(),
            &spec.format_candidates,
            Box::new(reader),
        );

        let iter = match iter_result {
            Ok(iter) => iter,
            Err(e) => {
                let err = SingleIoError {
                    stage: Stage::Parse,
                    target: target.clone(),
                    error: Box::new(e),
                };
                return Box::new(std::iter::once(Err(err)));
            }
        };

        let mapped = iter.map(move |res| {
            res.map_err(|e| SingleIoError {
                stage: Stage::Parse,
                target: target.clone(),
                error: Box::new(e),
            })
        });

        Box::new(mapped)
    }

    #[cfg(feature = "json")]
    fn json_stream_for_spec<T>(
        &self,
        spec: &InputSpec,
    ) -> Box<dyn Iterator<Item = Result<T, SingleIoError>> + '_>
    where
        T: DeserializeOwned + 'static,
    {
        // Resolve format first
        let kind = match self
            .registry
            .resolve(spec.explicit_format.as_ref(), &spec.format_candidates)
        {
            Ok(k) => k,
            Err(e) => {
                let err = SingleIoError {
                    stage: Stage::ResolveInput,
                    target: spec.raw.clone(),
                    error: Box::new(e),
                };
                return Box::new(std::iter::once(Err(err)));
            }
        };

        if kind != crate::format::FormatKind::Json {
            let err = SingleIoError {
                stage: Stage::ResolveInput,
                target: spec.raw.clone(),
                error: Box::new(crate::format::FormatError::UnknownFormat(kind)),
            };
            return Box::new(std::iter::once(Err(err)));
        }

        // Open the input
        let reader = match spec.provider.open() {
            Ok(r) => r,
            Err(e) => {
                let err = SingleIoError {
                    stage: Stage::Open,
                    target: spec.raw.clone(),
                    error: Box::new(e),
                };
                return Box::new(std::iter::once(Err(err)));
            }
        };

        let target = spec.raw.clone();
        let iter = crate::format::deserialize_json_stream::<T, _>(reader).map(move |res| {
            res.map_err(|e| SingleIoError {
                stage: Stage::Parse,
                target: target.clone(),
                error: Box::new(e),
            })
        });

        Box::new(iter)
    }
}