From 091dfcd501a41e6edea8ea214234d7e7258cb08c Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Fri, 15 Sep 2017 11:47:24 -0700 Subject: [PATCH] Use `#[derive(ErrorChain)]` instead of `error_chain!` This cleans up the definition, and has slightly less finicky syntax. Fixes #5 --- Cargo.lock | 11 +++++++++ Cargo.toml | 1 + src/lib.rs | 71 +++++++++++++++++++++++++++-------------------------- src/task.rs | 2 +- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cef9f0a..2e489f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ version = "0.1.0" dependencies = [ "backtrace 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.26.0 (registry+https://github.com/rust-lang/crates.io-index)", + "derive-error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "derive_is_enum_variant 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", @@ -196,6 +197,15 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "derive-error-chain" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "derive_is_enum_variant" version = "0.1.1" @@ -724,6 +734,7 @@ dependencies = [ "checksum cpp_demangle 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7f374549b5aa87e566f17445e1aa17c86db0a8d19be7a72b2b1ac7bf5414df50" "checksum custom_derive 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "ef8ae57c4978a2acd8b869ce6b9ca1dfe817bff704c220209fdef2c0b75a01b9" "checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850" +"checksum derive-error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "92183014af72c63aea490e66526c712bf1066ac50f66c9f34824f02483ec1d98" "checksum derive_is_enum_variant 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d0ac8859845146979953797f03cc5b282fb4396891807cdb3d04929a88418197" "checksum env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b" "checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3" diff --git a/Cargo.toml b/Cargo.toml index 02eba27..0846396 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ path = "src/bin/starling.rs" required-features = ["clap"] [dependencies] +derive-error-chain = "0.11.0" derive_is_enum_variant = "0.1.1" error-chain = "0.11.0" futures = "0.1.15" diff --git a/src/lib.rs b/src/lib.rs index b7c90bb..61c6d46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,9 +9,9 @@ #![allow(unused_doc_comment)] #[macro_use] -extern crate derive_is_enum_variant; +extern crate derive_error_chain; #[macro_use] -extern crate error_chain; +extern crate derive_is_enum_variant; #[macro_use] extern crate futures; extern crate futures_cpupool; @@ -22,7 +22,7 @@ extern crate tokio_core; pub(crate) mod task; -use futures::{future, Future, Sink, Stream}; +use futures::{Sink, Stream}; use futures_cpupool::CpuPool; use futures::sync::mpsc; use std::cmp; @@ -33,38 +33,39 @@ use std::path; use std::sync::Arc; use std::thread; -error_chain! { - foreign_links { - Io(::std::io::Error) - /// An IO error. - ; - - SendError(mpsc::SendError<()>) - /// Tried to send a value on a channel when the receiving half was - /// already dropped. - ; - } - - errors { - /// Could not create a JavaScript runtime. - CouldNotCreateJavaScriptRuntime { - description("Could not create a JavaScript Runtime") - display("Could not create a JavaScript Runtime") - } - - /// Could not read a value from a channel. - CouldNotReadValueFromChannel { - description("Could not read a value from a channel") - display("Could not read a value from a channel") - } - - /// There was an exception in JavaScript code. - // TODO: stack, line, column, filename, etc - JavaScriptException { - description("JavaScript exception") - display("JavaScript exception") - } - } +/// The kind of error that occurred. +#[derive(Debug, ErrorChain)] +pub enum ErrorKind { + /// Some other kind of miscellaneous error, described in the given string. + Msg(String), + + /// An IO error. + #[error_chain(foreign)] + Io(::std::io::Error), + + /// Tried to send a value on a channel when the receiving half was already + /// dropped. + #[error_chain(foreign)] + SendError(mpsc::SendError<()>), + + /// Could not create a JavaScript runtime. + #[error_chain(custom)] + #[error_chain(description = r#"|| "Could not create a JavaScript Runtime""#)] + #[error_chain(display = r#"|| write!(f, "Could not create a JavaScript Runtime")"#)] + CouldNotCreateJavaScriptRuntime, + + /// Could not read a value from a channel. + #[error_chain(custom)] + #[error_chain(description = r#"|| "Could not read a value from a channel""#)] + #[error_chain(display = r#"|| write!(f, "Could not read a value from a channel")"#)] + CouldNotReadValueFromChannel, + + /// There was an exception in JavaScript code. + // TODO: stack, line, column, filename, etc + #[error_chain(custom)] + #[error_chain(description = r#"|| "JavaScript exception""#)] + #[error_chain(display = r#"|| write!(f, "JavaScript exception")"#)] + JavaScriptException, } impl Clone for Error { diff --git a/src/task.rs b/src/task.rs index c70b3d5..d4781bc 100644 --- a/src/task.rs +++ b/src/task.rs @@ -25,7 +25,7 @@ //! [ongoing]: https://bugzilla.mozilla.org/show_bug.cgi?id=1323066 use super::{Error, ErrorKind, Result, StarlingHandle, StarlingMessage}; -use futures::{self, future, Async, Future, Sink}; +use futures::{self, Async, Future, Sink}; use futures::sync::oneshot; use futures_cpupool::CpuFuture; use futures::sync::mpsc;