qsv_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* Has a header row? -- whether or not the first row of the data file provdes column headers
21* Number of preamble rows -- number of rows in a CSV file before the data starts (occasionally used
22in data files to introduce the data)
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* Is utf8-encoded? -- whether the file is utf-8 encoded
26* Number of delimiter/fields -- maximum number of delimiters in each row (and therefore number of fields in
27each row)
28* Field names - the name of each field
29* Types -- the inferred data type of each field in the data table
30
31See [`Metadata`](metadata/struct.Metadata.html) for full information about what the sniffer returns.
32
33# Setup
34
35Add this to your `Cargo.toml`:
36
37```toml
38[dependencies]
39csv-sniffer = "0.1"
40```
41
42and this to your crate root:
43
44```rust
45extern crate qsv_sniffer;
46```
47
48# Example
49
50This example shows how to write a simple command-line tool for discovering the metadata of a CSV
51file:
52
53```no_run
54extern crate qsv_sniffer;
55
56use std::env;
57
58fn main() {
59    let args: Vec<String> = env::args().collect();
60    if args.len() != 2 {
61        eprintln!("Usage: {} <file>", args[0]);
62        ::std::process::exit(1);
63    }
64
65    // sniff the path provided by the first argument
66    match qsv_sniffer::Sniffer::new().sniff_path(&args[1]) {
67        Ok(metadata) => {
68            println!("{}", metadata);
69        },
70        Err(err) => {
71            eprintln!("ERROR: {}", err);
72        }
73    }
74}
75```
76
77This example is provided as the primary binary for this crate. In the source directory, this can be
78run as:
79
80```ignore
81$ cargo run -- tests/data/library-visitors.csv
82```
83
84*/
85
86#![warn(missing_docs)]
87
88pub(crate) mod chain;
89pub mod error;
90pub mod metadata;
91
92mod sniffer;
93pub use sniffer::Sniffer;
94
95mod sample;
96pub use sample::SampleSize;
97
98mod field_type;
99pub use field_type::{DatePreference, Type};
100
101mod snip;