Skip to content

Commit

Permalink
Huge refactoring. Parser now uses byte strings.
Browse files Browse the repository at this point in the history
Using byte strings is unfortunate, but it allows the parsing to
be extremely permissive with respect to the encoding of CSV data.
In practice, this is important.

The high level decoding routines still assume UTF-8 support. I don't
have any plans to change this at the moment.

Also, I've refactored the API quite bit. If you were using rust-csv
before, this commit will almost certainly break your code. There are
too many changes to list. It would be best to just consult the API
documentation: http:https://burntsushi.net/rustdoc/csv/index.html

[breaking-change]
  • Loading branch information
BurntSushi committed Sep 2, 2014
1 parent d4b2b46 commit a9894d3
Show file tree
Hide file tree
Showing 11 changed files with 1,245 additions and 954 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "csv"
version = "0.1.0"
version = "0.2.0"
authors = ["Andrew Gallant <[email protected]>"]

[lib]
Expand Down
32 changes: 13 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ fn main() {
let fp = &Path::new("./data/simple.csv");
let mut rdr = csv::Decoder::from_file(fp);

for (s1, s2, dist) in rdr.decode_iter::<(String, String, uint)>() {
for record in rdr.iter_decode::<(String, String, uint)>() {
let (s1, s2, dist) = record.unwrap();
println!("({}, {}): {}", s1, s2, dist);
}
}
Expand All @@ -48,7 +49,8 @@ fn main() {
let fp = &Path::new("./data/simple.csv");
let mut rdr = csv::Decoder::from_file(fp);

for record in rdr.decode_iter::<Record>() {
for record in rdr.iter_decode::<Record>() {
let record = record.unwrap();
println!("({}, {}): {}", record.s1, record.s2, record.dist);
}
}
Expand Down Expand Up @@ -77,29 +79,21 @@ The API is fully documented with lots of examples:

### Installation

This will hopefully get easier when the new `cargo` package manager lands, but
for right now, you can either clone the repo and build manually or install with
`cargo-lite`.

From source:
This crate works with Cargo. Assuming you have Rust and
[Cargo](http:https://crates.io/) installed, simply check out the source and run
tests:

```bash
git clone git:https://github.com/BurntSushi/rust-csv
git checkout git:https://github.com/BurntSushi/rust-csv
cd rust-csv
rustc -O --crate-type lib ./src/lib.rs # makes libcsv-{version}.rlib in CWD
cd ./examples
rustc -O -L .. ./nfl_plays.rs
./nfl_plays
cargo test
```

For `cargo-lite`:
You can also add `rust-csv` as a dependency to your project's `Cargo.toml`:

```bash
pip2 install cargo-lite
cargo-lite install git:https://github.com/BurntSushi/rust-csv # installs to ~/.rust
cd ~/.rust/src/rust-csv/examples
rustc -O ./nfl_plays.rs
./nfl_plays
```toml
[dependencies.rust-csv]
git = "git:https://github.com/BurntSushi/rust-csv"
```


Expand Down
1 change: 0 additions & 1 deletion examples/nfl_plays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ fn main() {
let fp = &Path::new("./data/2012_nfl_pbp_data.csv");

let mut dec = Decoder::from_file(fp);
dec.has_headers(true);
match dec.decode_all::<Play>() {
Err(err) => fail!("{}", err),
Ok(plays) => {
Expand Down
3 changes: 2 additions & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ fn main() {
let fp = &Path::new("./data/simple.csv");
let mut rdr = csv::Decoder::from_file(fp);

for (s1, s2, dist) in rdr.decode_iter::<(String, String, uint)>() {
for record in rdr.iter_decode::<(String, String, uint)>() {
let (s1, s2, dist) = record.unwrap();
println!("({}, {}): {}", s1, s2, dist);
}
}
3 changes: 2 additions & 1 deletion examples/simple_missing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ fn main() {
let fp = &Path::new("./data/simple_missing.csv");
let mut rdr = csv::Decoder::from_file(fp);

for record in rdr.decode_iter::<Record>() {
for record in rdr.iter_decode::<Record>() {
let record = record.unwrap();
println!("({}, {}): {}", record.s1, record.s2, record.dist);
}
}
3 changes: 2 additions & 1 deletion examples/simple_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ fn main() {
let fp = &Path::new("./data/simple.csv");
let mut rdr = csv::Decoder::from_file(fp);

for record in rdr.decode_iter::<Record>() {
for record in rdr.iter_decode::<Record>() {
let record = record.unwrap();
println!("({}, {}): {}", record.s1, record.s2, record.dist);
}
}
1 change: 0 additions & 1 deletion src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ fn short_decoded_records(b: &mut Bencher) {
b.iter(|| {
let _ = ordie(data.seek(0, io::SeekSet));
let mut dec = Decoder::from_reader(&mut data as &mut io::Reader);
dec.has_headers(true);
match dec.decode_all::<Play>() {
Ok(_) => {}
Err(err) => fail!("{}", err),
Expand Down
Loading

0 comments on commit a9894d3

Please sign in to comment.