Skip to main content

arrow_csv/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Transfer data between the [Apache Arrow] memory format and CSV (comma-separated values).
19//!
20//! [Apache Arrow]: https://arrow.apache.org/
21//!
22//! # Platform Support
23//!
24//! Only little-endian platforms are officially supported and tested in CI.
25//! Big-endian platforms are not tested in CI and may not work correctly.
26//! Fixes for big-endian platforms are welcome and handled on a best-effort basis,
27//! but compatibility is not guaranteed.
28
29#![doc(
30    html_logo_url = "https://arrow.apache.org/img/arrow-logo_chevrons_black-txt_white-bg.svg",
31    html_favicon_url = "https://arrow.apache.org/img/arrow-logo_chevrons_black-txt_transparent-bg.svg"
32)]
33#![cfg_attr(docsrs, feature(doc_cfg))]
34#![warn(missing_docs)]
35
36pub mod reader;
37pub mod writer;
38
39pub use self::reader::Reader;
40pub use self::reader::ReaderBuilder;
41pub use self::reader::infer_schema_from_files;
42pub use self::writer::QuoteStyle;
43pub use self::writer::Writer;
44pub use self::writer::WriterBuilder;
45use arrow_schema::ArrowError;
46
47fn map_csv_error(error: csv::Error) -> ArrowError {
48    match error.kind() {
49        csv::ErrorKind::Io(error) => ArrowError::CsvError(error.to_string()),
50        csv::ErrorKind::Utf8 { pos, err } => ArrowError::CsvError(format!(
51            "Encountered UTF-8 error while reading CSV file: {}{}",
52            err,
53            pos.as_ref()
54                .map(|pos| format!(" at line {}", pos.line()))
55                .unwrap_or_default(),
56        )),
57        csv::ErrorKind::UnequalLengths {
58            pos,
59            expected_len,
60            len,
61        } => ArrowError::CsvError(format!(
62            "Encountered unequal lengths between records on CSV file. Expected {} \
63                 records, found {} records{}",
64            expected_len,
65            len,
66            pos.as_ref()
67                .map(|pos| format!(" at line {}", pos.line()))
68                .unwrap_or_default(),
69        )),
70        _ => ArrowError::CsvError("Error reading CSV file".to_string()),
71    }
72}