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