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
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
//! Async builder for creating AsyncIoEngine instances.

use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::config::{
    AsyncInputSpec, AsyncOutputSpec, FileExistsPolicy, InputConfig, OutputConfig, PipelineConfig,
};
use crate::engine_async::AsyncIoEngine;
use crate::error::{AggregateError, ErrorPolicy, SingleIoError, Stage};
#[cfg(feature = "custom")]
use crate::format::CustomFormat;
use crate::format::{
    AsyncFormatRegistry, DEFAULT_FORMAT_ORDER, FormatKind, FormatRegistry, default_async_registry,
    default_registry,
};
use crate::io::{
    AsyncFileInput, AsyncFileOutput, AsyncInMemorySource, AsyncInputProvider, AsyncOutputTarget,
    AsyncStderrOutput, AsyncStdinInput, AsyncStdoutOutput,
};

pub struct MultiioAsyncBuilder {
    input_args: Vec<String>,
    output_args: Vec<String>,
    input_specs: Vec<AsyncInputSpec>,
    output_specs: Vec<AsyncOutputSpec>,
    registry: AsyncFormatRegistry,
    sync_registry: FormatRegistry,
    error_policy: ErrorPolicy,
    default_input_formats: Vec<FormatKind>,
    default_output_formats: Vec<FormatKind>,
    file_exists_policy: FileExistsPolicy,
}

impl Default for MultiioAsyncBuilder {
    fn default() -> Self {
        MultiioAsyncBuilder::new(default_async_registry())
    }
}

impl MultiioAsyncBuilder {
    pub fn new(registry: AsyncFormatRegistry) -> Self {
        Self {
            input_args: Vec::new(),
            output_args: Vec::new(),
            input_specs: Vec::new(),
            output_specs: Vec::new(),
            registry,
            sync_registry: default_registry(),
            error_policy: ErrorPolicy::Accumulate,
            default_input_formats: DEFAULT_FORMAT_ORDER.to_vec(),
            default_output_formats: DEFAULT_FORMAT_ORDER.to_vec(),
            file_exists_policy: FileExistsPolicy::Overwrite,
        }
    }

    /// Replace the underlying sync `FormatRegistry` used for decoding,
    /// encoding, and streaming.
    pub fn with_sync_registry(mut self, registry: FormatRegistry) -> Self {
        self.sync_registry = registry;
        self
    }

    /// Register a custom format on the sync registry and mark it available on
    /// the async registry.
    ///
    /// This ensures that custom formats participate fully in decode/encode and
    /// streaming via the sync `FormatRegistry`, while the async registry is
    /// aware of the corresponding `FormatKind::Custom` for feature gating or
    /// extension-based resolution paths.
    #[cfg(feature = "custom")]
    pub fn with_custom_format(mut self, format: CustomFormat) -> Self {
        // Register the custom format in the sync registry so that
        // `deserialize_value`, `serialize_value`, and
        // `stream_deserialize_into` can use it.
        self.sync_registry.register_custom(format.clone());

        // Also mark the custom kind as available in the async registry.
        self.registry.register(FormatKind::Custom(format.name));

        self
    }

    pub fn inputs_from_args(mut self, args: &[String]) -> Self {
        self.input_args = args.to_vec();
        self
    }

    pub fn with_input_args(self, args: &crate::cli::InputArgs) -> Self {
        self.inputs_from_args(args.as_slice())
    }

    pub fn outputs_from_args(mut self, args: &[String]) -> Self {
        self.output_args = args.to_vec();
        self
    }

    pub fn with_output_args(self, args: &crate::cli::OutputArgs) -> Self {
        self.outputs_from_args(args.as_slice())
    }

    pub fn add_input(mut self, arg: impl Into<String>) -> Self {
        self.input_args.push(arg.into());
        self
    }

    pub fn add_output(mut self, arg: impl Into<String>) -> Self {
        self.output_args.push(arg.into());
        self
    }

    pub fn add_input_spec(mut self, spec: AsyncInputSpec) -> Self {
        self.input_specs.push(spec);
        self
    }

    pub fn add_output_spec(mut self, spec: AsyncOutputSpec) -> Self {
        self.output_specs.push(spec);
        self
    }

    pub fn with_order(mut self, order: &[FormatKind]) -> Self {
        self.default_input_formats = order.to_vec();
        self.default_output_formats = order.to_vec();
        self
    }

