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