From 2def00501b0fde2a78d4f4e1eb1b63b17b72ef1e Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Mon, 4 Jun 2018 22:33:39 +0300 Subject: [PATCH] Use failure instead of error chain --- Cargo.lock | 12 ++---------- Cargo.toml | 3 ++- src/git.rs | 4 ++-- src/main.rs | 40 ++++++++++++++-------------------------- 4 files changed, 20 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8ea4d427..3d44702a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,14 +38,6 @@ name = "cfg-if" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "error-chain" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "backtrace 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "failure" version = "0.1.1" @@ -189,7 +181,8 @@ dependencies = [ name = "topgrade" version = "0.2.0" dependencies = [ - "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "os_type 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "which 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -257,7 +250,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum backtrace-sys 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)" = "5fd343a2466c4603f76f38de264bc0526cffc7fa38ba52fb9f13237eccc1ced2" "checksum cc 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "49ec142f5768efb5b7622aebc3fdbdbb8950a4b9ba996393cb76ef7466e8747d" "checksum cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "405216fd8fe65f718daa7102ea808a946b6ce40c742998fbfd3463645552de18" -"checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3" "checksum failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "934799b6c1de475a012a02dab0ace1ace43789ee4b99bcfbf1a2e3e8ced5de82" "checksum failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c7cdda555bb90c9bb67a3b670a0f42de8e73f5981524123ad8578aafec8ddb8b" "checksum lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e6412c5e2ad9584b0b8e979393122026cdd6d2a80b933f890dcd694ddbe73739" diff --git a/Cargo.toml b/Cargo.toml index b9c825f9..995215c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ exclude = ["doc/screenshot.gif"] [dependencies] which = "2.0.0" -error-chain = "0.11.0" os_type = "2.0.0" termion = "1.5.1" +failure = "0.1.1" +failure_derive = "0.1.1" diff --git a/src/git.rs b/src/git.rs index 5b78c5ad..af26f32b 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,4 +1,4 @@ -use super::error::*; +use failure::Error; use std::path::{Path, PathBuf}; use std::process::Command; use which::which; @@ -38,7 +38,7 @@ impl Git { None } - pub fn pull>(&self, path: P) -> Result<()> { + pub fn pull>(&self, path: P) -> Result<(), Error> { if let Some(git) = &self.git { if let Ok(mut command) = Command::new(&git) .arg("pull") diff --git a/src/main.rs b/src/main.rs index 262906ed..e5ef14ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,28 +1,14 @@ +extern crate failure; extern crate os_type; extern crate which; #[macro_use] -extern crate error_chain; +extern crate failure_derive; extern crate termion; -mod error { - error_chain!{ - foreign_links { - Io(::std::io::Error); - } - - errors { - ProcessFailed { - description("Process failed") - display("Process failed") - } - } - } -} - mod git; mod terminal; -use error::*; +use failure::Error; use git::Git; use os_type::OSType; use std::collections::HashSet; @@ -32,16 +18,20 @@ use std::process::{Command, ExitStatus}; use terminal::Terminal; use which::which; +#[derive(Fail, Debug)] +#[fail(display = "Process failed")] +struct ProcessFailed; + trait Check { - fn check(self) -> Result<()>; + fn check(self) -> Result<(), Error>; } impl Check for ExitStatus { - fn check(self) -> Result<()> { + fn check(self) -> Result<(), Error> { if self.success() { Ok(()) } else { - Err(ErrorKind::ProcessFailed.into()) + Err(Error::from(ProcessFailed {})) } } } @@ -68,7 +58,7 @@ fn tpm() -> Option { } } -fn run() -> Result<()> { +fn main() -> Result<(), Error> { let git = Git::new(); let mut git_repos: HashSet = HashSet::new(); let terminal = Terminal::new(); @@ -177,7 +167,7 @@ fn run() -> Result<()> { .args(&["apt", "dist-upgrade"]) .spawn()? .wait() - .map_err(|e| e.into()) + .map_err(Error::from) })?; } } @@ -203,7 +193,7 @@ fn run() -> Result<()> { .arg("get-updates") .spawn()? .wait() - .map_err(|e| e.into()) + .map_err(Error::from) })?; } @@ -228,7 +218,7 @@ fn run() -> Result<()> { .arg("upgrade") .spawn()? .wait() - .map_err(|e| e.into()) + .map_err(Error::from) })?; } @@ -241,5 +231,3 @@ fn run() -> Result<()> { Ok(()) } - -quick_main!(run);