csv_sniffer/lib.rs
1/*!
2This `csv-sniffer` crate provides methods to infer CSV file details (delimiter choice, quote
3character, number of fields, field data types, etc.).
4
5# Overview
6
7The [`Sniffer`](struct.Sniffer.html) type is the primary entry point for using this crate. Its
8[`Sniffer::open_path`](struct.Sniffer.html#method.open_path) and
9[`Sniffer::open_reader`](struct.Sniffer.html#method.open_reader) methods return a configured
10[`csv::Reader`](https://docs.rs/csv).
11
12Alternatively, the [`Sniffer::sniff_path`](struct.Sniffer.html#method.sniff_path) and
13[`Sniffer::sniff_reader`](struct.Sniffer.html#method.sniff_reader) methods return a
14[`Metadata`](metadata/struct.Metadata.html) object containing the deduced details about the
15underlying CSV input.
16
17This sniffer detects the following metadata about a CSV file:
18
19* Delimiter -- byte character between fields in a record
20* Number of preamble rows -- number of rows in a CSV file before the data starts (occasionally used
21in data files to introduce the data)
22* Has a header row? -- whether or not the first row of the data file provdes column headers
23* Quote -- byte character (either ", ', or `) used to quote fields, or that the file has no quotes
24* Flexible -- whether or not records are all of the same length
25* Delimiter count -- maximum number of delimiters in each row (and therefore number of fields in
26each row)
27* Types -- the inferred data type of each field in the data table
28
29See [`Metadata`](metadata/struct.Metadata.html) for full information about what the sniffer returns.
30
31# Setup
32
33Add this to your `Cargo.toml`:
34
35```toml
36[dependencies]
37csv-sniffer = "0.1"
38```
39
40and this to your crate root:
41
42```rust
43extern crate csv_sniffer;
44```
45
46# Example
47
48This example shows how to write a simple command-line tool for discovering the metadata of a CSV
49file:
50
51```no_run
52extern crate csv_sniffer;
53
54use std::env;
55
56fn main() {
57 let args: Vec<String> = env::args().collect();
58 if args.len() != 2 {
59 eprintln!("Usage: {} <file>", args[0]);
60 ::std::process::exit(1);
61 }
62
63 // sniff the path provided by the first argument
64 match csv_sniffer::Sniffer::new().sniff_path(&args[1]) {
65 Ok(metadata) => {
66 println!("{}", metadata);
67 },
68 Err(err) => {
69 eprintln!("ERROR: {}", err);
70 }
71 }
72}
73```
74
75This example is provided as the primary binary for this crate. In the source directory, this can be
76run as:
77
78```ignore
79$ cargo run -- tests/data/library-visitors.csv
80```
81
82*/
83
84#![warn(missing_docs)]
85
86extern crate csv;
87extern crate csv_core;
88extern crate regex;
89#[macro_use]
90extern crate bitflags;
91extern crate memchr;
92
93pub(crate) mod chain;
94pub mod error;
95pub mod metadata;
96
97mod sniffer;
98pub use sniffer::Sniffer;
99
100mod sample;
101pub use sample::SampleSize;
102
103pub(crate) mod field_type;
104pub use field_type::Type;
105
106mod snip;