    pub fn with_input_order(mut self, order: &[FormatKind]) -> Self {
        self.default_input_formats = order.to_vec();
        self
    }

    pub fn with_output_order(mut self, order: &[FormatKind]) -> Self {
        self.default_output_formats = order.to_vec();
        self
    }

    pub fn with_mode(mut self, policy: ErrorPolicy) -> Self {
        self.error_policy = policy;
        self
    }

    pub fn with_file_exists_policy(mut self, policy: FileExistsPolicy) -> Self {
        self.file_exists_policy = policy;
        self
    }

    pub fn build(self) -> Result<AsyncIoEngine, AggregateError> {
        let mut inputs = self.resolve_inputs()?;
        let mut outputs = self.resolve_outputs()?;

        // Add pre-built specs
        inputs.extend(self.input_specs);
        outputs.extend(self.output_specs);

        Ok(AsyncIoEngine::new_with_sync_registry(
            self.registry,
            self.sync_registry,
            self.error_policy,
            inputs,
            outputs,
        ))
    }

    fn resolve_inputs(&self) -> Result<Vec<AsyncInputSpec>, AggregateError> {
        let mut specs = Vec::with_capacity(self.input_args.len());
        let mut errors = Vec::new();

        for raw in &self.input_args {
            match self.resolve_single_input(raw) {
                Ok(spec) => specs.push(spec),
                Err(e) => {
                    errors.push(e);
                    if matches!(self.error_policy, ErrorPolicy::FastFail) {
                        return Err(AggregateError { errors });
                    }
                }
            }
        }

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

    fn resolve_single_input(&self, raw: &str) -> Result<AsyncInputSpec, SingleIoError> {
        let raw = raw.trim();

        if let Some(path) = raw.strip_prefix('@') {
            if path.is_empty() {
                return Err(SingleIoError {
                    stage: Stage::ResolveInput,
                    target: raw.to_string(),
                    error: Box::new(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "expected a path after '@'",
                    )),
                });
            }

            let path = PathBuf::from(path);
            let provider: Arc<dyn AsyncInputProvider> = Arc::new(AsyncFileInput::new(path.clone()));
            let explicit = self.infer_format_from_path(&path);

            return Ok(AsyncInputSpec {
                raw: path.to_string_lossy().into_owned(),
                provider,
                explicit_format: explicit,
                format_candidates: self.default_input_formats.clone(),
            });
        }

        if raw == "-" || raw.eq_ignore_ascii_case("stdin") {
            return Ok(AsyncInputSpec {
                raw: "-".to_string(),
                provider: Arc::new(AsyncStdinInput::new()),
                explicit_format: None,
                format_candidates: self.default_input_formats.clone(),
            });
        }

        if let Some(content) = raw.strip_prefix('=') {
            use std::hash::{Hash, Hasher};

            let mut hasher = std::collections::hash_map::DefaultHasher::new();
            content.hash(&mut hasher);
            let id = format!("inline:{:016x}", hasher.finish());

            let provider: Arc<dyn AsyncInputProvider> = Arc::new(AsyncInMemorySource::from_string(
                id.clone(),
                content.to_string(),
            ));

            return Ok(AsyncInputSpec {
                raw: id,
                provider,
                explicit_format: None,
                format_candidates: self.default_input_formats.clone(),
            });
        }

        let path = PathBuf::from(raw);
        let provider: Arc<dyn AsyncInputProvider> = Arc::new(AsyncFileInput::new(path.clone()));
        let explicit = self.infer_format_from_path(&path);

        Ok(AsyncInputSpec {
            raw: raw.to_string(),
            provider,
            explicit_format: explicit,
            format_candidates: self.default_input_formats.clone(),
        })
    }

