CsvWriter

Struct CsvWriter 

Source
pub struct CsvWriter { /* private fields */ }
Expand description

CSV file writer with streaming capabilities and compression support

Writes CSV files row by row, streaming data directly to disk or compressed ZIP. Memory usage is constant (~5MB or less) regardless of dataset size.

§Examples

use excelstream::csv_writer::CsvWriter;

let mut writer = CsvWriter::new("output.csv").unwrap();
writer.write_row(&["Name", "Age", "City"]).unwrap();
writer.write_row(&["Alice", "30", "NYC"]).unwrap();
writer.save().unwrap();

§Compression

Auto-detects compression from file extension:

  • .csv → Uncompressed
  • .csv.zst or .csv.zip → Zstd compression (level 3)
  • .csv.gz → Deflate/Gzip compression (level 6)
use excelstream::csv_writer::CsvWriter;
use excelstream::csv::CompressionMethod;

// Auto-detect from extension
let mut writer = CsvWriter::new("data.csv.zst").unwrap();

// Or explicit compression
let mut writer = CsvWriter::with_compression(
    "data.csv.zst",
    CompressionMethod::Zstd,
    3
).unwrap();

Implementations§

Source§

impl CsvWriter

Source

pub fn new<P: AsRef<Path>>(path: P) -> Result<Self>

Create a new CSV writer - auto-detects compression from file extension

§File Extensions
  • .csv → Uncompressed
  • .csv.zst or .csv.zip → Zstd compression (level 3)
  • .csv.gz → Deflate compression (level 6)
§Examples
use excelstream::csv_writer::CsvWriter;

// Plain CSV
let mut writer = CsvWriter::new("data.csv").unwrap();

// Zstd compressed
let mut writer = CsvWriter::new("data.csv.zst").unwrap();

// Gzip compressed
let mut writer = CsvWriter::new("data.csv.gz").unwrap();
Source

pub fn with_compression<P: AsRef<Path>>( path: P, method: CompressionMethod, level: u32, ) -> Result<Self>

Create a writer with explicit compression method and level

§Arguments
  • path - Output file path
  • method - Compression method (Zstd or Deflate)
  • level - Compression level:
    • Zstd: 1-21 (recommend 3 for balanced)
    • Deflate: 0-9 (recommend 6 for balanced)
§Examples
use excelstream::csv_writer::CsvWriter;
use excelstream::csv::CompressionMethod;

// Maximum Zstd compression
let mut writer = CsvWriter::with_compression(
    "data.csv.zst",
    CompressionMethod::Zstd,
    9
).unwrap();
Source

pub fn delimiter(self, delim: u8) -> Self

Set custom delimiter (builder pattern)

§Examples
use excelstream::csv_writer::CsvWriter;

let mut writer = CsvWriter::new("data.csv")
    .unwrap()
    .delimiter(b';');
Source

pub fn quote_char(self, quote: u8) -> Self

Set custom quote character (builder pattern)

Source

pub fn write_row<I, S>(&mut self, data: I) -> Result<()>
where I: IntoIterator<Item = S>, S: AsRef<str>,

Write a row of strings

§Examples
use excelstream::csv_writer::CsvWriter;

let mut writer = CsvWriter::new("data.csv").unwrap();
writer.write_row(&["Name", "Age", "City"]).unwrap();
writer.write_row(&["Alice", "30", "NYC"]).unwrap();
writer.save().unwrap();
Source

pub fn write_row_typed(&mut self, cells: &[CellValue]) -> Result<()>

Write a row of typed values

Converts CellValue types to strings before writing.

§Examples
use excelstream::csv_writer::CsvWriter;
use excelstream::types::CellValue;

let mut writer = CsvWriter::new("data.csv").unwrap();
writer.write_row_typed(&[
    CellValue::String("Alice".to_string()),
    CellValue::Int(30),
    CellValue::Float(75.5),
]).unwrap();
Source

pub fn write_rows_batch<I, R, S>(&mut self, rows: I) -> Result<()>
where I: IntoIterator<Item = R>, R: IntoIterator<Item = S>, S: AsRef<str>,

Write multiple rows at once

§Examples
use excelstream::csv_writer::CsvWriter;

let mut writer = CsvWriter::new("data.csv").unwrap();
let rows = vec![
    vec!["Alice", "30"],
    vec!["Bob", "25"],
];
writer.write_rows_batch(rows).unwrap();
Source

pub fn row_count(&self) -> u64

Get the number of rows written

Source

pub fn save(self) -> Result<()>

Finalize and save the CSV file

This must be called to properly close the file. Consumes the writer.

§Examples
use excelstream::csv_writer::CsvWriter;

let mut writer = CsvWriter::new("data.csv").unwrap();
writer.write_row(&["Name", "Age"]).unwrap();
writer.save().unwrap();

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.