Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit cca6dff

Browse files
committed
move from error-chain to failure
1 parent 97b443c commit cca6dff

File tree

7 files changed

+122
-94
lines changed

7 files changed

+122
-94
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.rs.bk
2+
.#*
23
target

Cargo.lock

Lines changed: 60 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ keywords = ["parse", "tool", "ARM", "ITM"]
77
license = "MIT OR Apache-2.0"
88
name = "itm"
99
repository = "https://github.com/japaric/itm"
10-
version = "0.2.1"
10+
version = "0.3.0"
1111

1212
[dependencies]
1313
chrono = "^0.4"
1414
clap = "^2.14.0"
1515
env_logger = "^0.4.3"
16-
error-chain = "^0.11.0"
16+
failure = "0.1.1"
17+
failure_derive = "0.1.1"
1718
log = "^0.3.8"
1819

1920
[dev-dependencies]

src/bin/itmdump.rs

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
extern crate chrono;
44
extern crate clap;
55
extern crate env_logger;
6+
extern crate failure;
67
extern crate itm;
78
#[macro_use]
89
extern crate log;
910

1011
use clap::{App, Arg, ArgMatches};
11-
use itm::error::{Error, ErrorKind, Result, ResultExt};
12-
use itm::{packet, Decoder};
12+
use itm::{packet, Decoder, Error};
1313
use log::{LogLevelFilter, LogRecord};
1414
use std::fs::File;
1515
use std::io::Write;
@@ -33,31 +33,14 @@ fn main() {
3333
.init()
3434
.unwrap();
3535

36-
fn show_backtrace() -> bool {
37-
env::var("RUST_BACKTRACE").as_ref().map(|s| &s[..]) == Ok("1")
38-
}
39-
4036
if let Err(e) = run() {
41-
let stderr = io::stderr();
42-
let mut stderr = stderr.lock();
43-
44-
writeln!(stderr, "{}", e).unwrap();
45-
46-
for e in e.iter().skip(1) {
47-
writeln!(stderr, "caused by: {}", e).unwrap();
48-
}
49-
50-
if show_backtrace() {
51-
if let Some(backtrace) = e.backtrace() {
52-
writeln!(stderr, "{:?}", backtrace).unwrap();
53-
}
54-
}
37+
eprintln!("{}", e);
5538

5639
process::exit(1)
5740
}
5841
}
5942

