Skip to main content

arrs/output/
mod.rs

1//! Output writers: CSV, JSONL, and table.
2//!
3//! Each writer consumes `RecordBatch`es whose schema has already been projected
4//! by the caller. Type formatting rules live in `value`. The factory
5//! `make_writer` dispatches `Format` → concrete writer; CLI-side stdout
6//! convenience lives in `commands::common::make_stdout_writer`.
7
8pub mod csv;
9pub mod jsonl;
10pub mod table;
11pub mod value;
12
13use std::io::Write;
14
15use arrow_array::RecordBatch;
16use arrow_schema::SchemaRef;
17
18use crate::Result;
19use crate::cli::{BinaryFormat, Format};
20use crate::output::table::TableStyle;
21
22pub trait RowWriter {
23    fn start(&mut self, schema: &SchemaRef) -> Result<()>;
24    fn write_batch(&mut self, batch: &RecordBatch) -> Result<()>;
25    fn finish(&mut self) -> Result<()>;
26}
27
28/// Construct a `RowWriter` for `format`, writing to `out`. `table_style`
29/// is consulted only for `Format::Table`; other formats ignore it.
30pub fn make_writer<'w, W: Write + 'w>(
31    format: Format,
32    binary_format: BinaryFormat,
33    table_style: TableStyle,
34    out: W,
35) -> Box<dyn RowWriter + 'w> {
36    match format {
37        Format::Csv => Box::new(csv::CsvRowWriter::new(out, binary_format)),
38        Format::Jsonl => Box::new(jsonl::JsonlRowWriter::new(out, binary_format)),
39        Format::Table => Box::new(table::TableRowWriter::new(out, binary_format, table_style)),
40    }
41}