    fn resolve_outputs(&self) -> Result<Vec<AsyncOutputSpec>, AggregateError> {
        let mut specs = Vec::with_capacity(self.output_args.len());
        let mut errors = Vec::new();

        for raw in &self.output_args {
            match self.resolve_single_output(raw) {
                Ok(spec) => specs.push(spec),
                Err(e) => {
                    errors.push(e);
                    if matches!(self.error_policy, ErrorPolicy::FastFail) {
                        return Err(AggregateError { errors });
                    }
                }
            }
        }

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

    fn resolve_single_output(&self, raw: &str) -> Result<AsyncOutputSpec, SingleIoError> {
        let raw = raw.trim();

        if let Some(path) = raw.strip_prefix('@') {
            if path.is_empty() {
                return Err(SingleIoError {
                    stage: Stage::ResolveOutput,
                    target: raw.to_string(),
                    error: Box::new(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "expected a path after '@'",
                    )),
                });
            }

            let path = PathBuf::from(path);
            let target: Arc<dyn AsyncOutputTarget> = Arc::new(AsyncFileOutput::new(path.clone()));
            let explicit = self.infer_format_from_path(&path);

            return Ok(AsyncOutputSpec {
                raw: path.to_string_lossy().into_owned(),
                target,
                explicit_format: explicit,
                format_candidates: self.default_output_formats.clone(),
                file_exists_policy: self.file_exists_policy,
            });
        }

        if raw == "-" || raw.eq_ignore_ascii_case("stdout") {
            return Ok(AsyncOutputSpec {
                raw: "-".to_string(),
                target: Arc::new(AsyncStdoutOutput::new()),
                explicit_format: None,
                format_candidates: self.default_output_formats.clone(),
                file_exists_policy: self.file_exists_policy,
            });
        }

        if raw.eq_ignore_ascii_case("stderr") {
            return Ok(AsyncOutputSpec {
                raw: "stderr".to_string(),
                target: Arc::new(AsyncStderrOutput::new()),
                explicit_format: None,
                format_candidates: self.default_output_formats.clone(),
                file_exists_policy: self.file_exists_policy,
            });
        }

        let path = PathBuf::from(raw);
        let target: Arc<dyn AsyncOutputTarget> = Arc::new(AsyncFileOutput::new(path.clone()));

        let explicit = self.infer_format_from_path(&path);

        Ok(AsyncOutputSpec {
            raw: raw.to_string(),
            target,
            explicit_format: explicit,
            format_candidates: self.default_output_formats.clone(),
            file_exists_policy: self.file_exists_policy,
        })
    }

    fn infer_format_from_path(&self, path: &Path) -> Option<FormatKind> {
        path.extension()
            .and_then(|s| s.to_str())
            .map(|s| s.to_ascii_lowercase())
            .as_deref()
            .and_then(|ext| self.registry.kind_for_extension(ext))
    }

    pub fn from_pipeline_config(
        config: PipelineConfig,
        registry: AsyncFormatRegistry,
    ) -> Result<Self, AggregateError> {
        let mut builder = MultiioAsyncBuilder::new(registry);

        if let Some(policy_str) = config.error_policy.as_deref() {
            let policy = match policy_str {
                "fast_fail" | "fastfail" => ErrorPolicy::FastFail,
                _ => ErrorPolicy::Accumulate,
            };
            builder = builder.with_mode(policy);
        }

        if let Some(order) = config.format_order.as_ref() {
            let kinds: Vec<FormatKind> = order
                .iter()
                .filter_map(|s| s.parse::<FormatKind>().ok())
                .collect();
            builder = builder.with_order(&kinds);
        }

        let mut errors = Vec::new();

        for input_cfg in config.inputs {
            match builder.input_from_config(&input_cfg) {
                Ok(spec) => builder.input_specs.push(spec),
                Err(e) => {
                    errors.push(e);
                    if matches!(builder.error_policy, ErrorPolicy::FastFail) {
                        return Err(AggregateError { errors });
                    }
                }
            }
        }

        for output_cfg in config.outputs {
            match builder.output_from_config(&output_cfg) {
                Ok(spec) => builder.output_specs.push(spec),
                Err(e) => {
                    errors.push(e);
                    if matches!(builder.error_policy, ErrorPolicy::FastFail) {
                        return Err(AggregateError { errors });
                    }
                }
            }
        }

        if !errors.is_empty() {
            return Err(AggregateError { errors });
        }

        Ok(builder)
    }

