Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Interoperability with Error Reporting Tools #355

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ serde = "1.0.55"

[dev-dependencies]
bstr = { version = "1.7.0", default-features = false, features = ["alloc", "serde"] }
eyre = "~0.6.12"
serde = { version = "1.0.55", features = ["derive"] }

[profile.release]
Expand Down
18 changes: 18 additions & 0 deletions examples/tutorial-error-05.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use eyre::Result;
use std::{io, process};

fn main() {
if let Err(err) = run() {
println!("{:?}", err);
process::exit(1);
}
}

fn run() -> Result<()> {
let mut rdr = csv::Reader::from_reader(io::stdin());
for result in rdr.records() {
let record = result?;
println!("{:?}", record);
}
Ok(())
}
7 changes: 4 additions & 3 deletions examples/tutorial-perf-alloc-01.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{error::Error, io, process};
use eyre::Result;
use std::{io, process};

fn run() -> Result<u64, Box<dyn Error>> {
fn run() -> Result<u64> {
let mut rdr = csv::Reader::from_reader(io::stdin());

let mut count = 0;
Expand All @@ -19,7 +20,7 @@ fn main() {
println!("{}", count);
}
Err(err) => {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/tutorial-perf-alloc-02.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{error::Error, io, process};
use eyre::Result;
use std::{io, process};

fn run() -> Result<u64, Box<dyn Error>> {
fn run() -> Result<u64> {
let mut rdr = csv::Reader::from_reader(io::stdin());

let mut count = 0;
Expand All @@ -19,7 +20,7 @@ fn main() {
println!("{}", count);
}
Err(err) => {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/tutorial-perf-alloc-03.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{error::Error, io, process};
use eyre::Result;
use std::{io, process};

fn run() -> Result<u64, Box<dyn Error>> {
fn run() -> Result<u64> {
let mut rdr = csv::Reader::from_reader(io::stdin());
let mut record = csv::ByteRecord::new();

Expand All @@ -19,7 +20,7 @@ fn main() {
println!("{}", count);
}
Err(err) => {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
Expand Down
3 changes: 2 additions & 1 deletion examples/tutorial-perf-core-01.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use eyre::Report;
use std::io::{self, Read};
use std::process;

Expand Down Expand Up @@ -63,7 +64,7 @@ fn main() {
// Read the entire contents of stdin up front.
let mut data = vec![];
if let Err(err) = io::stdin().read_to_end(&mut data) {
println!("{}", err);
println!("{:?}", Report::from(err));
process::exit(1);
}
match run(&data) {
Expand Down
7 changes: 4 additions & 3 deletions examples/tutorial-perf-serde-01.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(dead_code)]
use std::{error::Error, io, process};
use eyre::Result;
use std::{io, process};

use serde::Deserialize;

Expand All @@ -15,7 +16,7 @@ struct Record {
longitude: f64,
}

fn run() -> Result<u64, Box<dyn Error>> {
fn run() -> Result<u64> {
let mut rdr = csv::Reader::from_reader(io::stdin());

let mut count = 0;
Expand All @@ -34,7 +35,7 @@ fn main() {
println!("{}", count);
}
Err(err) => {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/tutorial-perf-serde-02.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(dead_code)]
use eyre::Result;
use serde::Deserialize;
use std::{error::Error, io, process};
use std::{io, process};

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
Expand All @@ -14,7 +15,7 @@ struct Record<'a> {
longitude: f64,
}

fn run() -> Result<u64, Box<dyn Error>> {
fn run() -> Result<u64> {
let mut rdr = csv::Reader::from_reader(io::stdin());
let mut raw_record = csv::StringRecord::new();
let headers = rdr.headers()?.clone();
Expand All @@ -35,7 +36,7 @@ fn main() {
println!("{}", count);
}
Err(err) => {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/tutorial-perf-serde-03.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(dead_code)]
use std::{error::Error, io, process};
use eyre::Result;
use std::{io, process};

use serde::Deserialize;

Expand All @@ -15,7 +16,7 @@ struct Record<'a> {
longitude: f64,
}

fn run() -> Result<u64, Box<dyn Error>> {
fn run() -> Result<u64> {
let mut rdr = csv::Reader::from_reader(io::stdin());
let mut raw_record = csv::ByteRecord::new();
let headers = rdr.byte_headers()?.clone();
Expand All @@ -36,7 +37,7 @@ fn main() {
println!("{}", count);
}
Err(err) => {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
Expand Down
9 changes: 5 additions & 4 deletions examples/tutorial-pipeline-pop-01.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{env, error::Error, io, process};
use eyre::{eyre, Result};
use std::{env, io, process};

use serde::{Deserialize, Serialize};

Expand All @@ -14,11 +15,11 @@ struct Record {
longitude: f64,
}

fn run() -> Result<(), Box<dyn Error>> {
fn run() -> Result<()> {
// Get the query from the positional arguments.
// If one doesn't exist or isn't an integer, return an error.
let minimum_pop: u64 = match env::args().nth(1) {
None => return Err(From::from("expected 1 argument, but got none")),
None => return Err(eyre!("expected 1 argument, but got none")),
Some(arg) => arg.parse()?,
};

Expand Down Expand Up @@ -53,7 +54,7 @@ fn run() -> Result<(), Box<dyn Error>> {

fn main() {
if let Err(err) = run() {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
9 changes: 5 additions & 4 deletions examples/tutorial-pipeline-search-01.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{env, error::Error, io, process};
use eyre::{eyre, Result};
use std::{env, io, process};

fn run() -> Result<(), Box<dyn Error>> {
fn run() -> Result<()> {
// Get the query from the positional arguments.
// If one doesn't exist, return an error.
let query = match env::args().nth(1) {
None => return Err(From::from("expected 1 argument, but got none")),
None => return Err(eyre!("expected 1 argument, but got none")),
Some(query) => query,
};

Expand All @@ -31,7 +32,7 @@ fn run() -> Result<(), Box<dyn Error>> {

fn main() {
if let Err(err) = run() {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
9 changes: 5 additions & 4 deletions examples/tutorial-pipeline-search-02.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{env, error::Error, io, process};
use eyre::{eyre, Result};
use std::{env, io, process};

fn run() -> Result<(), Box<dyn Error>> {
fn run() -> Result<()> {
let query = match env::args().nth(1) {
None => return Err(From::from("expected 1 argument, but got none")),
None => return Err(eyre!("expected 1 argument, but got none")),
Some(query) => query,
};

Expand All @@ -26,7 +27,7 @@ fn run() -> Result<(), Box<dyn Error>> {

fn main() {
if let Err(err) = run() {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
11 changes: 6 additions & 5 deletions examples/tutorial-read-01.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{env, error::Error, ffi::OsString, fs::File, process};
use eyre::{eyre, Result};
use std::{env, ffi::OsString, fs::File, process};

fn run() -> Result<(), Box<dyn Error>> {
fn run() -> Result<()> {
let file_path = get_first_arg()?;
let file = File::open(file_path)?;
let mut rdr = csv::Reader::from_reader(file);
Expand All @@ -13,16 +14,16 @@ fn run() -> Result<(), Box<dyn Error>> {

/// Returns the first positional argument sent to this process. If there are no
/// positional arguments, then this returns an error.
fn get_first_arg() -> Result<OsString, Box<dyn Error>> {
fn get_first_arg() -> Result<OsString> {
match env::args_os().nth(1) {
None => Err(From::from("expected 1 argument, but got none")),
None => Err(eyre!("expected 1 argument, but got none")),
Some(file_path) => Ok(file_path),
}
}

fn main() {
if let Err(err) = run() {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
7 changes: 4 additions & 3 deletions examples/tutorial-read-delimiter-01.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{error::Error, io, process};
use eyre::Result;
use std::{io, process};

fn run() -> Result<(), Box<dyn Error>> {
fn run() -> Result<()> {
let mut rdr = csv::ReaderBuilder::new()
.has_headers(false)
.delimiter(b';')
Expand All @@ -18,7 +19,7 @@ fn run() -> Result<(), Box<dyn Error>> {

fn main() {
if let Err(err) = run() {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
7 changes: 4 additions & 3 deletions examples/tutorial-read-headers-01.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{error::Error, io, process};
use eyre::Result;
use std::{io, process};

fn run() -> Result<(), Box<dyn Error>> {
fn run() -> Result<()> {
let mut rdr =
csv::ReaderBuilder::new().has_headers(false).from_reader(io::stdin());
for result in rdr.records() {
Expand All @@ -12,7 +13,7 @@ fn run() -> Result<(), Box<dyn Error>> {

fn main() {
if let Err(err) = run() {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
7 changes: 4 additions & 3 deletions examples/tutorial-read-headers-02.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{error::Error, io, process};
use eyre::Result;
use std::{io, process};

fn run() -> Result<(), Box<dyn Error>> {
fn run() -> Result<()> {
let mut rdr = csv::Reader::from_reader(io::stdin());
{
// We nest this call in its own scope because of lifetimes.
Expand All @@ -20,7 +21,7 @@ fn run() -> Result<(), Box<dyn Error>> {

fn main() {
if let Err(err) = run() {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
7 changes: 4 additions & 3 deletions examples/tutorial-read-serde-01.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{error::Error, io, process};
use eyre::Result;
use std::{io, process};

fn run() -> Result<(), Box<dyn Error>> {
fn run() -> Result<()> {
let mut rdr = csv::Reader::from_reader(io::stdin());
for result in rdr.records() {
let record = result?;
Expand All @@ -27,7 +28,7 @@ fn run() -> Result<(), Box<dyn Error>> {

fn main() {
if let Err(err) = run() {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
7 changes: 4 additions & 3 deletions examples/tutorial-read-serde-02.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{error::Error, io, process};
use eyre::Result;
use std::{io, process};

// This introduces a type alias so that we can conveniently reference our
// record type.
type Record = (String, String, Option<u64>, f64, f64);

fn run() -> Result<(), Box<dyn Error>> {
fn run() -> Result<()> {
let mut rdr = csv::Reader::from_reader(io::stdin());
// Instead of creating an iterator with the `records` method, we create
// an iterator with the `deserialize` method.
Expand All @@ -18,7 +19,7 @@ fn run() -> Result<(), Box<dyn Error>> {

fn main() {
if let Err(err) = run() {
println!("{}", err);
println!("{:?}", err);
process::exit(1);
}
}
Loading