csv_scout/lib.rs
1/*!
2This `csv-scout` 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-scout = "0.1"
40```
41
42# Example
43
44This example shows how to write a simple command-line tool for discovering the metadata of a CSV
45file:
46
47```no_run
48
49use std::env;
50
51fn main() {
52 let args: Vec<String> = env::args().collect();
53 if args.len() != 2 {
54 eprintln!("Usage: {} <file>", args[0]);
55 ::std::process::exit(1);
56 }
57
58 // sniff the path provided by the first argument
59 match csv_scout::Sniffer::new().sniff_path(&args[1]) {
60 Ok(metadata) => {
61 println!("{}", metadata);
62 },
63 Err(err) => {
64 eprintln!("ERROR: {}", err);
65 }
66 }
67}
68```
69
70This example is provided as the primary binary for this crate. In the source directory, this can be
71run as:
72
73```ignore
74$ cargo run -- tests/data/library-visitors.csv
75```
76
77*/
78
79#![warn(missing_docs)]
80
81pub(crate) mod chain;
82pub mod error;
83pub mod metadata;
84
85mod sniffer;
86pub use sniffer::Sniffer;
87
88mod sample;
89pub use sample::SampleSize;
90
91// mod field_type;
92// pub use field_type::{DatePreference, Type};
93
94// mod snip;