    fn input_from_config(&self, cfg: &InputConfig) -> Result<AsyncInputSpec, SingleIoError> {
        let provider: Arc<dyn AsyncInputProvider> = match cfg.kind.as_str() {
            "stdin" | "-" => Arc::new(AsyncStdinInput::new()),
            "file" => {
                let path = cfg.path.as_ref().ok_or_else(|| SingleIoError {
                    stage: Stage::ResolveInput,
                    target: cfg.id.clone(),
                    error: Box::new(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "file input requires 'path' field",
                    )),
                })?;
                Arc::new(AsyncFileInput::new(PathBuf::from(path)))
            }
            other => {
                return Err(SingleIoError {
                    stage: Stage::ResolveInput,
                    target: cfg.id.clone(),
                    error: Box::new(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        format!("unknown input kind: {}", other),
                    )),
                });
            }
        };

        let explicit_format = cfg
            .format
            .as_ref()
            .and_then(|s| s.parse::<FormatKind>().ok());

        Ok(AsyncInputSpec {
            raw: cfg.id.clone(),
            provider,
            explicit_format,
            format_candidates: self.default_input_formats.clone(),
        })
    }

    fn output_from_config(&self, cfg: &OutputConfig) -> Result<AsyncOutputSpec, SingleIoError> {
        let target: Arc<dyn AsyncOutputTarget> = match cfg.kind.as_str() {
            "stdout" | "-" => Arc::new(AsyncStdoutOutput::new()),
            "stderr" => Arc::new(AsyncStderrOutput::new()),
            "file" => {
                let path = cfg.path.as_ref().ok_or_else(|| SingleIoError {
                    stage: Stage::ResolveOutput,
                    target: cfg.id.clone(),
                    error: Box::new(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "file output requires 'path' field",
                    )),
                })?;
                Arc::new(AsyncFileOutput::new(PathBuf::from(path)))
            }
            other => {
                return Err(SingleIoError {
                    stage: Stage::ResolveOutput,
                    target: cfg.id.clone(),
                    error: Box::new(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        format!("unknown output kind: {}", other),
                    )),
                });
            }
        };

        let explicit_format = cfg
            .format
            .as_ref()
            .and_then(|s| s.parse::<FormatKind>().ok());

        let file_exists_policy = cfg
            .file_exists_policy
            .as_ref()
            .and_then(|s| s.parse::<FileExistsPolicy>().ok())
            .unwrap_or(self.file_exists_policy);

        Ok(AsyncOutputSpec {
            raw: cfg.id.clone(),
            target,
            explicit_format,
            format_candidates: self.default_output_formats.clone(),
            file_exists_policy,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::format::{DEFAULT_FORMAT_ORDER, FormatKind, default_async_registry};

    #[test]
    fn async_builder_defaults_match_default_format_order() {
        let builder = MultiioAsyncBuilder::new(default_async_registry());
        assert_eq!(builder.default_input_formats, DEFAULT_FORMAT_ORDER);
        assert_eq!(builder.default_output_formats, DEFAULT_FORMAT_ORDER);
    }

    #[test]
    fn resolve_single_input_supports_stdin_alias_and_inline_content() {
        let builder = MultiioAsyncBuilder::default();

        let stdin = builder.resolve_single_input("stdin").expect("stdin spec");
        assert_eq!(stdin.raw, "-");
        assert_eq!(stdin.provider.id(), "-");
        assert!(stdin.explicit_format.is_none());

        let inline = builder.resolve_single_input("=hello").expect("inline spec");
        assert!(inline.raw.starts_with("inline:"));
        assert_eq!(inline.provider.id(), inline.raw);
        assert!(inline.explicit_format.is_none());

        let forced_path = builder
            .resolve_single_input("@file.txt")
            .expect("forced path spec");
        assert_eq!(forced_path.raw, "file.txt");
        assert_eq!(forced_path.provider.id(), "file.txt");
        assert_eq!(forced_path.explicit_format, Some(FormatKind::Plaintext));
    }

    #[test]
    fn resolve_single_output_supports_stdout_alias_stderr_and_forced_path() {
        let builder = MultiioAsyncBuilder::default();

        let stdout = builder
            .resolve_single_output("stdout")
            .expect("stdout spec");
        assert_eq!(stdout.raw, "-");
        assert_eq!(stdout.target.id(), "-");
        assert!(stdout.explicit_format.is_none());

        let stderr = builder
            .resolve_single_output("stderr")
            .expect("stderr spec");
        assert_eq!(stderr.raw, "stderr");
        assert_eq!(stderr.target.id(), "stderr");
        assert!(stderr.explicit_format.is_none());

        let forced_path = builder
            .resolve_single_output("@out.txt")
            .expect("forced path spec");
        assert_eq!(forced_path.raw, "out.txt");
        assert_eq!(forced_path.target.id(), "out.txt");
        assert_eq!(forced_path.explicit_format, Some(FormatKind::Plaintext));
    }
}