diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2018-11-09 10:23:53 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2018-11-09 10:23:53 +0100 |
commit | 269aaf28deb8d1e53e450950480dbb39299ea76e (patch) | |
tree | cf19152fd05c0883b93e753d80ad9e230608044b | |
parent | 15c4de5e0ff12535bb7de6df8dfdc88bd9727762 (diff) | |
parent | 29e713c6efe55602922e0a7986b950afbd0a5500 (diff) |
Merge branch 'failure'
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | default.nix | 2 | ||||
-rw-r--r-- | src/error.rs | 42 | ||||
-rw-r--r-- | src/import.rs | 10 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/tw.rs | 23 |
7 files changed, 46 insertions, 38 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d18ad3..9479f86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This changelog was started with the 0.4.0 release. ## Next +## 0.6.0 + +* Switched error handling to `failure` * Added TaskBuilder * Added support for user defined attributes (short UDA) via the task-hookrs::uda module. (This reintroduced the dependency to the "log" crate.) @@ -27,7 +27,7 @@ serde_json = "1" uuid = { version = "0.7", features = ["serde", "v4"] } log = "0.4" derive_builder = "0.6.0" -error-chain = "0.12" +failure = "0.1" [dev-dependencies] env_logger = "0.5" diff --git a/default.nix b/default.nix index 9547b71..475b991 100644 --- a/default.nix +++ b/default.nix @@ -1,7 +1,7 @@ { pkgs ? (import <nixpkgs> {}) }: let - env = with pkgs.rustStable; [ + env = with pkgs.rustChannels.stable; [ rustc cargo pkgs.llvmPackages.lldb diff --git a/src/error.rs b/src/error.rs index a936736..2ed9bd5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,24 +4,26 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. // -//! Error module, containing error types -error_chain!{ - errors { - /// Error kind indicating that the JSON parser failed - ParserError { - description("Failed to create a Task from JSON") - } - /// Error kind indicating that the Reader failed to read something - ReaderError { - description("Failed to read tasks from a Reader") - } - /// Error kind indicating that a call to the task warrior binary failed - TaskCmdError { - description("There was a problem while calling the external 'task' binary") - } - /// Error kind indicating that a conversion to JSON failed - SerializeError { - description("A Task could not be converted to JSON") - } - } +//! Definitions for error handling with failure + +/// Failure error kind type, defining error messages +#[derive(Debug, Clone, Eq, PartialEq, Fail)] +pub enum ErrorKind { + + /// Error kind indicating that the JSON parser failed + #[fail(display = "Failed to create a Task from JSON")] + ParserError, + + /// Error kind indicating that the Reader failed to read something + #[fail(display = "Failed to read tasks from a Reader")] + ReaderError, + + /// Error kind indicating that a call to the task warrior binary failed + #[fail(display = "There was a problem while calling the external 'task' binary")] + TaskCmdError, + + /// Error kind indicating that a conversion to JSON failed + #[fail(display = "A Task could not be converted to JSON")] + SerializeError } + diff --git a/src/import.rs b/src/import.rs index dc5a9fc..68009b1 100644 --- a/src/import.rs +++ b/src/import.rs @@ -10,20 +10,22 @@ use std::io::BufRead; use std::io::Read; use serde_json; +use failure::Fallible as Result; +use failure::ResultExt; +use failure::Error; use task::Task; -use error::{Result, ResultExt}; use error::ErrorKind as EK; /// Import taskwarrior-exported JSON. This expects an JSON Array of objects, as exported by /// taskwarrior. pub fn import<R: Read>(r: R) -> Result<Vec<Task>> { - serde_json::from_reader(r).chain_err(|| EK::ParserError) + serde_json::from_reader(r).context(EK::ParserError).map_err(Error::from) } /// Import a single JSON-formatted Task pub fn import_task(s: &str) -> Result<Task> { - serde_json::from_str(s).chain_err(|| EK::ParserError) + serde_json::from_str(s).context(EK::ParserError).map_err(Error::from) } /// Reads line by line and tries to parse a task-object per line. @@ -31,7 +33,7 @@ pub fn import_tasks<BR: BufRead>(r: BR) -> Vec<Result<Task>> { let mut vt = Vec::new(); for line in r.lines() { if line.is_err() { - vt.push(Err(line.unwrap_err()).chain_err(|| EK::ReaderError)); + vt.push(Err(line.unwrap_err()).context(EK::ReaderError).map_err(Error::from)); continue; } // Unwrap is safe because of continue above @@ -53,7 +53,7 @@ extern crate uuid; #[macro_use] extern crate derive_builder; #[macro_use] -extern crate error_chain; +extern crate failure; #[cfg(test)] extern crate env_logger; @@ -9,13 +9,16 @@ //! in your path. This will always call task and never interact with your `.task` directory itself. //! (This is in accordance with the taskwarrior api guide lines.) -use error::{Result, ResultExt, ErrorKind as EK}; +use error::ErrorKind as EK; use task::Task; use std::process::{Command, Stdio, Child}; use std::io::Write; use std::iter::once; use import::import; + use serde_json; +use failure::Fallible as Result; +use failure::ResultExt; /// This will give you all tasks which match the given query in the taskwarrior query syntax. /// This is not sanitized. Never get the query string from an untrusted user. @@ -36,23 +39,21 @@ pub fn add_query_to_cmd(query: &str, mut cmd: Command) -> Command { /// This executes the given Command and trys to convert the Result into a Vec<Task>. pub fn run_query_cmd(mut cmd: Command) -> Result<Vec<Task>> { - let mut export = cmd.spawn().chain_err(|| EK::TaskCmdError)?; - export.wait().chain_err(|| EK::TaskCmdError)?; - import(export.stdout.chain_err(|| EK::TaskCmdError)?) + let mut export = cmd.spawn().context(EK::TaskCmdError)?; + export.wait().context(EK::TaskCmdError)?; + import(export.stdout.ok_or(EK::TaskCmdError)?) } /// This function runs the given Command, pipes the tasks as JSON to it and returns a handle to the child process. pub fn save_to_cmd<'a>(tasks: Vec<&'a Task>, mut cmd: Command) -> Result<Child> { - let input_buffer = serde_json::to_string(&tasks).chain_err( - || EK::SerializeError, - )?; - let mut import = cmd.spawn().chain_err(|| EK::TaskCmdError)?; + let input_buffer = serde_json::to_string(&tasks).context(EK::SerializeError)?; + let mut import = cmd.spawn().context(EK::TaskCmdError)?; import .stdin .as_mut() - .chain_err(|| EK::TaskCmdError)? + .ok_or(EK::TaskCmdError)? .write_all(input_buffer.as_bytes()) - .chain_err(|| EK::TaskCmdError)?; + .context(EK::TaskCmdError)?; Ok(import) } @@ -63,7 +64,7 @@ pub fn save<'a, T>(tasks: T) -> Result<()> where T: IntoIterator<Item = &'a Task>, { - save_async(tasks)?.wait().chain_err(|| EK::TaskCmdError)?; + save_async(tasks)?.wait().context(EK::TaskCmdError)?; Ok(()) } |