From 832cf7b35733b6b6c4014b7b865709b760f53a84 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 23 Apr 2017 15:37:17 -0700 Subject: [PATCH] Move output() and OutputError into brev (#187) They're pretty generic and generally useful, so move them into brev on crates.io. --- Cargo.lock | 6 +++--- src/lib.rs | 55 +++---------------------------------------------- src/platform.rs | 2 +- 3 files changed, 7 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 511d5369..ed35c3ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = "0.2.26" dependencies = [ "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "brev 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "brev 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.23.3 (registry+https://github.com/rust-lang/crates.io-index)", "edit-distance 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -45,7 +45,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "brev" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -217,7 +217,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6" "checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159" "checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4" -"checksum brev 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "363b8e31f04a5c26761f0a01da8f46eb172485d029a01042ad07fc01faef62ad" +"checksum brev 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "eb37ce3b65873276d1587151ef5b63bcc15c8da8dd5058792f5660cb32206d97" "checksum clap 2.23.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f57e9b63057a545ad2ecd773ea61e49422ed1b1d63d74d5da5ecaee55b3396cd" "checksum edit-distance 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cd50a61206c09132fdf9cbaccc64a82cfccb6be528453903e03d4eb4ec80a61d" "checksum either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18785c1ba806c258137c937e44ada9ee7e69a37e3c72077542cd2f069d78562a" diff --git a/src/lib.rs b/src/lib.rs index 6d77434a..ebbfb093 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ extern crate ansi_term; extern crate unicode_width; extern crate edit_distance; extern crate libc; +extern crate brev; #[cfg(test)] mod unit; @@ -34,11 +35,12 @@ use prelude::*; pub use app::app; use app::UseColor; +use brev::{output, OutputError}; +use platform::{Platform, PlatformInterface}; use std::borrow::Cow; use std::collections::{BTreeMap as Map, BTreeSet as Set}; use std::fmt::Display; use std::ops::Range; -use platform::{Platform, PlatformInterface}; macro_rules! warn { ($($arg:tt)*) => {{ @@ -101,48 +103,6 @@ fn contains(range: &Range, i: T) -> bool { i >= range.start && i < range.end } -#[derive(Debug)] -enum OutputError { - /// Non-zero exit code - Code(i32), - /// IO error upon execution - Io(io::Error), - /// Terminated by signal - Signal(i32), - /// Unknown failure - Unknown, - /// Stdtout not UTF-8 - Utf8(std::str::Utf8Error), -} - -/// Run a command and return the data it wrote to stdout as a string -fn output(mut command: process::Command) -> Result { - match command.output() { - Ok(output) => { - if let Some(code) = output.status.code() { - if code != 0 { - return Err(OutputError::Code(code)); - } - } else { - return Err(output_error_from_signal(output.status)); - } - match std::str::from_utf8(&output.stdout) { - Err(error) => Err(OutputError::Utf8(error)), - Ok(utf8) => { - Ok(if utf8.ends_with('\n') { - &utf8[0..utf8.len()-1] - } else if utf8.ends_with("\r\n") { - &utf8[0..utf8.len()-2] - } else { - utf8 - }.to_string()) - } - } - } - Err(io_error) => Err(OutputError::Io(io_error)), - } -} - #[derive(PartialEq, Debug)] struct Recipe<'a> { dependencies: Vec<&'a str>, @@ -257,15 +217,6 @@ fn error_from_signal( } } -/// Return an OutputError::Signal if the process was terminated by signal, -/// otherwise return an OutputError::UnknownFailure -fn output_error_from_signal(exit_status: process::ExitStatus) -> OutputError { - match Platform::signal_from_exit_status(exit_status) { - Some(signal) => OutputError::Signal(signal), - None => OutputError::Unknown, - } -} - fn export_env<'a>( command: &mut process::Command, scope: &Map<&'a str, String>, diff --git a/src/platform.rs b/src/platform.rs index 11f7f0ea..ac403db4 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -75,7 +75,7 @@ impl PlatformInterface for Platform { fn signal_from_exit_status(_exit_status: process::ExitStatus) -> Option { // The rust standard library does not expose a way to extract a signal - // from a process exit status, so just return None + // from a windows process exit status, so just return None None } }