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#![doc(
23    html_logo_url = "https://arrow.apache.org/img/arrow-logo_chevrons_black-txt_white-bg.svg",
24    html_favicon_url = "https://arrow.apache.org/img/arrow-logo_chevrons_black-txt_transparent-bg.svg"
25)]
26#![cfg_attr(docsrs, feature(doc_cfg))]
27#![deny(clippy::allow_attributes)]
28#![warn(missing_docs)]
29
30pub mod reader;
31pub mod writer;
32
33pub use self::reader::Reader;
34pub use self::reader::ReaderBuilder;
35pub use self::reader::infer_schema_from_files;
36pub use self::writer::QuoteStyle;
37pub use self::writer::Writer;
38pub use self::writer::WriterBuilder;
39use arrow_schema::ArrowError;
40
41fn map_csv_error(error: csv::Error) -> ArrowError {
42    match error.kind() {
43        csv::ErrorKind::Io(error) => ArrowError::CsvError(error.to_string()),
44        csv::ErrorKind::Utf8 { pos, err } => ArrowError::CsvError(format!(
45            "Encountered UTF-8 error while reading CSV file: {}{}",
46            err,
47            pos.as_ref()
48                .map(|pos| format!(" at line {}", pos.line()))
49                .unwrap_or_default(),
50        )),
51        csv::ErrorKind::UnequalLengths {
52            pos,
53            expected_len,
54            len,
55        } => ArrowError::CsvError(format!(
56            "Encountered unequal lengths between records on CSV file. Expected {} \
57                 records, found {} records{}",
58            expected_len,
59            len,
60            pos.as_ref()
61                .map(|pos| format!(" at line {}", pos.line()))
62                .unwrap_or_default(),
63        )),
64        _ => ArrowError::CsvError("Error reading CSV file".to_string()),
65    }
66}