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