Skip to main content

arrow_csv/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Transfer data between the [Apache Arrow] memory format and CSV (comma-separated values).
6//!
7//! [Apache Arrow]: https://arrow.apache.org/
8//!
9//! # Platform Support
10//!
11//! Only little-endian platforms are officially supported and tested in CI.
12//! Big-endian platforms are not tested in CI and may not work correctly.
13//! Fixes for big-endian platforms are welcome and handled on a best-effort basis,
14//! but compatibility is not guaranteed.
15
16#![doc(
17    html_logo_url = "https://arrow.apache.org/img/arrow-logo_chevrons_black-txt_white-bg.svg",
18    html_favicon_url = "https://arrow.apache.org/img/arrow-logo_chevrons_black-txt_transparent-bg.svg"
19)]
20#![cfg_attr(docsrs, feature(doc_cfg))]
21#![warn(missing_docs)]
22
23pub mod reader;
24pub mod writer;
25
26pub use self::reader::Reader;
27pub use self::reader::ReaderBuilder;
28pub use self::reader::infer_schema_from_files;
29pub use self::writer::QuoteStyle;
30pub use self::writer::Writer;
31pub use self::writer::WriterBuilder;
32use arrow_schema::ArrowError;
33
34fn map_csv_error(error: csv::Error) -> ArrowError {
35    match error.kind() {
36        csv::ErrorKind::Io(error) => ArrowError::CsvError(error.to_string()),
37        csv::ErrorKind::Utf8 { pos, err } => ArrowError::CsvError(format!(
38            "Encountered UTF-8 error while reading CSV file: {}{}",
39            err,
40            pos.as_ref()
41                .map(|pos| format!(" at line {}", pos.line()))
42                .unwrap_or_default(),
43        )),
44        csv::ErrorKind::UnequalLengths {
45            pos,
46            expected_len,
47            len,
48        } => ArrowError::CsvError(format!(
49            "Encountered unequal lengths between records on CSV file. Expected {} \
50                 records, found {} records{}",
51            expected_len,
52            len,
53            pos.as_ref()
54                .map(|pos| format!(" at line {}", pos.line()))
55                .unwrap_or_default(),
56        )),
57        _ => ArrowError::CsvError("Error reading CSV file".to_string()),
58    }
59}