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.zstor.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
impl CsvWriter
Sourcepub fn new<P: AsRef<Path>>(path: P) -> Result<Self>
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.zstor.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();Sourcepub fn with_compression<P: AsRef<Path>>(
path: P,
method: CompressionMethod,
level: u32,
) -> Result<Self>
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 pathmethod- 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();Sourcepub fn delimiter(self, delim: u8) -> Self
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';');Sourcepub fn quote_char(self, quote: u8) -> Self
pub fn quote_char(self, quote: u8) -> Self
Set custom quote character (builder pattern)
Sourcepub fn write_row<I, S>(&mut self, data: I) -> Result<()>
pub fn write_row<I, S>(&mut self, data: I) -> Result<()>
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();Sourcepub fn write_row_typed(&mut self, cells: &[CellValue]) -> Result<()>
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();Sourcepub fn write_rows_batch<I, R, S>(&mut self, rows: I) -> Result<()>
pub fn write_rows_batch<I, R, S>(&mut self, rows: I) -> Result<()>
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();Sourcepub fn save(self) -> Result<()>
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§
impl Freeze for CsvWriter
impl !RefUnwindSafe for CsvWriter
impl !Send for CsvWriter
impl !Sync for CsvWriter
impl Unpin for CsvWriter
impl !UnwindSafe for CsvWriter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more