60-
fn run() -> Result<()> {
43+
fn run() -> Result<(), failure::Error> {
6144
let matches = App::new("itmdump")
6245
.version(concat!(
6346
env!("CARGO_PKG_VERSION"),
@@ -117,41 +100,39 @@ fn run() -> Result<()> {
117100
}
118101
_ => (),
119102
},
120-
Err(e @ Error(ErrorKind::UnknownHeader(_), _)) => {
121-
// We don't know this header type; warn and continue.
122-
debug!("{}", e);
123-
}
124-
Err(Error(ErrorKind::EofBeforePacket, _)) => {
125-
if follow {
126-
// NOTE 10 ms let us achieve 60 FPS in the worst case scenario
127-
thread::sleep(Duration::from_millis(10));
103+
Err(fe) => {
104+
if let Some(ie) = fe.downcast_ref::<Error>().cloned() {
105+
match ie {
106+
Error::UnknownHeader { byte } => {
107+
// We don't know this header type; warn and continue.
108+
debug!("unknown header byte: {:x}", byte);
109+
}
110+
Error::EofBeforePacket => {
111+
if follow {
112+
// NOTE 10 ms let us achieve 60 FPS in the worst case scenario
113+
thread::sleep(Duration::from_millis(10));
114+
} else {
115+
// !follow and EOF. Exit.
116+
return Ok(());
117+
}
118+
}
119+
_ => return Err(fe.into()),
120+
}
128121
} else {
129-
// !follow and EOF. Exit.
130-
return Ok(());
122+
return Err(fe);
131123
}
132124
}
133-
134-
// FIXME: If in follow mode, we may try to read a packet
135-
// but reach the end of the file in the middle. Currently
136-
// we'd just return an error and hence exit. When we
137-
// receive an error, we've already read and discarded some
138-
// data, so we can't just go around this loop again.
139-
//
140-
// We could make a file following wrapper around `read`
141-
// that blocks in a loop retrying the read and sleeping if
142-
// there's no data.
143-
Err(e) => return Err(e),
144125
}
145126
} // end of read loop
146127

147128
// Unreachable.
148129
}
149130

150-
fn open_read(matches: &ArgMatches) -> Result<Box<io::Read + 'static>> {
131+
fn open_read(matches: &ArgMatches) -> Result<Box<io::Read + 'static>, io::Error> {
151132
let path = matches.value_of("file");
152133
Ok(match path {
153134
Some(path) => {
154-
let f = File::open(path).chain_err(|| format!("Couldn't open source file '{}'", path))?;
135+
let f = File::open(path)?;
155136
Box::new(f) as Box<io::Read + 'static>
156137
}
157138
None => Box::new(io::stdin()) as Box<io::Read + 'static>,

src/decoder.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! Parse ITM packets from bytes and streams.
22
3-
use error::{Error, ErrorKind, Result};
4-
use packet::{self, Instrumentation, Packet};
53
use std::io::{self, Cursor, Read};
64

5+
use failure;
6+
7+
use packet::{self, Instrumentation, Packet};
8+
use Error;
9+
710
/// Parses ITM packets.
811
pub struct Decoder<R: Read> {
912
inner: R,
@@ -45,13 +48,13 @@ impl<R: Read> Decoder<R> {
4548

4649
/// Read a single packet from the inner `Read`. This will block
4750
/// for input if no full packet is currently an available.
48-
pub fn read_packet(&mut self) -> Result<Packet> {
51+
pub fn read_packet(&mut self) -> Result<Packet, failure::Error> {
4952
let mut header = [0; 1];
5053
match self.inner.read_exact(&mut header) {
5154
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof => {
52-
return Err(Error::from(ErrorKind::EofBeforePacket))
55+
return Err(Error::EofBeforePacket.into())
5356
}
54-
Err(e) => return Err(Error::from(e)),
57+
Err(e) => return Err(e.into()),
5558
Ok(_) => (),
5659
};
5760
let header = header[0];
@@ -76,9 +79,9 @@ impl<R: Read> Decoder<R> {
7679
let buf = &mut ud.payload[0..payload_len];
7780
match read_exact_gently(&mut self.inner, buf, self.follow) {
7881
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof => {
79-
return Err(Error::from(ErrorKind::EofDuringPacket))
82+
return Err(Error::EofDuringPacket.into());
8083
}
81-
Err(e) => return Err(Error::from(e)),
84+
Err(e) => return Err(e.into()),
8285
Ok(_) => (),
8386
};
8487
}
@@ -88,15 +91,13 @@ impl<R: Read> Decoder<R> {
8891
kind: packet::Kind::Instrumentation(ud),
8992
})
9093
}
91-
_ => {
92-
return Err(Error::from(ErrorKind::UnknownHeader(header)));
93-
}
94+
_ => Err(Error::UnknownHeader { byte: header }.into()),
9495
}
9596
}
9697
}
9798

9899
/// Decode a single packet from a slice of bytes.
99-
pub fn from_slice(s: &[u8]) -> Result<Packet> {
100+
pub fn from_slice(s: &[u8]) -> Result<Packet, failure::Error> {
100101
let mut d = Decoder::new(Cursor::new(Vec::from(s)), false);
101102
d.read_packet()
102103
}

src/error.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/lib.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,36 @@
8181
//!
8282
//! [CoreSight ITM]: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0314h/CAAGGCDH.html
8383
84-
#![allow(renamed_and_removed_lints)]
85-
#![allow(unused_doc_comments)]
8684
#![deny(missing_docs)]
8785
#![deny(warnings)]
8886

87+
extern crate failure;
8988
#[macro_use]
90-
extern crate error_chain;
89+
extern crate failure_derive;
9190
#[allow(unused_imports)] // No logging yet.
9291
#[macro_use]
9392
extern crate log;
9493

9594
pub mod decoder;
9695
pub use decoder::Decoder;
9796

98-
pub mod error;
99-
10097
pub mod packet;
98+
99+
/// Error type
100+
#[derive(Clone, Copy, Debug, Fail)]
101+
pub enum Error {
102+
/// end of file before packet
103+
#[fail(display = "end of file before packet")]
104+
EofBeforePacket,
105+
106+
/// end of file during packet
107+
#[fail(display = "end of file during packet")]
108+
EofDuringPacket,
109+
110+
/// unknown header
111+
#[fail(display = "unknown header byte: {:x}", byte)]
112+
UnknownHeader {
113+
/// unknown header byte
114+
byte: u8,
115+
},
116+
}

0 commit comments

Comments
 (0)